# Math2220 Lect5

## while loop

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

### How it works?

• When the computer encounters the `while` line, it tests the condition.
• If the condition is `true`:
• statement1 to statement_last will be performed.
• after all the statements are performed, the computer goes back the the `while` line and do the same thing all over again
• if the condition is `false`:
• computer skips all the statements
• it goes to the line immediately after the close brace `}`
• i.e. statements will be performed as long as the condition is `true`.
• if there is only one statement in the loop, the braces can be omited.

Example

 ```#include 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 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 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?

• the computer performs all the statements first.
• when it reaches the `while` line. It tests the condition.
• if the condition is `true`, it performs the statements again
• if the condition is `false`, it goes to the next line.
• if there is only one statement in the loop, the braces can be omited.

Example

 ```#include 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 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 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

• in while loop, the condition is checked before the loop is executed.
• in do-while loop, the loop body is at least executed once.

## 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 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?

• there is no compiling error because a `bool` can be represented by an integer
• `age=21` is an arithmetic expression. It returns the value of right hand side (i.e. `21`).
• a non zero number means `true`
• be very careful! don't forget to use `==` in conditions! (unless you know exactly what you are doing)