Computer Science: Control Statements: For Loop: Do While Loop

Get top class preparation for IAS right from your home: Get complete video lectures from top expert with unlimited validity: cover entire syllabus, expected topics, in full detail- anytime and anywhere & ask your doubts to top experts.

Download PDF of This Page (Size: 213K)

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.

}

Developed by: