While loops

Syntax for a while loop:

while ( someBooleanExpressionHere) {
    // Put code that you want to execute in here
}

What this does:

  1. Check the Boolean Expression. If it's true, go to step (2). If false, quit looping and go on to the next part of the program.
  2. Execute the code, then go to step (1).

Examples:

// Example 1
#include<iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 0;
    
    while (b < a) {
        b = b+1;
    }

    cout << b << endl;
    return 0;
}
// What does this code do?


// Example 2
#include<iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 0;
    
    while (a < b) {
        b = b+1;
    }

    cout << b << endl;
    return 0;
}


// Example 3

#include<iostream>
using namespace std;

int main() {

    int a = 5;
    int b = 0;
    
    while (b < a) {
        a = a+1;
    }

    cout << b << endl;
    return 0;
}

Some Practice Problems:

Multiplication the old-fashioned way

Input two integers from the user. Call them a and b. For simplicity, assume that both are bigger than zero. Calculate a*b by adding a+a+a+...+a.

An addition problem

Input an integer from the user. Call it n. Find the sum 1+2+3+...+n. (There is a nice math formula for this, but we're not going to use it. Instead we'll actually add each number).

An edited addition problem

Write a program that allows the user to enter number (via cin) one at a time. After each number, ask if they would like to input another number. If they say no, then stop asking for new numbers and output the sum of all the numbers so far.

Example Input/Output:
  
Please input a number: 11.3
Would you like to input another number (y/n)?: y
Please input a number: 9231.32
Would you like to input another number (y/n)?: y
Please input a number: -23
Would you like to input another number (y/n)?: y
Please input a number: 91.32
Would you like to input another number (y/n)?: n
The sum of all your numbers is: 9310.94.
addLoop2.cpp

A longer problem: Find initials

Input a string from the user (using getline), and print to the screen the first letter of each word. For example, if I input my name:

Geoffrey Sankar Iyer

The program should output my initials

GSI
initials.cpp

For Loops

A for loop is a slightly different kind of loop. It is generally more useful when you know how many times you want to go through a loop.

Syntax:

for ( initialState; booleanExpression; thingToDoWhenLooping) {
    Code to execute here.
}

Example


#include<iostream>
using namespace std;

int main() {
    for (int i=0; i<6; i++)
        cout << i;
    cout << endl;
    return 0;
}

/* What this does:
   (1) At the beginning of the loops, we initialize a variable i=0;
   (2) Check that i<6 (if true, execute code. If false, loop is finished)
   (3) Execute code  (cout << i;)
   (4) Run the line i++, then go to step (2)
*/

More Practice Problems

Exercise 1

Write a program that inputs an integer from the user (via cin), and outputs (via cout) that many stars. Example input/output:

How many stars would you like to print? 10
Okay here you go!
**********

Exercise 2

Extend your program from the previous exercise so that it now prints a square of stars. Example input/output:

How big of a square do you want? 10
Alright here it is!
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Exercise 3

Write a program that inputs a string from the user (use getline), then goes through the string one character at a time and changes every 'a' to a '!'. Example input/output:

Please input a string: The rain in Spain stays mainly on the plain.
Here is your edited string:
The r!in in Sp!in st!ys m!inly on the pl!in

What would you do if I also wanted to change every 'A' to a '!'?

Also, completely unrelated, this movie stinks.

Exercise 4

Write a program that inputs a string from the user and removes all space-characters from the string. After you're done removing the spaces, output the result to the console.


Example input/output:
Please input a string: Frankly my dear, I don't give a damn.
Output: FranklymydearIdon'tgiveadamn.