Introduction to Programming in C

Index   |   Using VC++
 
Introduction
Basic Output
Variables
Conditionals
5 Loops
Example 1
More on Conditionals
Further Exploration
Algorithm Design
10 Arrays
 
< Previous

Loops

Next >
  • Create a project called Loops.
  • Create a source file called main.c.
  • Type in (or copy/paste) the following code:
  • #include <stdio.h>
    
    /* print the numbers 1 to 9, inclusive */
    int main()
    {
    	int i;
    
    	i = 1;			// start at 1
    	while (i <= 9)
    	{
    		printf("i is now %d\n", i);
    		i = i + 1;	// increase by 1
    	}
    	/* execution will continue here when the "while"
    	   condition is false */
    
    	return 0;
    }
    
  • Save your file.
  • Build your program (F7).
  • Check the Output window for errors.
  • Run your program (Ctrl-F5).

Explanations

  • As in the if statement, the while statement contains a Boolean expression that determines whether the block following the while expression is executed. The difference is that in the case of a while loop, after performing the steps in the block, execution returns back to the line with the while expression, evaluating the Boolean expression again. This looping continues as long as the expression is true. When the expression is false, the block is skipped, and program execution continues with the statement following the loop's block.
  • Note that as with if, there is no semicolon following the while expression; it is the first part of a statement, completed by the block following it.
  • As indicated by the comment, this program will print the numbers from one to nine, inclusive. Let's trace through to see what will happen as the program executes.
    • At the beginning of the program i is set to 1.
    • Next, the while expression is evaluated. Since i is 1, and 1 is less than or equal to 9, the expression is true, so we enter the block of code following the while expression.
    • In the block, we print "i is now 1", then change i by taking its current value (1) and adding 1 to it, thus getting 2.
    • When we reach the end of the block (the }), we go back to the while expression at the top of the loop.
    • Now, we compare the current value of i (2) to 9. The expression is still true, so we enter the block again.
    • This time, we print "i is now 2", then change i by taking its current value (2) and adding 1 to it, thus getting 3.
    • Again, we return to the while expression. This obviously continues for several more iterations. Let's jump ahead to when i is 9 when we return to the top of the loop.
    • Since "9 <= 9" is true, we enter the block, print "i is now 9", and then change i to be 10 (current value of 9, plus 1).
    • Now when we return to the while expression, it is (finally!) false. At this point, we "exit the loop," skipping the block and going to the code immediately after it, in this case the return statement (since the comment is ignored by the compiler).
    Note that when we exit the loop, i is not 9, the last "good" value -- it is 10, the value that made the condition false.

Definition

Loop Variable
A variable that controls a loop (in this case i). It is initialized before the loop, used in the loop's Boolean expression, and changes inside the loop's block.

Changes

Make the following change to your program:

  • Add a printf statement to show the value of i after the loop is exited.

Exercises

Try the following to practice with loops. For each of these, you may find it useful to use scratch paper to organize your thoughts. As you make changes to the code, be sure to update the comments so they accurately reflect what your code is doing!

  • Show the values from 10-20, inclusive. See if you can do this two different ways (by changing the Boolean expression).
  • Show the numbers 15 down to 5, inclusive (i.e. 15, 14, 13...3, 2, 1).
  • Count to 100 by 5s.

When you are done working with this project, select File/Close Solution.

Summary

  • while loops can be used to repeat code as long as the specified condition remains true.
< Previous Next >