C if-else Statement
In this page:
if (condition) {
// runs when condition is true
} else {
// runs when condition is false
}
What is if-else?
if-else covers exactly two possible outcomes for a condition: the if block runs when the condition is true, and the else block runs instead whenever it's false, so exactly one of the two paths always executes.
Example: What is if-else?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 15
int age = 15;
// Check whether age >= 18
if (age >= 18) {
// Print formatted text to the screen
printf("Adult");
} else {
// Print formatted text to the screen
printf("Minor");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pass or Fail Check
Comparing a score against a passing threshold with if-else -- if (score >= 50) ... else ... -- cleanly separates the pass and fail outcomes into two distinct, mutually exclusive code paths.
Example: Pass or Fail Check
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare score and set it to 40
int score = 40;
// Check whether score >= 50
if (score >= 50) {
// Print formatted text to the screen
printf("Pass");
} else {
// Print formatted text to the screen
printf("Fail");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Even or Odd Divider
Testing n % 2 == 0 inside an if-else lets a single conditional handle both the even and odd cases in one block, rather than needing two separate if statements to cover both outcomes.
Example: Even or Odd Divider
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 7
int n = 7;
// Check whether n % 2 == 0
if (n % 2 == 0) {
// Print formatted text to the screen
printf("Even");
} else {
// Print formatted text to the screen
printf("Odd");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Voting Eligibility
if-else is a natural fit for eligibility checks, such as verifying if (age >= 18) ... else ... to separate users who qualify for something from those who don't, in a single readable comparison.
Example: Voting Eligibility
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare age and set it to 16
int age = 16;
// Check whether age >= 18
if (age >= 18) {
// Print formatted text to the screen
printf("Can vote");
} else {
// Print formatted text to the screen
printf("Cannot vote");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested if-else
Nesting an if-else inside either branch of an outer if-else lets you handle sub-decisions -- for example, once you know a number is positive, a nested if-else can further classify it as even or odd.
Example: Nested if-else
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare n and set it to 8
int n = 8;
// Check whether n > 0
if (n > 0) {
// Check whether n % 2 == 0
if (n % 2 == 0) {
// Print formatted text to the screen
printf("Positive even");
} else {
// Print formatted text to the screen
printf("Positive odd");
}
} else {
printf("Not positive");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Putting a semicolon after
if (...), which breaks theelsethat follows. - Omitting braces so the wrong statement attaches to the
else. - Writing
else (x > 5), when a plainelsetakes no condition.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: