You are on page 1of 2

C++ Lecturer.

Ali al-musharfawi

The while Statement


The general form of the while statement is:

while (expression)
statement;

 First expression (called the loop condition) is evaluated. If


the outcome is nonzero.
 Then statement (called the loop body) is executed and the
whole process is repeated.
 Otherwise, the loop is terminated.

The do Statement
The general form of the do statement is:

do
statement;
while (expression);

 First statement is executed and then expression is


evaluated.
 If the outcome of the latter is nonzero
Then the whole process is repeated. Otherwise, the loop is
terminated.

Example1: Output: ( 0 1 2 3 4 5 6 7 8 9 )
i = 0;
while ( i < 10 )
{
cout << i;
i ++;
}
C++ Lecturer. Ali al-musharfawi

Example 2: Output: even numbers only ( 0 2 4 6 8 )


i = 0;
while ( i < 10 )
{
cout << i;
i += 2;
}

Example 3: Output: odd numbers only ( 1 3 5 7 9 )


i = 1;
while ( i < 10 )
{
cout << i;
i += 2;
}

You might also like