1. Intro
The if statement is the most basic decision-making construct in C++. It lets a program test a condition and run a block of code only when that condition is true. Decision-making statements let a program choose between different paths of execution based on data available at runtime, and if is the building block every other conditional construct (if-else, else if ladder, switch) is built on top of.
Cricket analogy: A fielding captain testing 'is the batsman out of his crease?' before calling for a run-out appeal is a basic decision, just as if is the foundational construct every more complex decision in C++ builds on.
2. Syntax
if (condition) {
// statement(s) executed only if condition is true
}3. Explanation
The condition inside the parentheses must evaluate to a boolean value (true/false), or to an expression that is implicitly convertible to bool — any nonzero value is treated as true, and 0 is treated as false. If the condition evaluates to true, the statement block runs; otherwise, it is skipped entirely and control moves to the next statement after the if block. If the body contains only a single statement, the curly braces {} are technically optional, but omitting them is considered bad practice because it makes code error-prone when new lines are added later.
Cricket analogy: Any nonzero run total from a ball being treated as 'runs scored' (true) while exactly zero runs is a dot ball (false) mirrors how C++ treats any nonzero value as true and 0 as false in an if condition.
4. Example
#include <iostream>
using namespace std;
int main() {
int age = 20;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
cout << "Program finished." << endl;
return 0;
}5. Output
You are eligible to vote.
Program finished.6. Key Takeaways
- The
ifstatement executes a block only when its condition evaluates totrue. - Any nonzero numeric value is treated as
true;0is treated asfalse. - If there is no matching
else, a false condition simply skips the block and execution continues after it. - Always use
{}around the body, even for a single statement, to avoid bugs when the code is edited later.
Practice what you learned
1. What type of value must the condition in an `if` statement evaluate to (or be convertible to)?
2. What happens when the condition in an `if` statement is `false` and there is no `else`?
3. What value does the integer `0` represent when used as an `if` condition?
4. Why is it recommended to always use curly braces `{}` with `if`, even for a single statement?
5. In `if (x)` where `x` is an `int` variable holding `-5`, what does the condition evaluate to?
Was this page helpful?
You May Also Like
if-else Statement in C++
Understand how the C++ `if-else` statement provides a two-way branch, executing one block when the condition is true and another when false.
Relational Operators in C++
Understand C++ relational (comparison) operators such as ==, !=, <, >, <=, and >=, and how they always evaluate to a bool result.
Logical Operators in C++
Master the C++ logical operators &&, ||, and !, and understand short-circuit evaluation and how it can skip side effects.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics