unit will cover following topics:
the completion of this unit the learner will be able to :
while statement creates a loop that repeats until the test expression
or zero. The while
statement is an entry-condition loop;
the decision to go
one or more loop is made before the loop is traversed.
Thus, it is possible
the loop is never traversed. The
statement part of the
form can be a simple
or a compound statement.
( expression )
statement portion is
repeated until the expression
becomes false or zero.
while ( x < 1000 )
x = x + step;
step = 2 * x;
the above example, if x is less than 1000 then the statements are executed
x is equal to or greater than 1000.
do while statement
do while statement creates a loop that repeats until the
false or zero. The do
while statement is an exit-condition loop;
decision to go through one more pass of the loop is traversed.
must be executed at least once. The statement part of the form can
a simple statement or a compound statement.
statement portion is repeated until the expression becomes false or zero.
while ( number != 20 );
the above example, the program reads all the value typed at the consol
types a number 20. After user types a 20, the program exits the loop.
are two instructions used in a loop to cause the program flow to jump
one location of a program to another location.
break command can be used with any of the loop forms and
with the switch
It causes program control to skip over the rest of the loop or
it and to resume with the next command following the loop or switch.
switch ( number )
case 4: printf (“
That’s a good choice. \n”);
case 5: printf (“
That’s a fair choice.\n”);
printf(“That’s a poor choice.\n”);
the above example, the program checks to see whether the number is 4, 5
something else. If the number is 4, then the program prints "
That's a good
and exits the switch statement, if the number
is 5 then the program
" That's a fair choice" and exits the switch
statement and for all other
it print " That's a poor choice."
continue command can be used with any of the loop forms but
not with a
statement. It causes program
control to skip the remaining statements in
loop. For a while or for
loop, the next loop cycle is started. For a do while
the exit condition is tested and then, if necessary, the next loop cycle
while ( (ch = getchar()) != EOF )
if ( ch == ‘ ‘)
fragment of code echoes and counts the non space characters. If the
a space then it skips the character count and continues looping until the
end of file.
Assignment: Worksheet 6