Lecture 06: Loops

Jan 29, 2024  β”‚  Last updated Jan 26, 2024 by Charlotte Curtis

HTML Slides html β”‚ PDF Slides PDF

Where we left off

  • Boolean expressions
  • if-else statements
  • Some C++ specific boolean behaviour
  • All the git lab chaos

Textbook Sections 2.4, 3.1-3.2

if (x % 3 == 0)
    cout << "Fizz";
if (x % 5 == 0)
    cout << "Buzz";

Today’s topics

Textbook Sections 3.3-3.4

Review: Loop design decisions

Think about:

  1. What statements do you want to repeat?
  2. What variable (the LCV) should control the loop?
  3. What condition should cause the loop to terminate? Then, invert it
  4. What should the initial conditions of the loop be?
  5. How should the LCV be updated?

Complete while loop example

int x = 1;              // Initialization
while (x <= 100) {      // Condition
    if (x % 3 == 0)
        cout << "Fizz";
    if (x % 5 == 0)
        cout << "Buzz";
    cout << "\n";
    x++;                // Update
}

for loops - a bit more different

for i in range(10):
    # code to execute
for (int i = 0; i < 10; i++) {
    // code to execute
}

FizzBuzz as a for loop

Since FizzBuzz is counting from 1 to 100, it’s a good candidate for a for loop:

for (int x = 1; x <= 100; x++) {
    if (x % 3 == 0)
        cout << "Fizz";
    if (x % 5 == 0)
        cout << "Buzz";
    cout << "\n";
}

emoji Review: Compound conditions

Say you want to roll a pair of dice until you get a 12 OR you reach 5 rolls. Which of the following is the correct condition?

  1. roll != 12 || n_rolls < 5
  2. roll != 12 && n_rolls < 5
  3. roll == 12 || n_rolls >= 5
  4. roll == 12 && n_rolls >= 5
  5. roll == 12 || n_rolls < 5
int roll = roll_dice();
int n_rolls = 1;

while (<condition>) {
    roll = roll_dice();
    n_rolls++;
}

De Morgan’s Laws

while loops vs for loops

for loop conventions

You really really should stick to the syntax of:

for (initialization; condition; update) {
    // loop body
}

C++ is highly flexible, and that power means it’s your job to understand exactly what you want to have happen.

Why while?

If for loops are just syntactic sugar for while loops, why do we have both?

Recall: Sentinel loops

emoji Remember this pattern?

The loop we just wrote is an example of a sentinel, but it’s also an example of which common loop pattern?

  1. Counted loop
  2. Accumulator
  3. Summation
  4. Variable-controlled loop
  5. Fruit loop

End of input: a useful sentinel

Example using eof()

Modify the vowel-counting program to use eof() instead of a period as a sentinel.

flavour center h:350

More eof() considerations

Alternatives to eof()

Controversial loop topics

Re-writing a loop with break

This is an example of actual code I’ve had submitted for assignments, often with a ChatGPT attribution:

int x, y;
while (true) {
    cin >> x >> y;
    if (eof())
        break;
    cout << x + y << endl; // actually more complex, but you get the idea
}

How would you re-write this loop without using break?

Summary of loop types

Getting fancy: nested loops

Challenge: write a function that takes an integer n and displays the times table up to $n \times n$

Arrays preview

C-style arrays

Working with arrays

Coming up next

Textbook Chapter 7



Previous: Lecture 05: Booleans and Decisions
Next: Lecture 07: Arrays