While Loops - a Review

This short review of while loops, with a start on loops in general, is intended as a review aid for students. There are several easy review questions at the end.

C SharpProgrammingLoopswhile loops
1.0x

While Loops - a Review

Created 2 years ago

Duration 0:06:35
lesson view count 26
This short review of while loops, with a start on loops in general, is intended as a review aid for students. There are several easy review questions at the end.
Select the file type you wish to download
Slide Content
  1. While Loops – C#

    Slide 1 - While 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. While Loops

    Slide 6 - While Loops

    • The while loop repeats a sequence of statements as long as a certain condition is true.
  7. Pretest Loop (While)

    Slide 7 - Pretest Loop (While)

    • 7
    • while ((!cmd.Equals("quit"))
    • {
    • .
    • .
    • .
    • }
    • If the
    • condition
    • is true,
    • these
    • statements
    • are executed.
    • Condition is tested at the
    • top of the loop
  8. Sample While Loop

    Slide 8 - Sample While Loop

    • private void btnDisplay_Click(object sender, EventArgs e)
    • {
    • // Display the numbers from 1 to 7
    • int num = 1;
    • while (num <= 7)
    • {
    • MessageBox.Show(num.ToString());
    • num = num + 1;
    • }
    • Change
    • Values
    • Initialize
    • Check for
    • completion
  9. Menu While Loop

    Slide 9 - Menu While Loop

    • string cmd;
    • int count = 0;
    • cmd = System.Console.ReadLine();
    • while (!cmd.Equals("quit"))
    • {
    • count = count + 1;
    • cmd = System.Console.ReadLine();
    • }
    • Change
    • Value
    • Initialize
    • Check for
    • completion
  10. Checking At Bottom

    Slide 10 - Checking At Bottom

    • Start with do keyword
    • Comparison at the end of the loop ends with a semi-colon
    • Body of the loop is always executed at least once!
  11. Checking at Bottom of Loop

    Slide 11 - Checking at Bottom of Loop

    • int i = 0;
    • do
    • {
    • listBox1.Items.Add(i.ToString());
    • i++;
    • } while (i < 10);
    • Change
    • Value
    • Initialize
    • Check for
    • completion
  12. Comments

    Slide 12 - Comments

    • Be careful to avoid infinite loops – loops that never end.
    • When an infinite loop is executing, the program can be terminated by clicking on the Stop Debugging button on the Toolbar
    • ( or Shift+F5) or from the debugging menu