Page 48 - DCAP404 _Object Oriented Programming
P. 48
Unit 2: Beginning of OOP Language
3 Notes
5
8
13
21
34
55
The control exits the loop when the condition, num2 == 8 9, becomes true.
Task Break statement is used with the conditional switch statement and with the do, for,
and while loop statements. Explain how with an example.
The do-while Statement
Sometimes, however, it is desirable to have a loop with the test for continuation at the end of
each pass. This can be accomplished by means of the do-while statement.
The general form of the do-while statement is:
do
statement
while (expression);
The statement will be executed repeatedly, as long as the value of expression is true (i.e., is
nonzero). Notice that the statement will always be executed at least once, since the test for
repetition does not occur until the end of the first pass through the loop. The statement can be
either simple or compound, though most applications will require it to be a compound statement.
It must include some feature that eventually alters the value of expression so the looping action
can terminate.
Here is another example to do the same thing, using the to display the consecutive digits (0, 1, 1,
2, 9) using the do-while loop.
#include <studio.h>
main() /* display the integers 0 through 9 */
{
int digit = 0;
do {
printf (“%d\n”, digit++);
}while (digit <= 9);
}
This program below accepts characters from keyboard until the character, ‘I’, is entered and
displays whether the total number of consonant characters entered is more than, less than, or
equal to the total number of vowels entered.
LOVELY PROFESSIONAL UNIVERSITY 41