Guides:C/C Crash Course/Loops

From CoderGuide

Jump to: navigation, search

Back to TOC

Loops

If-else and switch statements are cool, but how do we make our program go back and run again? We use loops for that (there is a goto statement, but it is considered very bad form to use in structured programming and is never needed).

The first loop we're going to talk about is the while, and do-while loops, next we'll cover the for loop.

The while and do-while loops

The while loop takes a similar form to the if statement, except there is no "else." The while loop will continue to execute the statements in the code block that immediately follow the while statement until the logical expression becomes false. The do-while statement is similar, except it executes everything between the do{ and the }while(expression) until that expression becomes false. Unlike the while loop, the do-while evaluate the logical expression after it has gone through the loop at least once.

Here is a few examples:

while(1){
        printf("I'll run forever!\n");
}/*end while(1)*/
 
while( (2+2)==5){
        printf("Only runs if your computer fried\n");
}/*end while( (2+..*/
 
do{
        printf("I will be displayed only once\n.");
}while(0)

There are two special statements that are used to break out of a loop, or to start the loop over again. They are break; and continue;. I see little use for the continue; statement, and I've personally never used it in any program I have written. The break; statement finds its place in many of the loops I write, and is the only way to break out of an otherwise infinite loop (aside from issuing the return statement which will break out of the current function).

The for loop

The for loop is a little bit more complicated in it's syntax, and hence, a little more confusing. It takes the form of:

for( initialization; expression; operation) statement; 

The initialization is performed first, the expression is the logical comparison that takes place, and the operation is something that is done every time the loop executes (in addition to the statement, or code block that follows the for loop, if it is present). Usually, the operation is an increment or decrement operation. If the decrement or increment operator is before (prefixed) the variable name, then the increment or decrement is performed first before the logical expression is evaluated. The for loop will continue until the logical expression is false, or it encounters a break statement.

On 80x86 (Intel/AMD and clones), and some other processors, the compiler can take advantage of a special case loop where you're counting a single integer down to zero, and then stops looping when the zero has been reached. A quick example:

#include <stdio.h>
 
int main(){
        int a,b;
 
 
        /*We're using a as our counter*/
 
        for(a=0;a<10;a++)printf("%d \n",a);
 
        /*You can initialize more than one variable if you
          separate the initializations with commas as so: */
 
        for(a=0,b=1;0;); /*this will never execute, as the
                           logical expression is false (zero)*/
        for(;;){ /*This loop will run forever*/
 
                break; /*unless broken*/
        }/*end for(;;)*/
}/*end main()*/

A key point to remember that some people new to C can get confused about: Even though these statements require parameters within parentheses, they are not functions. They are keywords and a part of the basic building blocks of the language. Functions will be discussed next.

Another thing about loops: If you're loop is counting up or down, should never use the == operator for testing the counter. You should always use either, <, >, <=, or >= to prevent your program from going into an infinite loop. If you use the straight equal-to comparison, then, if you accidentally miss that number for some reason, then your loop will continue on forever. Yup, done that more than once.... An that loop was spawning a new instance of it's self each time it looped! My computer didn't crash, it just got VERY slow (I was running Linux at the time, of course).

Ready to make a game? Lets move on to Game Example - Guess the Number

Personal tools