For loops continued

General form of for-loop:

for (init-statement; check; step-statement)
{
  statement;
  statement;
  ...
}
Show the flowchart: first init-statement executes, followed by check, and so on. If the check is false at the first entry to the loop, the body is never executed. The init-statement or the step-statement could be empty. Also, point out that the init-statement could be anything, e.g., declaration, assignment, function call, another for-loop, etc. Same for step-statement.

Why do we need the for-loop when we already have the while loop? More structure, better readability. e.g., if we just want to count, for loop has much better readability than the while loop.

Example: add up the first 100 numbers

int sum = 0;
for (int i = 0; i < 100; i++)
{
  sum = sum + i;
}
cout << "The sum is " << sum << endl;
Show the correspondence of the for-loop with the while loop.

sum = sum + i can also be written as sum += i.

While without braces:

while (condition)
  statement;
If only one statement in body, can omit left and right braces (just like if statement). Similarly for for loop.

Example: countdown using the for loop

for (int i = 10; i >= 1; i--)
{
  cout << i << endl;
}
cout << "Blast off!" << endl;
Output:
10
9
8
7
6
5
4
3
2
1
Blast off!

Discuss that any (or all) of init-stmt, check, or step-stmt could be empty.

Introducing the break statement: You can use a special statement called break;. It simply breaks out of the loop at that point and transfers control to the first statement following the end of the for the block.

Example:

#include <iostream>
#include "simpio.h"
using namespace std;
int main() {
  int sum = 0; //sum must be declared outside of the loop! Otherwise it will be redeclared many times
  int num = getInteger("Enter a number: "); //num must be declared outside of the loop! Otherwise, the loop condition makes no sense.
  while (true)
  {
    sum += num;
    num = getInteger("Enter a number: ");
    if (num == -1)
      break;
  }
  cout << "Sum is " << sum << endl;
  return 0;
}

Nested loops

A loop placed inside another loop
for (int i = 0; i < 5; i++)
{
  for (int j = 0; j < 10; j++)
  {
    cout << "*";
  }
  cout << endl;
}
Could we have ommitted the braces in the for loop here?

Output:

**********
**********
**********
**********
**********
The outer loop repeats five times; the inner loop repeats ten times

Exercise: What is produced by the following code

for (int i = 0; i < 5; i++)
{
  for (int j = 0; j < i + 1; j++)
  {
    cout << "*";
  }
  cout << endl;
}

Write programs to print the following patterns

Pattern 1

*******
******
*****
****
***
**
*

Pattern 2

*
**
***
****
*****
******
*******

Pattern 3

1
22
333
4444
55555
666666
7777777

Pattern 4

......1
.....22
....333
...4444
..55555
.666666
7777777

Pattern 5

......1
.....21
....321
...4321
..54321
.654321
7654321

Methods (or functions)

We can make new commands (or methods). This lets us decompose our program into smaller pieces that are easier to understand.
type name()
{
  statement1;
  statement2;
  ...
}

Example:

int printGreeting()
{
  cout << "Hello, world!" << endl;
  cout << "I hope you had a great day.";
  return 0;
}

int main()
{
  string name = readLine("Next name? ");
  while (name != "")
  {
    printGreeting();
    name = readLine("Next name? ");
  }
  return 0;
}
Introduce the void type. Replace return type of printGreeting with void.