C++ if-else Statement
In this page:
if (condition) {
// runs when condition is true
} else {
// runs when condition is false
}
Introduction to if-else
An if-else statement gives you exactly two paths: the if block runs when the condition is true, and the else block runs whenever it's false, guaranteeing that exactly one of the two always executes.
This is more useful than a bare if alone whenever you have a specific fallback action rather than simply doing nothing.
Example: Introduction to if-else
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 15
int age = 15;
// Check whether age >= 18
if (age >= 18) {
// Print to the console with cout
std::cout << "Adult" << std::endl;
} else {
// Print to the console with cout
std::cout << "Minor" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Variable Assignment inside if-else
Using if-else to set a variable's value based on a condition, like choosing between a weekday or weekend label, lets that decision live right where the variable is defined rather than scattered through later code.
This pattern is common enough that C++ also offers the ternary operator as a more compact alternative for simple two-way value choices.
Example: Variable Assignment inside if-else
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
// Declare day and set it to 6
int day = 6;
std::string label;
// Check whether day == 6 || day == 7
if (day == 6 || day == 7) {
label = "weekend";
} else {
label = "weekday";
}
// Print to the console with cout
std::cout << label << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Boolean Flags in if-else
Boolean variables pair naturally with if-else because the condition itself already reads like a question — if (isLoggedIn) is immediately clear, unlike checking a numeric flag against 0 or 1.
This is exactly the pattern behind common features like login gates and subscription-only content.
Example: Boolean Flags in if-else
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isLoggedIn and set it to false
bool isLoggedIn = false;
// Check whether isLoggedIn
if (isLoggedIn) {
// Print to the console with cout
std::cout << "Welcome back" << std::endl;
} else {
// Print to the console with cout
std::cout << "Please log in" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Block Scope inside if-else
A variable declared inside an if or else block's curly braces only exists for the duration of that block and disappears once execution leaves it — attempting to reference it afterward causes a compile error.
This scoping rule prevents variables meant for one branch's temporary logic from accidentally leaking into or colliding with code elsewhere.
Example: Block Scope inside if-else
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare condition and set it to true
bool condition = true;
// Check whether condition
if (condition) {
// Declare x and set it to 5
int x = 5;
// Print to the console with cout
std::cout << x << std::endl;
} else {
int x = 10;
// Print to the console with cout
std::cout << x << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compound Conditions in if-else
Combining several conditions with && and || inside a single if-else lets you express fairly sophisticated decision logic in one statement, such as granting access only to logged-in admins or verified moderators.
As these compound conditions grow more complex, breaking them into clearly named boolean variables first often keeps the final if-else far more readable.
Example: Compound Conditions in if-else
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Declare hasTicket and set it to true
bool hasTicket = true;
// Check whether age >= 18 && hasTicket
if (age >= 18 && hasTicket) {
// Print to the console with cout
std::cout << "Entry allowed" << std::endl;
} else {
// Print to the console with cout
std::cout << "Entry denied" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Putting a semicolon after the
ifcondition, which ends theifand leaves theelsewithout a partner. - Omitting braces so the wrong statement attaches to the
else(dangling else). - Writing
else (x > 5)expecting a condition, when a plainelsetakes none.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: