Types of Loops for,while and do-while.

Types of Loops for,while and do-while.

Types of loops for, while, do-while.

1.The for loop

The for loop is the easiest to understand of the C++ loop. All its loop control elements are gathered in one place (on the top of the loop), while on the other loop construction of C++, they (top contrl elements ) are scattered about the program. 

The general form syntax of the for loop statement is
for  (initialization expression(s) ; test-expression ; update expression(s) )
         body-of-the-loop ;

The following example program illustrates the use of for statement.

#include<iostream.h>
int main()
{
int n;
cout<<"Enter the number to print the table :- ";
cin>>n;
int i;
for(i=1;i<=10;++1)
cout<<"n * "<<i<<"="<<n*i;
return 0;
}



2. The while Loop

The second loop available in C++ is the while loop. The while loop is an entry-controlled loop. The syntax of a while loop is 

while(expression)
          loop-body

where the loop-body may contain a single statement, a compound statement or an empty statement. The loop iterates while the expression evaluates to true. When the expression becomes false, the program control passes to the line after the loop-body code.

In a while loop, a loop control variable should be initialized before the loop begins as an uninitialized variable can be used in an expression. The loop variable should be updated inside the body-of the-while, Following example program illustrates the working of a while loop. 

#include<iostream.h>
#include<stdlib.h>
int main()
{
system("cls");
int i, num, fact = 1;
cout<<"\n Enter integer :";
cin>>num;
i=num;
while(num)
{
fact * = num;
--num;
}
cout<<"The factorial of "<<i<<"is "<<fact<<"\n";
return 0;
}

Output:  Enter integer : 5
                The factorial of 5 is 120

3. The do-while Loop

Unlike the for and while loops, the do-while is an exit-controlled loop i.e., it evaluates its test-expression at the bottom of the loop after executing its loop-body statements. This means that a do-while loop always executes at least once.

In the other two loops for and while, the test-expression is evaluated at the beginning of the loop i.e., before executing the loop-body. If the test-expression evaluates to false for the first time itself, the loop is never executed. But in some situations, it is wanted that the loop-body is executed at least once, no matter what the initial state of the test-expression is. In such cases, the do-while loop is the obvious choice. The syntax of the do-while loop is:

do{
         statement;

     }
while(test-expression);

The braces {} are not necessary when the loop-body contains a single statement. The following do-while loop prints all upper-case letters:
     
     char ch='A';
     do{cout<<"\n"<<ch;
           ch++;
      }while (ch<='Z');

The above code prints characters from 'A' onward until the condition ch <= 'Z' becomes false.

The most common use of the do-while loop is in menu selection routine, where the menu is flashed at least once. Then depending upon the user's response it is either repeated or terminated. The following example program is a menu selection program.

You may also like to visit:-



Post a Comment

0 Comments