Bird
Raised Fist0
Cprogramming~20 mins

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

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

Practice

(1/5)
1. What does an if-else statement do in C?
easy
A. It declares a variable.
B. It repeats a block of code multiple times.
C. It defines a new function.
D. It chooses between two actions based on a condition.

Solution

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

    An if-else statement lets the program pick one of two paths depending on whether a condition is true or false.
  2. Step 2: Match the description to the options

    Only It chooses between two actions based on a condition. describes choosing between two actions based on a condition, which is exactly what if-else does.
  3. Final Answer:

    It chooses between two actions based on a condition. -> Option D
  4. Quick Check:

    If-else = choose between two paths [OK]
Hint: If you want two choices, use if-else [OK]
Common Mistakes:
  • Confusing if-else with loops
  • Thinking if-else declares variables
  • Mixing if-else with function definitions
2. Which of the following is the correct syntax for an if-else statement in C?
easy
A. if (condition) then { /* code */ } else { /* code */ }
B. if condition then { /* code */ } else { /* code */ }
C. if (condition) { /* code */ } else { /* code */ }
D. if condition { /* code */ } else { /* code */ }

Solution

  1. Step 1: Recall C syntax rules for if-else

    In C, conditions must be inside parentheses and braces {} are used for code blocks.
  2. Step 2: Check each option

    Only if (condition) { /* code */ } else { /* code */ } uses parentheses around the condition and braces correctly without extra keywords like 'then'.
  3. Final Answer:

    if (condition) { /* code */ } else { /* code */ } -> Option C
  4. Quick Check:

    Correct syntax uses parentheses and braces [OK]
Hint: Remember: if(condition) with braces {} [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using 'then' keyword (not in C)
  • Skipping braces for multiple statements
3. What will be the output of this C code?
#include 
int main() {
    int x = 10;
    if (x > 5) {
        printf("Greater\n");
    } else {
        printf("Smaller\n");
    }
    return 0;
}
medium
A. Greater
B. Smaller
C. 10
D. No output

Solution

  1. Step 1: Evaluate the condition x > 5

    Variable x is 10, which is greater than 5, so the condition is true.
  2. Step 2: Determine which block runs

    Since the condition is true, the code inside the if block runs, printing "Greater".
  3. Final Answer:

    Greater -> Option A
  4. Quick Check:

    10 > 5 is true, so prints "Greater" [OK]
Hint: Check if condition is true or false to pick output [OK]
Common Mistakes:
  • Confusing which block runs on true condition
  • Ignoring the newline character in printf
  • Assuming else block runs when condition is true
4. Find the error in this if-else code snippet:
int a = 3;
if a > 0
    printf("Positive\n");
else
    printf("Non-positive\n");
medium
A. Missing semicolon after if statement
B. Missing parentheses around condition in if statement
C. Missing braces around printf statements
D. Incorrect else keyword

Solution

  1. Step 1: Check syntax of if condition

    In C, the condition in an if statement must be inside parentheses (). Here, it is missing.
  2. Step 2: Verify other parts

    Semicolons after printf are present, braces are optional for single statements, and else keyword is correct.
  3. Final Answer:

    Missing parentheses around condition in if statement -> Option B
  4. Quick Check:

    if condition needs parentheses ( ) [OK]
Hint: Always put condition inside parentheses ( ) [OK]
Common Mistakes:
  • Forgetting parentheses in if condition
  • Thinking braces are always required for single statements
  • Misplacing else keyword
5. You want to write a program that prints "Even" if a number is even, and "Odd" if it is odd. Which if-else code correctly does this in C?
hard
A. if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); }
B. if (num / 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); }
C. if (num % 2 = 0) { printf("Even\n"); } else { printf("Odd\n"); }
D. if (num % 2 != 0) { printf("Even\n"); } else { printf("Odd\n"); }

Solution

  1. Step 1: Understand how to check even numbers

    A number is even if dividing by 2 leaves remainder 0, which is checked by num % 2 == 0.
  2. Step 2: Analyze each option

    if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } correctly uses modulus operator and equality check. if (num / 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } uses division instead of modulus. if (num % 2 = 0) { printf("Even\n"); } else { printf("Odd\n"); } uses assignment (=) instead of comparison (==). if (num % 2 != 0) { printf("Even\n"); } else { printf("Odd\n"); } reverses the logic.
  3. Final Answer:

    if (num % 2 == 0) { printf("Even\n"); } else { printf("Odd\n"); } -> Option A
  4. Quick Check:

    Use modulus == 0 to check even [OK]
Hint: Use num % 2 == 0 to test even numbers [OK]
Common Mistakes:
  • Using division instead of modulus
  • Using = instead of == in condition
  • Reversing even and odd logic
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