Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

What Is a While Loop?

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

while(x<10) // Keep looping as long as x<10


{
y = y + x; // y will sum number 0-9
x = x + 1; // Update variable for condition to test
}

while(1); // Stay here when the program is done

3. The program begins by creating two variables, x and y and setting them equal to 0.

x will be the control variable that is tested in the loop’s condition.

y will be used to sum the number 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.


Copyright © 2012-2015 Page 1 of 10
Valparaiso University
4. The program then comes to the first while loop.

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.

7. After x is added to y, the program increments x.

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);.

Copyright © 2012-2015 Page 2 of 10


Valparaiso University
10. Now, let’s take a look at the last instruction. Like the for loop we saw in the previous handout,
the while(1); loop can be rewritten:

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.

Graphically, it looks like this:

Is 1 Non-Zero?

Yes

Copyright © 2012-2015 Page 3 of 10


Valparaiso University
13. It is fairly common to see infinite loops like this at the end of short microcontroller programs like
this. In most embedded systems, the microcontroller program does not end.

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.

Specify the MSP430FRxxx Family and the MSP430FR6989 microcontroller.

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.

17. Save your program, but DO NOT Build it yet.

Copyright © 2012-2015 Page 4 of 10


Valparaiso University
18. In the Project Explorer pane, right click on your project name and select Properties from
the pop-up menu.

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.

Copyright © 2012-2015 Page 5 of 10


Valparaiso University
22. Click the Step Into button and execute the program line-by-line. You can watch as the
program iterates through the while(x<10) loop and updates the x and y variables.

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.

Copyright © 2012-2015 Page 6 of 10


Valparaiso University
25. This time, however, x is NOT less than 10. Therefore, when you click Step Into again, the
program proceeds past the while(x<10) loop to the next instruction – the infinite while(1);
loop.

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.

Otherwise, click Terminate to return to the CCS Editor.

Copyright © 2012-2015 Page 7 of 10


Valparaiso University
28. Challenge time! Are you ready to try your own program? Here is a flow chart that calculates the
factorial of the number x:

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.

Copyright © 2012-2015 Page 8 of 10


Valparaiso University
30. We do not want to give you too many hints, but this is what your CCS Debugger Variables pane
should look like when your program finally reaches the infinite loop.

Give it a try, and let us know if you have any questions. We want you to be successful.

Copyright © 2012-2015 Page 9 of 10


Valparaiso University
All tutorials and software examples included herewith are intended solely for
educational purposes. The material is provided in an “as is” condition. Any
express or implied warranties, including, but not limited to the implied warranties
of merchantability and fitness for particular purposes are disclaimed.

The software examples are self-contained low-level programs that typically


demonstrate a single peripheral function or device feature in a highly concise
manner. Therefore, the code may rely on the device's power-on default register
values and settings such as the clock configuration and care must be taken when
combining code from several examples to avoid potential side effects.
Additionally, the tutorials and software examples should not be considered for use
in life support devices or systems or mission critical devices or systems.

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.

Copyright © 2012-2015 Page 10 of 10


Valparaiso University

You might also like