capypad
0 day streak
cpp / beginner
Snippet

If-Else Statements: Making Decisions

If-else statements allow your program to make decisions based on conditions. If the condition in the if-part is true, that block executes. Otherwise, the else block runs.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
 
int main() {
int age = 18;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are a minor." << endl;
}
return 0;
}
Breakdown
1
if (age >= 18)
Checks if age is greater than or equal to 18
2
cout << "You are an adult." << endl;
This line runs only when the condition is true
3
else { ... }
The else block runs when the if condition is false