Math2220 Lect5

while loop

while (condition) {
     statement1;
     statement2;
     .............
     statement_last;
}

How it works?

Example

#include<iostream>
using namespace std;
void main() {
    int n=1;
    while (n < 10) {
        cout << n << " is strictly smaller than 10.\n";
        n++;
    }

    char line[100];
    cin.getline(line,100);
}

Sample output

1 is strictly smaller than 10.
2 is strictly smaller than 10.
3 is strictly smaller than 10.
4 is strictly smaller than 10.
5 is strictly smaller than 10.
6 is strictly smaller than 10.
7 is strictly smaller than 10.
8 is strictly smaller than 10.
9 is strictly smaller than 10.

Example

#include<iostream>
using namespace std;

void main() {
    int countDown;

    cout << "How many greetings do you want? ";
    cin >> countDown;
    
    
   while(countDown > 0) {
        cout << "Hello ";
        countDown--;
    }
    

    cout << endl;
    cout << "That's all!\n";

    char line[100];
    cin.getline(line,100);
        cin.getline(line,100);
}

Sample output

How many greetings do you want? 3
Hello Hello Hello
That's all!

How many greetings do you want? 0

That's all!

Example

#include<iostream>
using namespace std;
void main() {
    char ch;
    int counter=0;
    cout << "Please enter a character (enter Q or q to quit): ";
    cin >> ch;
    counter++;
    while((ch!='Q') && (ch!='q'))
    {
        cout << "Please enter a character (enter Q or q to quit): ";
        cin >> ch;
        counter++;
    }
    cout << "Your entered " << counter << " characters.\n";


    char line[100];
    cin.getline(line,100);
    cin.getline(line,100);
}

Sample output

Please enter a character (enter Q or q to quit): A
Please enter a character (enter Q or q to quit): B
Please enter a character (enter Q or q to quit): C
Please enter a character (enter Q or q to quit): d
Please enter a character (enter Q or q to quit): e
Please enter a character (enter Q or q to quit): k
Please enter a character (enter Q or q to quit): l
Please enter a character (enter Q or q to quit): q
Your entered 8 characters.

do-while loop

do {
    statement1;
    statement2;
    ..........
    statement_last;
} while (condition);

Don't forget the last semicolon!!!

How it works?

Example

#include<iostream>
using namespace std;

void main() {
    int countDown;

    cout << "How many greetings do you want? ";
    cin >> countDown;
    do {
        cout << "Hello ";
        countDown--;
     } while(countDown > 0);
     // Don't forget the last semicolon ; 

    cout << endl;
    cout << "That's all!\n";

    char line[100];
    cin.getline(line,100);
    cin.getline(line,100);
}

Sample output

How many greetings do you want? 3
Hello Hello Hello
That's all!

How many greetings do you want? 0
Hello
That's all!

Example

#include<iostream>
using namespace std;
void main() {
    char ch;
    int counter=0;

    do {
        cout << "Please enter a character (enter Q or q to quit): ";
        cin >> ch;
        counter++;
    }while((ch!='Q') && (ch!='q')); 
    // Don't forget the last semicolon ; 

    cout << "Your entered " << counter << " characters.\n";

    char line[100];
    cin.getline(line,100);
    cin.getline(line,100);
}

Sample output

Please enter a character (enter Q or q to quit): A
Please enter a character (enter Q or q to quit): B
Please enter a character (enter Q or q to quit): C
Please enter a character (enter Q or q to quit): d
Please enter a character (enter Q or q to quit): e
Please enter a character (enter Q or q to quit): k
Please enter a character (enter Q or q to quit): l
Please enter a character (enter Q or q to quit): q
Your entered 8 characters.

Example

/*
Enter your score (within 0 to 100).
The program will find out your grade.
A: 90-100
B: 80-90
C: 65-80
D: 50-65
*/
#include<iostream>
using namespace std;

void  main() {
    double score;
    char again; // y or n
       do {
        cout << "Please enter your score: ";
        cin >> score;
        if(score> 100 || score < 0) {
            cout << "Invalid score.\n";
            cout << "Please check the score again.\n";
        } else if (score >= 90) {
            cout << "Your grade is A.\n";
        } else if (score >= 80) {
            cout << "Your grade is B.\n";
        } else if (score >= 65) {
            cout << "Your grade is C.\n";
        } else if(score >= 50) {
            cout << "Your grade is D.\n";
        } else {
            cout << "You fail!\n";
        }
        cout << "Continue(y/n)? ";
        cin >> again;
    } while (again=='y' || again=='Y');

    char line[100];
    cin.getline(line,100);
    cin.getline(line,100);
}

Sample output

Please enter your score: 53
Your grade is D.
Continue(y/n)? y
Please enter your score: 78
Your grade is C.
Continue(y/n)? y
Please enter your score: 120
Invalid score.
Please check the score again.
Continue(y/n)? y
Please enter your score: 92
Your grade is A.
Continue(y/n)?n

Difference between while and do-while

Pitfall : Using = in place of ==

Question:

What is the output of the following program if I enter 20 ?
Notice that I use age=21 instead of age==21

Example

#include<iostream>
using namespace std;

void main() {
    int age;
    cout << "How old are you? ";
    cin >> age;
    if(age=21) {
        cout << "You are 21.\n";
    } else {
        cout << "You are not 21.\n";
    }

    char line[100];
    cin.getline(line,100); 
    cin.getline(line,100);
}

Sample output

How old are you? 20
You are 21.

What's wrong?