capypad
0 day streak
cpp / beginner
Snippet

If-Else Conditional Logic

If-else statements let your program make decisions. If the condition is true, the first block runs. If false, the else block runs. You can also chain multiple conditions with else if.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
int main() {
int age = 18;
if (age >= 18) {
std::cout << "Adult" << std::endl;
} else {
std::cout << "Minor" << std::endl;
}
return 0;
}
Breakdown
1
int age = 18;
Declare and initialize an integer variable
2
if (age >= 18)
Check if age is greater than or equal to 18
3
std::cout << "Adult" << std::endl;
Output executed when condition is true
4
else { ... }
Alternative block executed when condition is false