Conditions
C++ conditional statements work exactly like C's, with the bonus that conditions now naturally evaluate to a real bool instead of just an integer convention.
💡 Key insight
Because C++ has real booleans, comparison results read more clearly in code and in the debugger.
Syntax
syntax
if (condition) {
// code
} else if (condition2) {
// code
} else {
// code
}Example
Only the first matching branch runs; the rest are skipped.
cpp
#include <iostream>
using namespace std;
int main() {
int marks = 72;
if (marks >= 90) {
cout << "Grade: A" << endl;
} else if (marks >= 60) {
cout << "Grade: B" << endl;
} else {
cout << "Grade: C" << endl;
}
return 0;
}outputGrade: B
Try it yourself
Copy this example into the C++ compiler and run it — try changing a value and see what happens.
