• While Loop

    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
      • 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

    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 is true
      • after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is false

    Syntax:

    • the keyword is while
    • the while keyword starts the while loop control statement
    • the condition follows the while keyword, like while (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

    while_counter.c:

    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 loop while (counter <= 3) if (counter == 3) value of counter at the end of the loop
      1 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 and if statements never gets executed counter never gets incremented

    Another implementation:

    while_counter_offset.c:

    • counter = 0;
    • while (counter < 3)
    • if (counter == 2)
    • Progression of counter
      loop iteration value of counter at the beginning of the loop while (counter < 3) if (counter == 2) value of counter at the end of the loop
      1 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 executed counter never gets incremented

    Things to note:

    • there's a condition to test whether things are true or false
      • a condition is an expression that simplifies to true or false
    • there's one block of code
      • the block of code is executed whenever the condition is true
      • after the block of code has finished executing, the condition is re-evaluated and the process is repeated until the condition is false
    • there's no false block of code
      • the block of code is skipped when the condition is false
    • 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
    • Use Scratch to visualize the loop

  • Mimicking the while loop

    while_mimic.c:

      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

    while_flag.c:

    Things to note:

    • you can use a float or int instead of an char; 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 floats and doubles, 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