The short answer
A conditional statement runs a piece of code only when a test is true, or it picks one piece of code out of several. In C++, the basic form is the if keyword, a condition in parentheses, and a block of code in braces:
if (condition) {
// runs when condition is true
}
The condition must be an expression that can convert to bool. Use braces to group several statements under one condition. A single statement can go without braces, but using them every time is a safer habit.
The if statement
#include <iostream>
int main() {
int temperature = 30;
if (temperature > 25) {
std::cout << "It is warm.\n";
}
std::cout << "This line always runs.\n";
}
With a temperature of 30, the output is:
It is warm.
This line always runs.
The block after the if runs only when the condition is true. The line after the closing brace runs either way.
Adding else
An else block runs when the condition is false:
if (temperature > 25) {
std::cout << "It is warm.\n";
} else {
std::cout << "It is cool.\n";
}
The else part is optional. Leave it out when there is nothing to do in the false case. For a fuller treatment of if and else together, the if else statement syntax in c covers the details.
Chaining with else if
To test several conditions in order, add else if blocks:
int score = 75;
if (score >= 90) {
std::cout << "A\n";
} else if (score >= 80) {
std::cout << "B\n";
} else if (score >= 70) {
std::cout << "C\n";
} else {
std::cout << "Below C\n";
}
The output is C. The tests run from top to bottom. The first true condition wins, the rest are skipped, and the final else catches anything that matched none of them.
The switch statement
A switch case statement in c compares one integer-like value against fixed constants:
int day = 3;
switch (day) {
case 1:
std::cout << "Monday\n";
break;
case 2:
std::cout << "Tuesday\n";
break;
case 3:
std::cout << "Wednesday\n";
break;
default:
std::cout << "Another day\n";
break;
}
The output is Wednesday. Case labels must be constant values of an integer, character or enumeration type. Each case should end with a break statement in c. Without it, execution falls through into the next case. Use a switch when you compare one value to several fixed options. For ranges, such as scores from 70 to 79, use if and else if instead.
The conditional operator
C++ also has an expression form for a short choice that produces a value, condition ? a : b:
int a = 4;
int b = 9;
int larger = (a > b) ? a : b; // larger is 9
The conditional operator in c covers the rules for this form, including how its two values must match in type.
Common mistakes
Using = where == is meant is a common error. This block runs, because the assignment sets x to 5, and 5 counts as true:
int x = 0;
if (x = 5) {
std::cout << "This prints.\n";
}
Leaving out braces is another. In this code, only the first line after the if belongs to it, even though the indentation suggests both lines do:
if (x > 0)
std::cout << "positive\n";
std::cout << "always printed\n"; // not inside the if
Summary
if (condition) { ... }runs a block only when the condition is true.- Add
elsefor the false case, andelse iffor more tests in order. - A switch picks one of several fixed integer-like values, and each case usually ends with
break. - The
?:form gives a value in one line.