While loop


 

OBJECTIVE

This unit will cover following topics:

  • Writing C program using loop characteristics

    • While ...

    • do ... while

    • continue

    • break

At the completion of this unit the learner will be able to :

  •  Develop application in C Language using loops.


The while statement

 

The while statement creates a loop that repeats until the test expression becomes 

false, or zero.  The while statement is an entry-condition loop;  the decision to go 

through one or more loop is made before the loop is traversed.  Thus, it is possible 

that the loop is never traversed.  The statement part of the form can be a simple 

statement or a compound statement.

 

Format

while ( expression )

        statement

 

The statement portion is repeated until the expression becomes false or zero.

Examples:

            while ( x < 1000 )

                        {

                          x = x + step;

                          step = 2 * x;

                        }

In the above example, if x is less than 1000 then the statements are executed

until x is equal to or greater than 1000.

The do while statement

 

The do while statement creates a loop that repeats until the test expression 

becomes false or zero.  The do while statement is an exit-condition loop;  

the decision to go through one more pass of the loop is traversed.  Thus, the 

loop must be executed at least once.  The statement part of the form can 

be a simple statement or a compound statement.

 

Format:

do

       statement

while (expression);

 

The statement portion is repeated until the expression becomes false or zero.

 

Example:

            do

                        scanf(“%d”, &number);

            while ( number != 20 );

 

In the above example, the program reads all the value typed at the consol until the 

user types a number 20. After user types a 20, the program exits the loop.

Program Jumps

 

Following are two instructions used in a loop to cause the program flow to jump 

from one location of a program to another location.

  • break command

  • continue command

The break Command

The break command can be used with any of the loop forms and with the switch 

statement.  It causes program control to skip over the rest of the loop or switch 

containing it and to resume with the next command following the loop or switch.

Example:

            switch ( number )

                        {

                         case 4: printf (“ That’s a good choice. \n”);

                                    break;

                         case 5: printf (“ That’s a fair choice.\n”);

                                    break;

                         default: printf(“That’s a poor choice.\n”);

                        }

 

In the above example, the program checks to see whether the number is 4, 5

or something else.  If the number is 4, then the program prints " That's a good 

choice." and exits the switch statement, if the number is 5 then the program

prints " That's a fair choice" and exits the switch statement and for all other 

cases it print " That's a poor choice."

The continue Command

The continue command can be used with any of the loop forms but not with a 

switch statement.  It causes program control to skip the remaining statements in 

a loop.  For a while or for loop, the next loop cycle is started.  For a do while 

loop, the exit condition is tested and then, if necessary, the next loop cycle is 

started.

Example:

            while ( (ch = getchar()) != EOF )

                        {

                         if ( ch == ‘ ‘)

                                    continue;

                         putchar (ch);

                         chcount++;

                        }

 

This fragment of code echoes and counts the non space characters. If the character

is a space then it skips the character count and continues looping until the end of file.


Reading Assignment: Worksheet 6