capypad
0 day streak
cpp / beginner
Snippet

If-Else: Making Decisions

If-else statements allow your program to make decisions based on conditions. The condition is evaluated as true or false. If true, the first block executes. Otherwise, else-if and else blocks are checked in sequence.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main() {
int score = 85;
if (score >= 90) {
std::cout << "Excellent!" << std::endl;
} else if (score >= 70) {
std::cout << "Good job!" << std::endl;
} else {
std::cout << "Keep practicing!" << std::endl;
}
return 0;
}
Breakdown
1
if (score >= 90)
First condition checked - if score is 90 or higher
2
else if (score >= 70)
Second condition checked only if first was false
3
else
Executes only if all previous conditions were false