while (condition)
{ |
while line, it
tests the condition.true:while line
and do the same thing all over againfalse:} true.#include<iostream> |
| How many greetings do you want? 3 Hello Hello Hello That's all! |
| How many greetings do you want? 0 That's all! |
do { |
while line. It tests the condition.true,
it performs the statements
againfalse,
it goes to the next line.#include<iostream> |
| How many greetings do you want? 3 Hello Hello Hello That's all! |
How many greetings do you want? 0 |
| /* 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; int 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'); return 0; } |
| 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 |
age=21 instead of age==21#include<iostream> |
| How old are you? 20 You are 21. |
bool can be
represented by an integerage=21 is an arithmetic expression. It returns the
value of right hand side (i.e. 21).true== in
conditions! (unless you know exactly what you are doing)