0
0
Cprogramming~20 mins

Else–if ladder in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Else-if Ladder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of an else-if ladder with multiple conditions
What is the output of the following C code?
C
#include <stdio.h>

int main() {
    int x = 15;
    if (x < 10) {
        printf("Less than 10\n");
    } else if (x < 20) {
        printf("Between 10 and 19\n");
    } else if (x < 30) {
        printf("Between 20 and 29\n");
    } else {
        printf("30 or more\n");
    }
    return 0;
}
ALess than 10
BBetween 20 and 29
CBetween 10 and 19
D30 or more
Attempts:
2 left
💡 Hint
Check which condition matches first in the else-if ladder.
Predict Output
intermediate
2:00remaining
Output when all else-if conditions fail
What will this C program print?
C
#include <stdio.h>

int main() {
    int num = 50;
    if (num < 10) {
        printf("Small number\n");
    } else if (num < 20) {
        printf("Medium number\n");
    } else if (num < 30) {
        printf("Large number\n");
    } else {
        printf("Extra large number\n");
    }
    return 0;
}
AExtra large number
BMedium number
CLarge number
DSmall number
Attempts:
2 left
💡 Hint
Check which condition matches the value 50.
🔧 Debug
advanced
2:00remaining
Identify the error in else-if ladder syntax
What error does this C code produce when compiled?
C
#include <stdio.h>

int main() {
    int a = 5;
    if (a > 0) {
        printf("Positive\n");
    } else if a == 0 {
        printf("Zero\n");
    } else {
        printf("Negative\n");
    }
    return 0;
}
ARuntime error: segmentation fault
BTypeError: invalid comparison
CNo error, prints 'Zero'
DSyntaxError: missing parentheses in else-if condition
Attempts:
2 left
💡 Hint
Check the syntax of the else-if condition.
Predict Output
advanced
2:00remaining
Output of nested else-if ladder with multiple variables
What is the output of this C program?
C
#include <stdio.h>

int main() {
    int x = 8, y = 12;
    if (x > 10) {
        if (y > 10) {
            printf("x > 10 and y > 10\n");
        } else {
            printf("x > 10 and y <= 10\n");
        }
    } else if (x > 5) {
        printf("x between 6 and 10\n");
    } else {
        printf("x 5 or less\n");
    }
    return 0;
}
Ax > 10 and y > 10
Bx between 6 and 10
Cx > 10 and y <= 10
Dx 5 or less
Attempts:
2 left
💡 Hint
Check the first condition and then the else-if condition carefully.
🧠 Conceptual
expert
2:00remaining
Number of times conditions are evaluated in else-if ladder
Consider this else-if ladder in C:
if (cond1) {
    // block1
} else if (cond2) {
    // block2
} else if (cond3) {
    // block3
} else {
    // block4
}
If cond1 is false, cond2 is true, and cond3 is true, how many conditions are evaluated before executing a block?
A2 conditions
B1 condition
C3 conditions
D4 conditions
Attempts:
2 left
💡 Hint
Remember else-if stops checking after the first true condition.