Loading....
Coupon Accepted Successfully!

 

Iteration Statement

  • The iteration statement allows a set of instruction to be performed repeatedly until a certain condition is fulfilled.
  • They are of three types. They are
    • for loop
    • while loop
    • do - while loop

for loop Statement

  • for statement is used to execute a statement or a group of statements repeatedly for a known number of times.
  • The general format is

for (initialization, test condition, iteration)

{

body of the loop;

}

next statement;

Where

  1. initialization condition is used to set initial values to control variables.
  2. test condition is used to check the control variable. According to this the loop is executed or not.
  3. Iteration statement is used to perform increment or decrement operation to change the value of the control variables.
    • When the for statement is executed the value of the control variable is initialized and tested with the test condition.
    • If the value of the test condition is true, the body of the loop will be executed and the control is transferred to the for statement.
    • Then the value of the control variables is incremented or decremented.
    • When the test condition becomes false the control is transferred to the next statement.
    • The body of the loop is executed repeatedly as long as the test value is true.
    • The block diagram is as below.

Program 5: to check the execution of for loop.

#include <iostream.h>

void main()

{

int i, sum;

sum =0;

for( i=1;i<=100; i++)

{

sum = sum + i;

}

cout << "sum = %d"<<sum;

}

Note: The initialization part of for loop should be terminated only with a semicolon (;).

While Statement

  • While loop is an entry controlled loop.
  • The general format is

while (test condition)

{

Body of the loop;

}

Next statement;

  • When this statement is executed, the computer first evaluates the test condition.
  • If the value is false, the control is transferred to next statement.
  • If the value is true, then the body of the loop is executed repeatedly until the test condition becomes false.
  • When the test condition becomes false the control is transferred to next statement.
  • The block diagram

Program 6: This program displays the message.

#include <iostream.h>

void main()

{

int i;

i =1;

while( i < 5)

{

cout <"god loves you';

i++;

}

}

Note: Rules to be followed for while loop
  • The test condition should be any relational or logical expression enclosed within brackets.
  • If the body of the loop contains more than one statement, the {} brackets must be used.

do... while Statement

  • It is similar like for and while loop.
  • It evaluates expression at the bottom of the loop after executing its loop body statement.
  • The general form is

do

{

Body of the statement;

}

while (test condition);

next statement;

  • When this statement is executed toe body of the loop is executed first.
  • Then the test condition is evaluated.
  • If the value is false, the control is transferred to the next statement.
  • If the value id true the body of the loop is executed repeatedly until the test condition becomes false.
  • When the test condition becomes false the control is transferred to the next statement.
  • The block diagram

Program 7: to display a message.

 

#include <iostream.h>

void main()

{

int i;

i =1;

do

{

cout <"god loves you';

i++;

}

while( i < 5);

}

Note: Rules to be followed for do...while loop
  • The test condition should be any relational or logical expression enclosed within brackets.
  • If the body of the loop contains more than one statement, the {} brackets must be used.
  • The while statement should be immediately after the body of the loop.




Test Your Skills Now!
Take a Quiz now
Reviewer Name