NIOS Computer Science: Chapter 9 – Control Statements Part 8

Download PDF of This Page (Size: 254K)

Image of do-while loop

Image of Do-While Loop

Image of do-while loop

Do-While Loop

Syntax of do-while loop

do

{

--------

} while (condition);

Structure of do while loop

Structure of Do While Loop

Structure of do while loop

The flow diagram indicates that after each execution of the loop body, the condition is true; the loop body is executed again.

If the condition evaluates to false, loop exit occurs and the next program statement is executed.

In the do-while loop, the loop termination test is performed after each execution of the loop body; hence, the loop body is always executed at least once.

Example

To find the sum of the first N natural number.

# include < iostream.h>

void main ( )

{

int N, number, sum;

cin >> N;

sum = 0;

number = 1;

do

{

sum + = number;

number + + ;

}

while (number < = N) ;

cout << sum;

}