C++ if Statement
In this page:
if (condition) {
// runs when condition is true
}
Basic if Statement
An if statement evaluates a condition and executes its associated block only when that condition is true; when it's false, the block is skipped entirely and execution continues right after it.
This is the most fundamental building block for making a program's behavior depend on data rather than always doing the same thing.
Example: Basic if Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 20
int age = 20;
// Check whether age >= 18
if (age >= 18) {
// Print to the console with cout
std::cout << "Adult" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Comparison inside if Condition
Comparison operators like == inside an if condition let you branch based on whether two values match, such as if (userChoice == y).
Be careful not to accidentally type a single = here, since that would assign instead of compare and would almost always evaluate to true regardless of the actual values.
Example: Comparison inside if Condition
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare userChoice and set it to 'y'
char userChoice = 'y';
// Check whether userChoice == 'y'
if (userChoice == 'y') {
// Print to the console with cout
std::cout << "Confirmed" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical AND in if Conditions
Combining two conditions with && inside an if requires both to be true before the block runs, such as if (age >= 18 && hasLicense), which only grants access when every requirement is satisfied simultaneously.
This is how you express 'all of these things must hold' in code.
Example: Logical AND in if Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 20
int age = 20;
// Declare hasLicense and set it to true
bool hasLicense = true;
// Check whether age >= 18 && hasLicense
if (age >= 18 && hasLicense) {
// Print to the console with cout
std::cout << "Can drive" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Logical OR in if Conditions
Using || instead lets the block run if at least one condition is true, such as if (isWeekend || isHoliday), useful whenever any single qualifying condition should be enough to proceed.
Mixing && and || in one condition often needs parentheses to make the intended grouping unambiguous.
Example: Logical OR in if Conditions
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isWeekend and set it to false
bool isWeekend = false;
// Declare isHoliday and set it to true
bool isHoliday = true;
// Check whether isWeekend || isHoliday
if (isWeekend || isHoliday) {
// Print to the console with cout
std::cout << "No work today" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested if Statements
Placing one if inside another lets you check a second condition only once the first has already passed, such as verifying a user is logged in before separately checking whether they have admin rights.
This mirrors real decision-making, where some questions only make sense to ask after an earlier one has already been answered.
Example: Nested if Statements
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare isLoggedIn and set it to true
bool isLoggedIn = true;
// Declare isAdmin and set it to true
bool isAdmin = true;
// Check whether isLoggedIn
if (isLoggedIn) {
// Check whether isAdmin
if (isAdmin) {
// Print to the console with cout
std::cout << "Welcome, admin" << std::endl;
}
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
=instead of==in the condition, likeif (x = 5). - Adding a semicolon right after the condition,
if (x > 5);, so the block always runs. - Omitting braces on a multi-line body, so only the first statement belongs to the
if.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: