C++ Loops

CS1 Lecture 3(?)

Cameron Rutherford

Thursday, the 1th of August, 2024

Intro

Covered so far

  • Hello world! Building and running from the command line
  • Vi / Emacs basics, and familiarity with your IDE of choice
  • Some other basic C++ I/O techniques (std::cout and std::cin)
  • Intro to what C++ standards are
  • Control flow and basic conditionals

Covering today

  • Loops
    • While
    • Do-While
    • For
    • Range-based for loop (C++11)
  • Examples of each
  • Debugging

What’s next?

  • Math Libraries
  • Data Structures
  • Pointers
  • Object Oriented Programming

While Loops

Motivation

Say we have our basic hello world program (compiled with gcc main.cpp -lstdc++ -o a.out):

#include <iostream>

int main() {
  std::cout << "Hello World!\n";
  return 0;
}

What if we want to print Hello World!:

  • 5 times?
  • 10 times?
  • A variable amount of times (read through standard input)

Copy and Paste?

#include <iostream>

int main() {
  std::cout << "Hello World!\n";
  std::cout << "Hello World!\n";
  std::cout << "Hello World!\n";
  // ...
  std::cout << "Hello World!\n";
  std::cout << "Hello World!\n";
  return 0;
}

What’s wrong with this approach?

  1. Hard to read!
  2. Labor intensive
  3. Hard to maintain

First while loop

Lets take a look as a while loop that prints Hello World 5 times:

#include <iostream>

int main() {
  int n = 5;
  while (n > 0) // <-- While loop condition -->
  {
    // <-- While loop statement
    std::cout << "Hello World!\n";
    n--;
    // -->
  }
  return 0;
}

While control flow

This while loop is equivalent to…

#include <iostream>

int main() {
  int n = 5;
start: // goto label
  {
    if (n > 0) // <-- While loop condition -->
    {
      // <-- While loop statement
      std::cout << "Hello World!\n";
      n--;
      // -->
      goto start;
    }
  }
  return 0;
}

Notice that this is:

  • Arguably, harder to read than a while loop
  • Contains a goto statement - these should be used sparingly
  • Scope of variables is the same

Infinite Loops!?

#include <iostream>

int main() {
  while(true)
  {
    std::cout << "Hello World!\n";
  }
  return 0;
}
  • What do you expect the output of this program to be?
  • How do you terminate a stray running program? (CTRL + C)
  • Are there other ways that we can control loop execution?

To Continue or To Break, that is the question

  • Say we want to print the numbers 1-10, but we don’t want to print 4!
  • Lets also do this without a condition for the while loop
#include <iostream>

int main() {
  int n = 0;
  while(true)
  {
    n++;
    if (n == 4)
    {
      continue; // <-- skip back to the start of the loop
    }
    std::cout << "Counting..." << n << "\n";
    if (n == 10)
    {
      break; // <-- exit the loop, resume control flow
    }
  }
  return 0;
}

Continue / break is not unique to this style of loop, and is applicable to all loops.

Do-While Loops

Lets do a side-by-side conparison of the while and do-while loop styles:

#include <iostream>

int main() {
  int n = 5;
  do
  {
    // <-- Do While statement
    std::cout << "Hello World!\n";
    n--;
    // -->
  }
  while (n > 0); // <-- Do While loop conditional -->
  return 0;
}
#include <iostream>

int main() {
  int n = 5;
  while (n > 0) // <-- While loop condition -->
  {
    // <-- While loop statement
    std::cout << "Hello World!\n";
    n--;
    // -->
  }
  return 0;
}

Both programs produce the same output, and use roughly the same lines of code.

Do-While use cases

From this SO post:

I’ve been programming for a while now (2 years work + 4.5 years degree + 1 year pre-college), and I’ve never used a do-while loop short of being forced to in the Introduction to Programming course. I have a growing feeling that I’m doing programming wrong if I never run into something so fundamental.

So, when should we use a do-while loop?

  • When you want to try something once, and then retry until it succeeds / you give up
  • Display / do something while waiting for an exit signal (embedded systems / applications)
  • If the code should always execute at least once
  • Your initial conditions would cause a while loop to terminate

Do-While control flow

For Loops

Here is our same “Hello World” program using a traditional C++ for loop:

#include <iostream>

int main() {
  for (int i = 0; i < 5; i++)
  {
    std::cout << "Hello World!\n";
  }
  return 0;
}

Notice the different components separated via ;: - Init-statement - Condition - Expression - Loop body / statement

You can include multiple / zero expressions within each component. Expressions are separated via ,

Could this be a while loop?

#include <iostream>

int main() {
  for (int i = 0; i < 5; i++)
  {
    std::cout << "Hello World!\n";
  }
  return 0;
}
#include <iostream>

int main() {
  int n = 5;
  while (n > 0) // <-- While loop condition -->
  {
    // <-- While loop statement
    std::cout << "Hello World!\n";
    n--;
    // -->
  }
  return 0;
}
  • What are the key differences between these two approaches?
  • Do you think that one is more / less readable than the other?
  • What do you prefer?

For loops can be infinite as well!

#include <iostream>

int main() {
  for (;;)
    std::cout << "Hello World!\n";
  return 0;
}
  • This is perhaps the most space efficient way to write an infinite loop
  • I call this a “spider” inifinite loop because (;;) kind of looks like a spider…

Technically Hello World

#include <iostream>

int main() {
  for (int i = 0; i < 5 ; i++, std::cout << "Hello World!\n");
  return 0;
}

Range-based Loops

TODO

Debugging

TODO

Questions?