| < 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;
}
Explanations
- As in the
ifstatement, thewhilestatement contains a Boolean expression that determines whether the block following thewhileexpression is executed. The difference is that in the case of awhileloop, after performing the steps in the block, execution returns back to the line with thewhileexpression, 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 thewhileexpression; 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
iis set to 1. - Next, the
whileexpression is evaluated. Sinceiis 1, and 1 is less than or equal to 9, the expression is true, so we enter the block of code following thewhileexpression. - In the block, we print "
i is now 1", then changeiby 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 thewhileexpression 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 changeiby taking its current value (2) and adding 1 to it, thus getting 3. - Again, we return to the
whileexpression. This obviously continues for several more iterations. Let's jump ahead to wheniis 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 changeito be 10 (current value of 9, plus 1). - Now when we return to the
whileexpression, 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).
iis not 9, the last "good" value -- it is 10, the value that made the condition false. - At the beginning of the program
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
printfstatement to show the value ofiafter 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
whileloops can be used to repeat code as long as the specified condition remains true.
| < Previous | Next > |