Bird
Raised Fist0
Cprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does an else-if ladder do in C programming?
easy
A. Checks multiple conditions one by one and executes the first true block
B. Runs all conditions regardless of their truth value
C. Only executes the last condition in the ladder
D. Skips all conditions and runs the else block always

Solution

  1. Step 1: Understand the purpose of else-if ladder

    An else-if ladder is used to check multiple conditions sequentially.
  2. Step 2: Identify how conditions are executed

    Only the first condition that is true will have its block executed, and the rest are skipped.
  3. Final Answer:

    Checks multiple conditions one by one and executes the first true block -> Option A
  4. Quick Check:

    Else-if ladder = first true condition runs [OK]
Hint: Remember: else-if stops at first true condition [OK]
Common Mistakes:
  • Thinking all conditions run regardless
  • Believing else runs always
  • Confusing else-if with multiple ifs
2. Which of the following is the correct syntax for an else-if ladder in C?
easy
A. if(condition) { } else(condition) { } else if { }
B. if(condition) { } else if(condition) { } else { }
C. if(condition) { } else if { } else(condition) { }
D. if(condition) { } else if(condition) { } else if

Solution

  1. Step 1: Review correct else-if syntax

    The else-if ladder uses: if(condition) { } else if(condition) { } else { }.
  2. Step 2: Check each option for syntax errors

    if(condition) { } else if(condition) { } else { } matches correct syntax; others have misplaced else or missing conditions.
  3. Final Answer:

    if(condition) { } else if(condition) { } else { } -> Option B
  4. Quick Check:

    Correct else-if syntax = if(condition) { } else if(condition) { } else { } [OK]
Hint: else if always needs a condition in parentheses [OK]
Common Mistakes:
  • Writing else with a condition
  • Omitting parentheses in else-if
  • Ending else-if without braces
3. What will be the output of the following C code?
int x = 15;
if (x < 10) {
  printf("Less than 10\n");
} else if (x < 20) {
  printf("Less than 20\n");
} else {
  printf("20 or more\n");
}
medium
A. Less than 20
B. 20 or more
C. Less than 10
D. No output

Solution

  1. Step 1: Evaluate first condition (x < 10)

    x is 15, which is not less than 10, so this block is skipped.
  2. Step 2: Evaluate second condition (x < 20)

    15 is less than 20, so this block runs and prints "Less than 20".
  3. Final Answer:

    Less than 20 -> Option A
  4. Quick Check:

    15 < 20 = true, prints "Less than 20" [OK]
Hint: Check conditions top to bottom, first true runs [OK]
Common Mistakes:
  • Choosing first condition without checking value
  • Assuming else always runs
  • Confusing output strings
4. Find the error in this else-if ladder code snippet:
int num = 5;
if (num > 10)
  printf("Greater than 10\n");
else if num > 0
  printf("Positive number\n");
else
  printf("Zero or negative\n");
medium
A. Missing semicolon after printf statements
B. else cannot be used without if
C. Incorrect variable name used
D. Missing parentheses around condition in else-if

Solution

  1. Step 1: Check syntax of else-if condition

    In C, else-if condition must be inside parentheses: else if (condition).
  2. Step 2: Identify the error in given code

    Code has else if num > 0 without parentheses, causing syntax error.
  3. Final Answer:

    Missing parentheses around condition in else-if -> Option D
  4. Quick Check:

    else-if needs parentheses [OK]
Hint: Always put conditions in parentheses after else if [OK]
Common Mistakes:
  • Forgetting parentheses in else-if
  • Missing semicolons after printf
  • Misusing else without if
5. You want to write a program that prints "Child" if age < 13, "Teen" if age is between 13 and 19 inclusive, "Adult" if age is between 20 and 59 inclusive, and "Senior" if age is 60 or more. Which else-if ladder correctly implements this?
hard
A. if (age < 13) { printf("Child\n"); } else if (age < 20) { printf("Teen\n"); } else if (age < 59) { printf("Adult\n"); } else { printf("Senior\n"); }
B. if (age <= 13) { printf("Child\n"); } else if (age < 19) { printf("Teen\n"); } else if (age < 59) { printf("Adult\n"); } else { printf("Senior\n"); }
C. if (age <= 12) { printf("Child\n"); } else if (age <= 19) { printf("Teen\n"); } else if (age <= 59) { printf("Adult\n"); } else { printf("Senior\n"); }
D. if (age < 13) { printf("Child\n"); } else if (age < 19) { printf("Teen\n"); } else if (age <= 59) { printf("Adult\n"); } else { printf("Senior\n"); }

Solution

  1. Step 1: Analyze age ranges for each category

    Child: age < 13 means age ≤ 12; Teen: 13 to 19 inclusive; Adult: 20 to 59 inclusive; Senior: 60 or more.
  2. Step 2: Check each option's conditions

    if (age <= 12) { printf("Child\n"); } else if (age <= 19) { printf("Teen\n"); } else if (age <= 59) { printf("Adult\n"); } else { printf("Senior\n"); } correctly uses age ≤ 12 for Child, ≤ 19 for Teen, ≤ 59 for Adult, else Senior.
  3. Final Answer:

    if (age <= 12) { printf("Child\n"); } else if (age <= 19) { printf("Teen\n"); } else if (age <= 59) { printf("Adult\n"); } else { printf("Senior\n"); } -> Option C
  4. Quick Check:

    Correct age ranges with inclusive bounds = if (age <= 12) { printf("Child\n"); } else if (age <= 19) { printf("Teen\n"); } else if (age <= 59) { printf("Adult\n"); } else { printf("Senior\n"); } [OK]
Hint: Use inclusive bounds carefully in else-if conditions [OK]
Common Mistakes:
  • Using wrong comparison operators
  • Overlapping or missing age ranges
  • Incorrect boundary values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard