← C++ tutorial

C++

Conditionals

if, else if, and else behave the same as in Java or JavaScript.

Example: a time-of-day greeting

#include <iostream>
using namespace std;

int main() {
    int hour = 14;
    string greeting;

    if (hour < 12) {
        greeting = "Good morning";
    } else if (hour < 18) {
        greeting = "Good afternoon";
    } else {
        greeting = "Good evening";
    }

    cout << greeting << endl;
    return 0;
}
Good afternoon

If you've already read the Java tutorial's conditionals lesson, this should look almost identical — that's exactly the point of a shared "C-family" syntax: once you know the pattern in one of these languages, recognizing it in another is mostly about spotting small syntax differences, not relearning the underlying logic from scratch.

Example
#include <iostream>
using namespace std;

int main() {
    int hour = 14;
    string greeting;

    if (hour < 12) {
        greeting = "Good morning";
    } else if (hour < 18) {
        greeting = "Good afternoon";
    } else {
        greeting = "Good evening";
    }

    cout << greeting << endl;
    return 0;
}
Output
Good afternoon