bool x = false, y = true;cout << sizeof(bool) << endl; // 1cout << x << " " << y << endl; // 0 1cout << x + 6 << " " << y + 3.14 << endl; // 6 4.14bool x = 0, y = 3.14, z = 0x1100;cout << x << " " << y << " " << z << endl; // 0 1 1
Comparative Operators
Logical Operators
Short-Circuit Evaluation
int x = 0;bool b = (x == 0 || ++x == 1); // ++x will not be executedcout << b << endl;x = 0;b = (x != 0 && ++x == 1); // ++x will not be executedcout << b << endl;
if Statement
int mark;cout << "What is your exam mark?\\n"; cin >> mark;if (mark >= 30) { cout << "You passed the exam of CS2311!\\n"; cout << โCongratulations!\\nโ;} else cout << โYou failed CS2311 ... \\nโ; // will only be executed in else cout << โYou need to retake CS2311.\\nโ; // will be executed everytime
Switch Statement
int day;cin >> day;switch (day) { // if day is 6 case 1: cout << "Mon" << endl; case 2: cout << "Tue" << endl; case 3: cout << "Wed" << endl; case 4: cout << "Thur" << endl; case 5: cout << "Fri" << endl; case 6: cout << "Sat" << endl; case 7: cout << "Sun" << endl; default: cout << "Invalid" << endl;}// output will be// Sat// Sun// Invalid