Todayโ€™s Outline


  • Logical data type, operators, and expressions
  • If statement
    • Simple
    • Nested
  • Switch statement

Logical Data Type: bool


bool x = false, y = true;
cout << sizeof(bool) << endl;              // 1
cout << x << " " << y << endl;             // 0 1
cout << x + 6 << " " << y + 3.14 << endl;  // 6 4.14
 
bool 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 executed
cout << b << endl;
 
x = 0;
b = (x != 0 && ++x == 1);     // ++x will not be executed
cout << 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