Loops in cpp


C++ Loops




What is Loop?

A statement or a set of statements that is executed repeatedly is called loop. The statements in a loop are executed for a specified number of times or until some given condition remains true.

In C++, there are three kinds of loop statements. These are:

  • The "while" loop
  • The "for" loop
  • The "do-while" loop


The "while" Loop

It is a conditional loop statement. It is used to execute a statement or a set of statement as long as the given condition remains true.

Syntax:

The syntax of the while loop is:

   while (condition)
     {
        Statement;
      }

where

condition: It consists of a relational expression. If it is true, the statement or the set of statement given in the while loop is executed.

statement: It represents the body of loop. The compound statements or a set of statements are written in braces {}.


Example:

Print "I Love Pakistan" five times using the "while" loop.


#include<iostream>

using namespace std;

int main()

{

int a=1;

while(a<=5)

{

   cout<<"I Love Pakistan"<<"\n";

   a=a+1;

   }

}


Output:

Pakistan

Pakistan

Pakistan

Pakistan

Pakistan



Flowchart:




The "do-while" Loop

The do-while loop is also a conditional loop statement. It is like a while loop but in this loop the condition is tested after executing the statement of the loop.

Syntax:

The syntax of the do-while is:

   do

      { 

        statements;

       }

        while (condition);


where

do                  is a keyword of C++. It indicates the starting of the do-while loop.

statements   statements enclosed in braces represent the body of loop.

condition      it is the condition that must remain true for the execution of the loop.


Example:

Print first ten numbers by using do-while loop.

 #include<iostream>

using namespace std;

int main()

{

    int a;

    a=1;

    do

    {

        cout<<a<<"\n";

        a++;

        }

        while(a<=10);

}


Output:

1

2

3

4

5

6

7

8

9

10


Flowchart:




The "for" Loop

The for loop statement is used to execute a set of statements repeatedly for a fixed number of times. It is also known as counter loop.

The structure of this loop is different from both the "while" and the "do-while" loops. It has the following parts:

  • initialization
  • condition
  • increment or decrement
  • body of the loop


Syntax:

The general syntax of the for loop is:

for (initialization; condition; increment/decrement)


Example:

Write a program to print first ten natural numbers using for loop.

#include<iostream>

using namespace std;

int main ()

{

int a;

for(a=1; a<=10; a++)

cout<<a<<"\n";

}


Flowchart:






Difference between while and do-while loop

while loop:

In while loop, test condition comes before the body of the loop. First the condition is tested and the then body of the loop is executed.

do-while loop:

In do-while loop, the body of the loop comes before the test condition. The body of the loop is executed and then the condition is tested.


Post a Comment

Previous Post Next Post