Loops


LOOPS

The mechanism to perform certain action repeatedly is known as Looping. It causes a section of program to be repeated up to certain number of times. This repeatation will be continued while the condition is true. When the condition becomes false, the loop will end there and control of program will be passed to statements outside of the loop.

There are Three major types of Loop structures in C++:
  1. For Loop
  2. While Loop 
  3. Do While Loop
 

The For Loop

The for loop is used to execute the block of statements for a specified number of times. It’s usually used when you know that how many times you want to execute the statements.
 

The for loop contains four major parts

  1. Initial expression: It is used to initialize any loop variable. It executes only once when the loop starts.
  2.  Conditional expression or Test Expression: It specifies any condition. It is made up of Logical operators. It executes each time through the loop, just before the body of the loop executed.
  3.  Increment expression: It increments the variable when the loop executes. It always executed at the end of the loop after the loop body execution.

  4. Body of Loop: Body of Loop contains either single statements or there can be a  block of statements depends on the program requirements.

Syntax


for( initial-expression; conditional-expression;  increment-expression)







Example:

int main()
{
int a;
for(a=1;a<=10;a++)
            {
cout<<a;
}
getche();
}
Output
 1  2  3  4  5  6  7  8  9  10


The While Loop

While Loop is used when we don't know how many times we want to execute certain statements.

Syntax

While( expression )
statement;

The statement may be a single statement or compound statement (enclosed in the braces {} ). The statement is executed zero or more times until the expression becomes FALSE. In the operation of while loop the expression is first tested if this test is FALSE, Then statement is never executed. And control passed from the while statement to the rest of the program. If the test is TRUE , then statement is executed and the process is repeated again.

Example


int main()
{
int a=1;
while(a<=10)
            {
cout<<a;
a++;
            }
}
Output
 1  2  3  4  5  6  7  8  9  10

The do-while loop

The do-while loop is similar to the while loop, the difference being that the  condition is judged after the loop is executed, but while loop tests the condition before the loop is executed.

  

Syntax


do

Statement;

while(expression);


Where statement may be a single or a compound statement, statement is executed one or more times until expression becomes FALSE ( a value of 0), if the expression becomes FALSE, the do statement terminates, and control passes to the next statement in the program. Otherwise if the expression is TRUE, statement is repeated, and the process stars over again. The main difference between while and do-while loop is that do-while loop is execute at least once, even the expression is FALSE.


 

Example


int main()
{
int a=1;

do

{
cout<<a;

a++;

}
while(a<=10);

}

Output


1        2  3  4  5  6  7  8  9  10
  1.      Program to generate ASCII code table from 1 to 255 using For Loop
  2.      Program to calculate sum of first 100 Natural numbers using For Loop
  3.      Program which accepts an integer and prints its factorial using For Loop 
  4.      Program to display X pyramid Structure
  5.      Program which accepts a number and prints its Table
  6.      Program to generate series of natural numbers from 1 to 100 using While Loop
  7.      Program to generate series of Even numbers from 1 to 100 using While Loop
  8.      Program to generate series of Odd numbers from 1 to 100 using do-while loop
  9.      Program to generate the following output using for loop

                            1
                            12
                            123
                            1234
                            12345