C++/Lecture 5 – Decision Making
if statement

#include <iostream>
using namespace std;

int main() {
    if (20 > 18) {
        cout << "20 is greater than 18";
    }
    return 0;
}

if-else – compare two numbers

#include <iostream>
using namespace std;

int main() {
    int x, y;
    cout << "Enter two numbers: ";
    cin >> x >> y;

    if (x > y) {
        cout << "x is greater than y";
    } else {
        cout << "y is greater than or equal to x";
    }
    return 0;
}

if-else – time message

#include <iostream>
using namespace std;

int main() {
    int time;
    cout << "Enter time: ";
    cin >> time;

    if (time < 18) {
        cout << "Good day.";
    } else {
        cout << "Good evening.";
    }
    return 0;
}

else-if – time message

#include <iostream>
using namespace std;

int main() {
    int time;
    cout << "Enter time: ";
    cin >> time;

    if (time < 10) {
        cout << "Good morning.";
    } else if (time < 20) {
        cout << "Good day.";
    } else {
        cout << "Good evening.";
    }
    return 0;
}

Ternary operator

#include <iostream>
using namespace std;

int main() {
    int time;
    cout << "Enter time: ";
    cin >> time;

    string result = (time < 18) ? "Good day." : "Good evening.";
    cout << result;
    return 0;
}

switch – weekday name

#include <iostream>
using namespace std;

int main() {
    int day;
    cout << "Enter weekday number (1-7): ";
    cin >> day;

    switch (day) {
        case 1: cout << "Monday"; break;
        case 2: cout << "Tuesday"; break;
        case 3: cout << "Wednesday"; break;
        case 4: cout << "Thursday"; break;
        case 5: cout << "Friday"; break;
        case 6: cout << "Saturday"; break;
        case 7: cout << "Sunday"; break;
        default: cout << "Invalid day";
    }
    return 0;
}

Real-life example 1 – door code

#include <iostream>
using namespace std;

int main() {
    int code;
    cout << "Enter door code: ";
    cin >> code;

    if (code == 1337) {
        cout << "Correct code.";
    } else {
        cout << "Incorrect code.";
    }
    return 0;
}

Real-life example 2 – positive or negative

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter a number: ";
    cin >> number;

    if (number > 0) {
        cout << "The value is a positive number.";
    } else if (number < 0) {
        cout << "The value is a negative number.";
    } else {
        cout << "The number is zero.";
    }
    return 0;
}

Real-life example 3 – even or odd

#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter your number: ";
    cin >> number;

    if (number % 2 == 0) {
        cout << number << " is even.";
    } else {
        cout << number << " is odd.";
    }
    return 0;
}

Real-life example 4 – number to word

#include <iostream>
using namespace std;

int main() {
    int num;
    cout << "Enter a number between 1 and 3: ";
    cin >> num;

    switch (num) {
        case 1: cout << "One\n"; break;
        case 2: cout << "Two\n"; break;
        case 3: cout << "Three\n"; break;
        default: cout << "Invalid\n";
    }
    return 0;
}
Ready to practice this lecture?