What is the while
loop control statement?
The while
control statement is a group of code that tells the program to execute some code repeatedly based on a condition
Why is the while
loop control statement important?
- it allows a program to repeat without having to predetermine how many times the program needs to be repeated
- that means: it allows your program to perform repetitive tasks for an unknown number of times
- a program without looping is like a train that:
- loops between Honolulu and Waipahu a set number of times
- some passengers might be left without a train if they've missed the last train
- sometimes the train might be running with no passengers on board
- loops between Honolulu and Waipahu a set number of times
- a program with looping is like a train that:
- loops between Honolulu and Waipahu until all commuters have reached their destinations
- no passengers will be left stranded
- the train won't ever run when it's not needed
- loops between Honolulu and Waipahu until all commuters have reached their destinations
How to use the while
loop control statement?
A group of code that includes:
- a
condition
- a block of code that is executed whenever the
condition
istrue
- after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is
false
- after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is
Syntax:
- the keyword is
while
- the
while
keyword starts thewhile
loop control statement - the condition follows the
while
keyword, likewhile (condition)
- the condition is wrapped in parenthesis
(condition)
- the block of code is wrapped in curly braces, like
{ /*block of code*/ }
- read more at http://en.wikipedia.org/wiki/While_loop
While Loop Example
Be careful with the counter:
- be careful not to make an infinite loop
- if that happens:
- usually you can just hit CTRL+C, if you were working on a real terminal
- since Cloud9 is in a web browser, you would sometimes need to close the terminal tab
- if closing the terminal tab doesn't fix the infinite loop, then exit Cloud9 and re-enter
- be sure to update the code so avoid running another infinite loop again
counter = 1;
while (counter <= 3)
if (counter == 3)
- be sure the offset is correct to get the results you expect
-
Progression of counter
loop iteration value of counter
at the beginning of the loopwhile (counter <= 3)
if (counter == 3)
value of counter
at the end of the loop1 1 while (1 <= 3)
=while (true)
if (1 == 3)
=if (false)
2 2 2 while (2 <= 3)
=while (true)
if (2 == 3)
=if (false)
3 3 3 while (3 <= 3)
=while (true)
if (3 == 3)
=if (true)
4 4 4 while (4 <= 3)
=while (false)
if
condition never gets evaluated andif
statements never gets executedcounter
never gets incremented
Another implementation:
counter = 0;
while (counter < 3)
if (counter == 2)
-
Progression of counter
loop iteration value of counter
at the beginning of the loopwhile (counter < 3)
if (counter == 2)
value of counter
at the end of the loop1 0 while (0 < 3)
=while (true)
if (0 == 2)
=if (false)
1 2 1 while (1 < 3)
=while (true)
if (1 == 2)
=if (false)
2 3 2 while (2 < 3)
=while (true)
if (2 == 2)
=if (true)
3 4 3 while (3 < 3)
=while (false)
if
statement never gets executedcounter
never gets incremented
Things to note:
- there's a
condition
to test whether things aretrue
orfalse
- a
condition
is an expression that simplifies totrue
orfalse
- a
- there's one block of code
- the block of code is executed whenever the
condition
istrue
- after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is
false
- the block of code is executed whenever the
- there's no
false
block of code- the block of code is skipped when the
condition
isfalse
- the block of code is skipped when the
- control statements can be nested
- nesting control statements too deeply will make the code hard to read
- if you need to nest deeply, then consider using functions to abstract away some of the logic; we'll discuss functions and abstraction in a later lecture
- while_counter.c is utilizing the
while
loop control statement- read more at http://en.wikipedia.org/wiki/While_loop
- read the introduction
- read the Equivalent Constructs Section, if you are curious
- there's not much value in reading the Demonstrating While Loops Section
- read more at http://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue
- read the While Loop Section
- read more at http://en.wikipedia.org/wiki/While_loop
Use Scratch to visualize the loop
Mimicking the while loop
- Things to note:
- it will be hard to make the code mimic a
while
loop that iterates 100 times - it will be easier to change the code inside the
while
loop one time vs changing the mimic code multiple times
While Example Using Flag
Things to note:
- you can use a
float
orint
instead of anchar
; use whatever is most descriptive and easy to understand - the flag can be triggered based on user input or some sort of calculation or the loop number (like a counter)
- it will be hard to mimic a
while
loop that terminates based on a flag
Write a while loop to count to 100
Assignment
Homework Problem 9.1, at the end of this problem you should submit to Laulima:
- a file named UHusername_09_1.c
example:
- zhaol_09_1.c
write a program that gets a value from the user and then use a while
loop to print out the following:
- the number to the 1st power
- the number to the 2nd power
- the number to the 3rd power
- the number to the 4th power
- the number to the 5th power
- ...
- the number to the 25th power
- additional considerations:
- do not use any built-in C functions like
pow
- because a number to the 25th power can quickly make C run out of accuracy, testing your program with the 2 test cases below should be sufficient; this is an exercise on the
while
loop, not C memory management - we will be going over mathematical operators in detail later, but you can read http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B (Section Arithmetic Operators) and see if there's anything you can use for this assignment
- for decimal numbers, this class will only be using
float
s anddouble
s, but do take note of the precision inaccuracies at the higher exponential calculations - test case 1:
Please enter your number: [user enters 1.1] 1.100000 1.210000 1.331000 1.464100 1.610510 1.771561 1.948717 2.143589 2.357948 2.593742 2.853117 3.138428 3.452271 3.797498 4.177248 4.594973 5.054470 5.559917 6.115909 6.727500 7.400250 8.140275 8.954302 9.849733 10.834706
- test case 2:
Please enter your number: [user enters 2.2] 2.200000 4.840000 10.648000 23.425600 51.536320 113.379904 249.435789 548.758735 1207.269218 2655.992279 5843.183014 12855.002631 28281.005788 62218.212734 136880.068015 301136.149634 662499.529195 1457498.964228 3206497.721302 7054294.986864 15519448.971101 34142787.736422 75114133.020128 165251092.644283 363552403.817422