For Loops - Brief Review

This is intended as a brief review of how a for loop works in C#. There are a few easy review questions at the end.

LoopsprogrammingC Sharp
1.0x

For Loops - Brief Review

Created 2 years ago

Duration 0:04:36
lesson view count 34
This is intended as a brief review of how a for loop works in C#. There are a few easy review questions at the end.
Select the file type you wish to download
Slide Content
  1. For Loops - C#

    Slide 1 - For Loops - C#

  2. Three Essential Parts

    Slide 2 - Three Essential Parts

    • Set up control conditions
    • Check control conditions for completion
    • Change control values
    • All loops requires these steps!
  3. Set Control Conditions

    Slide 3 - Set Control Conditions

    • What are the initial conditions?
    • Set initial value(s) for loop control variables
    • Make sure everything is at a known state
  4. Check Control Conditions

    Slide 4 - Check Control Conditions

    • Are we done yet?
    • Is the counter at maximum value?
    • Is the “completed flag” set?
  5. Change control values

    Slide 5 - Change control values

    • Avoiding the dreaded infinite loop!
    • Get new input from user or other source
    • Other changes depending on type of loop
    • Increment (decrement) counter
  6. For Loop

    Slide 6 - For Loop

    • A counter controlled loop
    • Used when we know how many times we want the loop to execute
    • An alternative to the while Loop
  7. For Loop Example

    Slide 7 - For Loop Example

    • Built-in
    • Counter
    • Start
    • Value
    • End
    • Value
    • for (int i = m; i < n; i++)
    • {
    • statement(s)
    • }
    • All three steps in a single line
    • Change
    • Value
  8. Details

    Slide 8 - Details

    • for (int i = 1; I < 5; i++)
    • {
    • MessageBox.Show(i.ToString());
    • }
    • “i” is initialized to 1
    • “i” is tested against the
    • stop value 5
    • Increment i
  9. Altering the Counter Variable

    Slide 9 - Altering the Counter Variable

    • The value of the counter variable should not be altered within the body of a for loop.
    • Doing so might cause the loop to repeat indefinitely or have an unpredictable number of repetitions.
    • So Don’t do it!
    • 9
  10. Let’s See What You’ve Learned

    Slide 10 - Let’s See What You’ve Learned