Loop structures:while loop,do-while loop,for loop

What is Looping structure in C++

Looping Structure in C++

Loop


A type of control structure that repeats a statement or set of statements for a specific number of time is called loop.It is also known as iterative or repetitive structure.In sequential structure all statements are executed once.On the other hand,conditional structure may execute or skip a statement on the basis of a condition.
There are three types of Loops:

  • While Loop
  • Do-While Loop
  • For Loop
                          ---------------------------------------------------------

while Loop:

This is the simplest loop of c++ language.This loop execute one or more statement while the given condition remains true.It is useful when the number of repetations not known in advence.

While Loop Flowchart

Syntax:
              while(condition)
                       statement;

compound statement syntax:
                    while(condition)
                    {
                          statement1;
                         statement2;
                         :
                        :
                        statementN;
                   }

                      ---------------------------------------------------------

do while Loop:

In this loop the condition comes after the body of the program.This loop execute one or more statement while given condition tru.This is also an iterative loop.This loop is important when loop body must be executed at least once.In this loop semi-colon is also used after the condition as  ' while(condition); '
Syntax:
                     do
                    {
                       statement1;
                       statement2;
                       statement3;
                    }
                   while(condition);
do-while loop flowchart

                         ---------------------------------------------------------

For Loop: 

For loop is also called counter-controlled loop.It is the most flexible loop.This loop execute one or more statement for a specified number of time.
syntax:
           


              for(initialization;condition;increment/decrement)
             {
               statement1;
              statement2;
            }
for loop flowchart

Loop structures:while loop,do-while loop,for loop Loop structures:while loop,do-while loop,for loop Reviewed by Shoaib Jameel on 3/14/2017 09:31:00 pm Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.