Revisiting the While Loop in C

Computers’ ability to process large amounts of data is partly due to their ability to repeat a task endlessly, and without complaint.

The while loop enables our programs to repeat a series of statements, over and over, so long as a certain test condition is met before each repetition.

The while statement is a looping statement that controls the execution of an associated series of statements. Looping statements cause delineated areas of a program to execute repeatedly, as long as certain conditions are being met.

#include <stdio.h>


int main(void){
    
    int a=73;
    int b=42;
    int c = 0;
    
    while(a-- > b){
        printf("repeating this loop for the %d time\n",
                ++c);
    }
    
    
    return 0;
    
}

It is required to put parentheses around the test expression. As long as this expression resolves to True, the code block will be executed repeatedly, until the expression becomes False. Braces are required around the body of the while loop, unless the body is only one single statement.

#include <stdio.h>

int main(void){
    
    //1 is always true
    while(1){
        printf("This loop would go on forever\n");
        break;
    }
    
    printf("...were it not for the break statement.\n");
    
    
    return 0;
    
}

The body of the loop must in some way change the values used in the test expression, otherwise the loop will execute forever; this is what is known as an infinite loop. Although, the break statement can also be used as an alternative way to end the loop.

The body of a while loop can contain one or more C statements, including additional while loops.

#include <stdio.h>

int main(void){
    
    char a='a';
    int x;
    
    while(a!='z'){
        putchar(a++);
        x = 1;
        while(x<11){
            printf("%3d", x++);
        }
        putchar('\n');
    }
       
    return 0;
    
}

A while loop can be used to ensure valid user keyboard input. We can include both a prompt and a scanf() function within the body of the while loop to ensure that the user is prompted for input until the test expression resolves to False.

#include <stdio.h>

int main(void){
    
    char answer='x';
    
    while((answer!='y') && (answer!='n')){
        printf("Should we continue? (y/n)\n");
        scanf(" %c", &answer);
    }
    
    return 0;
    
}

The while loop tests that test expression at the top of the loop; this is why we set the value to be tested before starting the loop, so that we could be sure the expression would evaluate to be True at least once, so that the prompt would be displayed.

If you want to learn more about C, get my book http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/.

It is filled with completely new content not seen on this blog.

 

 

Advertisements

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