Nested If - Else
Part of the Fundamentals section of Coddy's C journey — lesson 31 of 63.
We can nest if-else statements within each other. This allows us to create hierarchical decision-making structures.
For example:
if (age > 18) {
if (hasLicense) {
printf("You can drive");
} else {
printf("Get a license first");
}
} else {
printf("Too young to drive");
}It can be infinitely nested:
if (condition1) {
if (condition2) {
if (condition3) {
// if condition1, condition2 and condition3 are true
}
}
}Challenge
EasyCreate a program that checks if someone can ride a rollercoaster. The requirements are:
- Must be at least 12 years old
- Must be taller than 150cm
- If they meet both requirements but are under 15, they need adult supervision
Print exactly these messages for each case:
- If too young:
Sorry, you are too young - If not tall enough:
Sorry, you are not tall enough - If under 15 and no adult:
Sorry, you need an adult with you - If under 15 with adult:
You can ride with adult supervision! - If 15 or older and tall enough:
You can ride by yourself!
Cheat sheet
You can nest if-else statements within each other to create hierarchical decision-making structures:
if (age > 18) {
if (hasLicense) {
printf("You can drive");
} else {
printf("Get a license first");
}
} else {
printf("Too young to drive");
}Nesting can be done infinitely:
if (condition1) {
if (condition2) {
if (condition3) {
// if condition1, condition2 and condition3 are true
}
}
}Try it yourself
#include <stdio.h>
int main() {
int age, height;
int hasAdult;
scanf("%d %d %d", &age, &height, &hasAdult); // Don't change this line
// Write your code below
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Control Flow
If StatementIf - ElseElse-IfSwitch CaseTernary Conditional OperatorRecap ChallengeNested If - Else3Operators
Arithmetic OperatorsModulo OperatorIncrement/DecrementAssignment OperatorsRelational OperatorsLogical Operators Part 1Logical Operators Part 2Logical Operators Part 3Recap Challenge