C++ Tutorial
C++ If Else
if runs a block when a condition is true. else and else if cover the other paths.
if runs a block
Write if, a condition in parentheses, then a brace-wrapped block. If the condition is true, the block runs. If it is false, C++ skips the block and continues after the closing brace.
Always use braces, even for one statement. A later edit that adds a second line without braces will not do what it looks like. This tutorial never omits them.
Example
#include <iostream>
using namespace std;
int main() {
int score = 72;
if (score >= 50) {
cout << "pass" << endl;
}
return 0;
}Change score and compile in /cpp/try. At 49 the program prints nothing. That is a reason to add else.
else covers the rest
Pair else with an if when you need exactly one of two paths. One block runs. The other does not. There is no third option in a plain if/else.
Example
#include <iostream>
using namespace std;
int main() {
int score = 41;
if (score >= 50) {
cout << "pass" << endl;
} else {
cout << "fail" << endl;
}
return 0;
}else if chains more tests
else if means: the previous tests were false, so try this one. C++ walks the chain from the top and runs the first block whose condition is true. A final else catches everything that did not match.
Order matters. Put the strictest test first. If you check score >= 50 beforescore >= 90, a 95 never reaches the A branch.
Example
#include <iostream>
using namespace std;
int main() {
int score = 82;
if (score >= 90) {
cout << "A" << endl;
} else if (score >= 80) {
cout << "B" << endl;
} else if (score >= 70) {
cout << "C" << endl;
} else {
cout << "below C" << endl;
}
return 0;
}Nested if
An if inside another if is a nested if. Use it when the second question only makes sense after the first one is true. Indent the inner block so a person can see the shape.
Example
#include <iostream>
using namespace std;
int main() {
int age = 20;
bool hasTicket = true;
if (age >= 18) {
if (hasTicket) {
cout << "enter" << endl;
} else {
cout << "buy a ticket" << endl;
}
} else {
cout << "too young" << endl;
}
return 0;
}Two independent facts can often be one condition: if (age >= 18 && hasTicket). Nest when the inner else needs a different message.
Conditions you will write
| Need | Write |
|---|---|
| Equal | if (a == b) { ... } |
| Not equal | if (a != b) { ... } |
| Both true | if (a && b) { ... } |
| Either true | if (a || b) { ... } |
Do not write if (x = 1). That assigns 1 to x and then treats it as true. Compare with ==.
Braces are not optional here
C++ lets you skip braces when the body is one statement. Skip them and a later line that looks nested is actually outside the if. Always open and close { }. Match them. Indent the body.
There is no elif keyword. The spelling is two words: else if.
Example
#include <iostream>
using namespace std;
int main() {
int hour = 14;
if (hour < 12) {
cout << "morning" << endl;
} else if (hour < 18) {
cout << "afternoon" << endl;
} else {
cout << "evening" << endl;
}
return 0;
}When if is the wrong tool
If you are picking among many integer labels — a menu choice, a day number — a switch is often clearer. That is the next chapter. For ranges such as scores, stay with else if.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.
Biology
Fever as a threshold
Many clinics treat 38.0 °C oral as fever. 37.5–38.0 is a grey band they may call raised. 36.8 °C is ordinary. These cutoffs are policy, not a law of nature.
else if chains the bands so a temperature is classified once. Order matters: test the strict fever first.
Example
#include <iostream>
using namespace std;
int main() {
double temp_c = 38.4;
if (temp_c >= 38.0) {
cout << "fever" << endl;
} else if (temp_c >= 37.5) {
cout << "raised" << endl;
} else {
cout << "normal" << endl;
}
return 0;
}Physics
Compared with the speed of sound
In dry air near 20 °C, sound travels at about 343 m/s. School problems often round to 340. A craft at 310 m/s is subsonic. Mach number is v / v_sound.
Mach = v / v_sound
Example
#include <iostream>
using namespace std;
int main() {
double v = 310.0;
if (v > 340.0) {
cout << "supersonic" << endl;
} else if (v == 340.0) {
cout << "Mach 1" << endl;
} else {
cout << "subsonic" << endl;
}
return 0;
}