• for Loop

    Why use for loops?:

    • more expressive, succinct, and compact when expressing loops that execute a known number of iterations
    • for loops convey more intent than while loops that execute a known number of iterations

    How to use for loops?:

    for.syntax:

      Things to note:
    • any variable used in expression1, expression2, or expression3 will need to be previously declared
    • expression1 is used to initialize some parameter, typically called an index
    • expression2 is a condition that must be true for the loop to continue execution
    • expression3 is is used to alter the value of the parameter initially assigned by expression1
  • for Loop Example

    Comparing for loop with while loop:

    This program compares using a for loop with using a while loop

    for_vs_while.c:

      Things to note:
    • both blocks of code do the same thing
    • for loop is more compact vertically
    • for loop is more concise and all relevant statements are located in one place
    • while loop is more compact horizontally
    • for loop is better for loops with a known number of iterations
    • while loop is better for loops with an unknown number of iterations
  • do while Loop

    Why use do while loops?:

    • do while loops convey more intent than while loops when the code should be executed once before the condition is checked

    How to use for loops?:

    do_while.syntax:

      Things to note:
    • the code is executed once before the condition is evaluated
  • do while Loop Examples

    Comparing do while loop with while loop:

    This program compares using a for loop with using a while loop

    do_while_vs_while.c:

      Things to note:
    • both blocks of code do the same thing
    • the code is executed once before the condition is evaluated
    • both blocks of code are nearly identical

    One Example of Where Using the do while Loop is More Natural Than Using the while Loop:

    This program compares uses a do while loop to remove an unnecessary variable initialization

    do_while_vs_while_no_initialization.c:

      Things to note:
    • you do not need to initialize the input to be some arbitrary made up value to drop into the while loop
    • other than initializing input, both blocks of code behaves the same
  • Write a for loop with multiple counters

  • Assignment

    Homework Problem 27.1, at the end of this problem you should submit to Laulima:

    • a file named UHusername_27_1.c
      • example:
      • zhaol_27_1.c
    • Write a program that allows the user to input a positive integer (with a maximum of 5 digits) or the letter 'q'. If the user inputs a positive integer then have the program count from 0 until the number entered by the user. If the user enters 'q', then the program tells the user it is quitting and quits. If the user enters anything else, then the program will tell the user to enter in a valid entry. Be sure to utilize the do while and for loops.

      Assume the user will only enter in all numbers or all characters (e.g. the user will never enter in 123abc).

      Feel free to use the atoi function: http://www.cplusplus.com/reference/cstdlib/atoi/

      For example:

      Please enter a number to count or 'q' to quit:
      [user enters 3]
      counting: 0
      counting: 1
      counting: 2
      counting: 3
      Please enter a number to count or 'q' to quit:
      [user enters 13]
      counting: 0
      counting: 1
      counting: 2
      counting: 3
      counting: 4
      counting: 5
      counting: 6
      counting: 7
      counting: 8
      counting: 9
      counting: 10
      counting: 11
      counting: 12
      counting: 13
      Please enter a number to count or 'q' to quit:
      [user enters a]
      Please enter a valid entry
      Please enter a number to count or 'q' to quit:
      [user enters q]
      Quitting...