C Tutorial (14) : while & do..while LOOP

A loop is simply a section of code that repeats a few times. You don’t want a loop to repeat forever— that’s called an infinite loop. The loops you write (if you write them properly) should come to a conclusion when they finish doing the job you set them up to do.

while

The while statement always appears at the beginning or end of a loop. The easiest type of loop that uses while is called the while loop. (The other is called the do…while loop. You’ll see it a little later.) Here is the format of while:

while (condition)
{
 block of one or more C statements; 
}

The condition is a relational test that is exactly like the relational test condition you learned for ‘if’. The block of one or more C statements is called the body of the while.

The body of the while repeats as long as the condition is true. This is the difference between a while statement and an if statement: The body of the if executes if the condition is true. The body of the if executes only once, however, whereas the body of the while can execute a lot of times.

Figure shown below helps explain the similarities and differences between if and while. The formats of the two commands are similar, in that braces are required if the body of the while has more than one statement. Even if the body of the while contains only a single statement, you should enclose the body in braces so that the braces will still be there if you later add statements to the while. Never put a semicolon after the while’s parenthesis. The semicolon follows only the statements inside the body of the while.

You must somehow change a variable inside the while loop’s condition. If you don’t, the while will loop forever because it will test the same condition each time through the loop. Therefore, you avoid infinite loops by making sure the body of the while loop changes something in the condition so that eventually the condition becomes false and the program continues with the statements that follow the while loop.

 

c_while

Note : As with if, the while might never execute! If the condition is false going into while the first time, the body of the while doesn’t execute.

If you want to repeat a section of code until a certain condition becomes false, while is the way to go.

#include <stdio.h>

main()
{
int ctr = 0;

while (ctr < 5)
{
  printf("Counter is at %d.\n", ++ctr);
}

while (ctr > 1)
{
  printf("Counter is at %d.\n", --ctr);
}

return 0;
}

The body continues to execute until ctr is incremented to 5. Because 5 is not less than 5 (they are equal), the condition becomes false and the loop stops repeating.

Note : If ctr were not incremented in the while loop, the printf() would execute forever or until you pressed Ctrl+Break to stop it.

do…while

while also can be used in conjunction with the do statement. When used as a pair, the statements normally are called do…while statements or the do…while loop. The do…while behaves almost exactly like the while loop. Here is the format of do…while:

do
{
 block of one or more C statements; 
} while (condition);

Note : The do and while act like wrappers around the body of the loop. Again, braces are required if the body has more than a single statement.

Use a do…while in place of a while only when the body of the loop must execute at least one time. The condition is located at the bottom of the do…while loop, so C can’t test the condition until the loop finishes the first time.

/* This program will multiply two numbers and display the result for 
   as long as the user wants. Answering 'N' will break the loop. */

#include <stdio.h>

main()
{
float num1, num2, result; 
char choice;

do {

printf("Enter your first number to multiply: "); 
scanf(" %f", &num1);

printf("Enter your second number to multiply: "); 
scanf(" %f", &num2);


result = num1 * num2;
printf("%.2f times %.2f equals %.2f\n\n", num1, num2, result);

printf("Do you want to enter another pair of numbers "); 
printf("to multiply (Y/N): ");

scanf(" %c", &choice);

/* If the user enters a lowercase n, this if statement will 
   convert it to an N */
if (choice == 'n')
{
choice = 'N';
}
} while (choice != 'N');

return 0;
}

Although this program is simple and straightforward, it demonstrates an effective use of a do…while loop.

Converting a lowercase n to N is not the only way you could account for this possibility. You could also use a logical AND operator in the while portion of the loop, as follows:

} while (choice != 'N'&& choice != 'n');

 

Note : Don’t put a semicolon after the while condition’s closing parenthesis. If you do, an infinite loop will occur. But its required for do..while loop.

Conditional, Increment-Decrement operators < Prev        Next > For Loop

 

Advertisements

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s