Professional Documents
Culture Documents
03+ +What+is+a+While+Loop+ +MMB+ +v009
03+ +What+is+a+While+Loop+ +MMB+ +v009
1. A while loop is similar to a for loop but it is a little more flexible. A while loop executes a
block of code over and over again so long as condition is true and is formatted as follows:
while (condition)
{
// Do something here
// Update control variable
}
As soon as condition is false, the loop will end and move on to the next line of code.
Note, it important to include something inside of the while loop to eventually modify the
condition. Otherwise, the while loop will run forever.
2. Similar to the for loop we saw in the previous handout, below is an example of a program with a
while loop that adds the numbers 1 through 9 to variable y.
Notice how the program also includes an instruction inside of the while loop to update the
variable, x:
main()
{
char x=0; // Create variables and initialize them
char y=0; // Create variables and initialize them
3. The program begins by creating two variables, x and y and setting them equal to 0.
The loop will initially test the condition, in this case, is x less than 10? If it is true, the
program will start performing the instructions inside of the loop.
If x is NOT less than 10, the program will skip the entire loop and move on to the next
instruction.
5. The first time the program comes to the while(x<10) loop, we will have just set x to be 0.
Since 0 is less than 10, the program will proceed into the loop.
6. The first instruction inside the loop begins the summation process.
Remember, you must be doing something inside of the while loop to affect the condition.
8. Since we have completed all of the tasks inside of the curly braces, the program will then return
to the top of the loop and retest condition. Since x=1 is less than 10, the program continues
into the loop again.
The program will then add the updated value of x to y and increment x before returning to the top
of the loop to test the condition again.
9. This process of testing the condition and updating the x and y variables continues until the
value of x reaches a value of 10.
After x=10, the program will again return to the top of the loop.
This time, however, x is not less than 10. Therefore, the program will skip past the curly braces
and move on to the next instruction, while(1);.
while(1)
{
}
Where the semicolon after the statement indicates that there is nothing inside of the loop to do.
11. But, after the first while loop is over, what will the program do with the empty while(1);
loop?
Recall from our digital logic lessons, that a non-zero value is always considered to be true.
Therefore the condition (1) will always true.
12. When the program first reaches the while(1); loop, it will test the condition. Since the (1) is
true, the program will try to perform any/all of the instructions inside the loop.
Since there are no instructions inside the loop, the loop will return to the top of the while(1);
loop and retest the condition. Since (1) will always be true, the statement forms an infinite loop.
Is 1 Non-Zero?
Yes
For example, after I make a cup of coffee, I don’t want my coffee maker’s microcontroller to
stop. I want my coffee maker to be ready to make the next cup.
14. Create a new CCS project by selecting New / CCS Project from the File menu.
15. In the New CCS Project window, create a project called Loops_While.
Also, make sure you select Empty Project (with main.c) from the
Project templates and examples pane before clicking Finish.
16. Copy the program from above and paste it into the main.c file in the CCS Editor.
In the Properties window, select Optimization under Build / MSP430 Compiler and
make sure that the Optimization level is set to off.
19. Build your project. If you have any errors, make sure you did not accidentally modify your
program.
20. After successfully Building your project, launch the CCS Debugger.
21. When it is ready, your screen should look something like this. You should see both x and y in the
Variables pane, although their values may be different.
If x and y are not shown in base 10, right click on their Value column and
select Number Format / Decimal.
23. Pause when you get to this point – we are just about ready to increment x to 10.
24. Click Step Into again. x is now 10 and the program returns to the top of the first while loop
to test the condition again.
26. If you keep clicking Step Into, as expected, the program stays in the infinite loop.
27. If you want to try this again, click the Soft Reset button and continue to Step Into your
program.
x=5
y=1
Yes
Is x = 0? Go to an infinite loop
No
y=y*x
x = x-1
29. Create a new CCS Project called Factorial and see if you can write a program to implement
the flow chart.
As you work, make sure you follow all the steps we have been using to create your project and
turn off all the optimization options.
Give it a try, and let us know if you have any questions. We want you to be successful.
In no event shall the owner or contributors to the tutorials and software be liable
for any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services; loss of
use, data, or profits; or business interruption) however caused and on any theory
of liability, whether in contract, strict liability, or tort (including negligence or
otherwise) arising in any way out of the use of this software, even if advised of
the possibility of such damage.