learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!
VB - soon here!

Search

 

 

 

 

Next Back


This lesson will show you how to:

  • Use the while loop
  • Use the do-while loop

Q: Why did the blonde try and steal a police car?
A: She saw "911" on the back and thought it was a Porsche.

The while statement

    The while provides a mechanism for repeating C statements wiles a condition is true. Its format is:

    while (condition)
      program statement;

Somewhere within the body of the loop a statement must alter a value of the condition to allow the loop to finish.

/* Sample program including the while loop */

#include <stdio.h>

int main(void)
{

int loop = 0;

while (loop <=10)
    {
        printf(“%d”, loop);
        ++loop
    }

return 0;

}

Sample program output

0
1

10

The above program uses a while loop to repeat the statements:

printf(“%d”, loop);
++loop

whilst the value of the variable loop is less than or equal to 10.

Note how the variable upon which the while is dependant is initialized prior too the while statement (in this case the previous line), and also that the value of the variable is altered within the loop, so that eventually the conditional test will succeed and the while loop will terminate. The program is functionally equivalent to the earlier for program which counted to ten.

 

The do while statement

The do {} while statement allows a loop to continue whilst a condition evaluates as TRUE (non-zero). The loop is executed at least once

/* Demonstration of the do…..while loop */

#include <stdio.h>

int main(void)
{

int value, r_digit;

printf(“Enter a number to be reversed.\n”);
scanf(“%d”, &value);

do
    {
        r_digit = value % 10;
        printf(“%d”, r_digit);
        value = value / 10;
    } while (value != 0);

printf(“\n”);

return 0;


}

The above program reverses a number that is entered by the user. It does this by using the modulus % operator to extract the right most digit into the variable r_digit. The original number is then divided by 10, and the operation is repeated whilst the number is not equal to 0.

It is our contention that this programming construct is improper and should be avoided. It has potential problems, and you should be aware o these.

One such problem is deemed to be lack of control. Considering the above program code portion:

do
    {
        r_digit = value % 10;
        printf(“%d”, r_digit);
        value = value / 10;
    } while (value != 0);

There is NO choice whether to execute the loop. Entry to the loop automatic, as you only get a choice to continue.

Another problem is that the loop is always executes at least once. This is a by-product of the lack of control. This means its possible to enter a do {} while loop with invalid data.

Beginner programmers can easily get into a while heap of trouble, so our advice is to avoid its use. This is the only time that you will encounter it in this course. Its easy to avoid the use of this construct by replacing it with the following algorithms:

initialize loop control variable
while (loop control variable is valid)
    {
    process data
    adjust control variable if necessary
    }

Okay, lets now rewrite the above example and remove the do {} while construct.

/* rewritten code to remove construct */

#include <stdio.h>

int main(void)
{

int value, r_digit;

value = 0;

while (value <=0)
    {
        printf(“Enter the number to be reversed.\n”);
        scanf(“%d”, &value);
        if (value <= 0)
            printf(“The number must be positive\n”);
    }

while (value !=0)
    {
        r_digit = value % 10);
        printf(“%d”, r_digit);
        value = value / 10;
    }
printf(“\n”);

return 0;


}

Sample program output

Enter the number to be reversed.
-43
The number is positive
Enter the number to be reversed.
423
324

back to top          exercises on lesson 10        lesson 9          lesson 11

 

 






1