0
0
Cprogramming~20 mins

If–else statement in C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If–else Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested if–else
What is the output of this C code snippet?
C
int main() {
    int x = 5, y = 10;
    if (x > y) {
        printf("A\n");
    } else if (x == y) {
        printf("B\n");
    } else {
        printf("C\n");
    }
    return 0;
}
AB
BA
CC
DNo output
Attempts:
2 left
💡 Hint
Compare the values of x and y carefully.
Predict Output
intermediate
2:00remaining
If–else with logical operators
What will this program print?
C
int main() {
    int a = 3, b = 7;
    if (a > 0 && b < 5) {
        printf("X\n");
    } else {
        printf("Y\n");
    }
    return 0;
}
AY
BNo output
CX
DCompilation error
Attempts:
2 left
💡 Hint
Check both conditions in the if statement.
Predict Output
advanced
2:00remaining
If–else with assignment inside condition
What is the output of this code?
C
int main() {
    int x = 0;
    if (x = 5) {
        printf("Hello\n");
    } else {
        printf("World\n");
    }
    return 0;
}
AWorld
BHello
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition returns the assigned value.
Predict Output
advanced
2:00remaining
If–else with multiple conditions and else
What will this program print?
C
int main() {
    int n = 0;
    if (n > 0) {
        printf("Positive\n");
    } else if (n < 0) {
        printf("Negative\n");
    } else {
        printf("Zero\n");
    }
    return 0;
}
ANegative
BNo output
CPositive
DZero
Attempts:
2 left
💡 Hint
Check the value of n and which condition matches.
Predict Output
expert
2:00remaining
If–else with uninitialized variable
What error or output does this code produce?
C
int main() {
    int x;
    if (x > 0) {
        printf("Positive\n");
    } else {
        printf("Non-positive\n");
    }
    return 0;
}
AUndefined behavior (may print anything)
BNon-positive
CCompilation error
DPositive
Attempts:
2 left
💡 Hint
Consider what happens when you use a variable without setting it first.