The second loop available in C/C++ is the while loop. Its general form is
while(condition) statement;
where statement is either an empty statement, a single statement, or a block of
statements. The condition may be any expression, and true is any nonzero value. The
loop iterates while the condition is true. When the condition becomes false, program
control passes to the line of code immediately following the loop.
The following example shows a keyboard input routine that simply loops until the
user types A:
char wait_for_char(void)
{
char ch;
ch = '\0'; /* initialize ch */
while(ch != 'A') ch = getchar();
return ch;
}
C h a p t e r 3 : S t a t e m e n t s 77
while(condition) statement;
where statement is either an empty statement, a single statement, or a block of
statements. The condition may be any expression, and true is any nonzero value. The
loop iterates while the condition is true. When the condition becomes false, program
control passes to the line of code immediately following the loop.
The following example shows a keyboard input routine that simply loops until the
user types A:
char wait_for_char(void)
{
char ch;
ch = '\0'; /* initialize ch */
while(ch != 'A') ch = getchar();
return ch;
}
C h a p t e r 3 : S t a t e m e n t s 77
First, ch is initialized to null. As a local variable, its value is not known when
wait_for_char() is executed. The while loop then checks to see if ch is not equal to A.
Because ch was initialized to null, the test is true and the loop begins. Each time you
press a key, the condition is tested again. Once you enter an A, the condition becomes
false because ch equals A, and the loop terminates.
Like for loops, while loops check the test condition at the top of the loop, which
means that the body of the loop will not execute if the condition is false to begin with.
This feature may eliminate the need to perform a separate conditional test before the
loop. The pad() function provides a good illustration of this. It adds spaces to the end
of a string to fill the string to a predefined length. If the string is already at the desired
length, no spaces are added.
#include <stdio.h>
#include <string.h>
void pad(char *s, int length);
int main(void)
{
char str[80];
strcpy(str, "this is a test");
pad(str, 40);
printf("%d", strlen(str));
return 0;
}
/* Add spaces to the end of a string. */
void pad(char *s, int length)
{
int l;
l = strlen(s); /* find out how long it is */
while(l<length) {
s[l] = ' '; /* insert a space */
l++;
}
s[l]= '\0'; /* strings need to be
terminated in a null */
}
wait_for_char() is executed. The while loop then checks to see if ch is not equal to A.
Because ch was initialized to null, the test is true and the loop begins. Each time you
press a key, the condition is tested again. Once you enter an A, the condition becomes
false because ch equals A, and the loop terminates.
Like for loops, while loops check the test condition at the top of the loop, which
means that the body of the loop will not execute if the condition is false to begin with.
This feature may eliminate the need to perform a separate conditional test before the
loop. The pad() function provides a good illustration of this. It adds spaces to the end
of a string to fill the string to a predefined length. If the string is already at the desired
length, no spaces are added.
#include <stdio.h>
#include <string.h>
void pad(char *s, int length);
int main(void)
{
char str[80];
strcpy(str, "this is a test");
pad(str, 40);
printf("%d", strlen(str));
return 0;
}
/* Add spaces to the end of a string. */
void pad(char *s, int length)
{
int l;
l = strlen(s); /* find out how long it is */
while(l<length) {
s[l] = ' '; /* insert a space */
l++;
}
s[l]= '\0'; /* strings need to be
terminated in a null */
}
The two arguments of pad() are s, a pointer to the string to lengthen, and length, the
number of characters that s should have. If the length of string s is already equal to or
greater than length, the code inside the while loop does not execute. If s is shorter than
length, pad() adds the required number of spaces. The strlen() function, part of the
standard library, returns the length of the string.
If several separate conditions need to terminate a while loop, a single variable
commonly forms the conditional expression. The value of this variable is set at various
points throughout the loop. In this example,
void func1(void)
{
int working;
working = 1; /* i.e., true */
while(working) {
working = process1();
if(working)
working = process2();
if(working)
working = process3();
}
}
any of the three routines may return false and cause the loop to exit.
There need not be any statements in the body of the while loop. For example,
while((ch=getchar()) != 'A') ;
will simply loop until the user types A. If you feel uncomfortable putting the
assignment inside the while conditional expression, remember that the equal sign is
just an operator that evaluates to the value of the right-hand operand.
number of characters that s should have. If the length of string s is already equal to or
greater than length, the code inside the while loop does not execute. If s is shorter than
length, pad() adds the required number of spaces. The strlen() function, part of the
standard library, returns the length of the string.
If several separate conditions need to terminate a while loop, a single variable
commonly forms the conditional expression. The value of this variable is set at various
points throughout the loop. In this example,
void func1(void)
{
int working;
working = 1; /* i.e., true */
while(working) {
working = process1();
if(working)
working = process2();
if(working)
working = process3();
}
}
any of the three routines may return false and cause the loop to exit.
There need not be any statements in the body of the while loop. For example,
while((ch=getchar()) != 'A') ;
will simply loop until the user types A. If you feel uncomfortable putting the
assignment inside the while conditional expression, remember that the equal sign is
just an operator that evaluates to the value of the right-hand operand.
No comments:
Post a Comment