Bird
Raised Fist0
Cprogramming~20 mins

If statement in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
If Statement Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of nested if statements
What is the output of this C code snippet?
C
#include <stdio.h>

int main() {
    int x = 5;
    if (x > 3) {
        if (x < 10) {
            printf("Inside nested if\n");
        }
    } else {
        printf("Outside if\n");
    }
    return 0;
}
AInside nested if
BOutside if
CNo output
DCompilation error
Attempts:
2 left
💡 Hint
Check the conditions in the nested if statements carefully.
❓ Predict Output
intermediate
2:00remaining
Output of if-else with logical operators
What will this program print?
C
#include <stdio.h>

int main() {
    int a = 4, b = 7;
    if (a > 3 && b < 10) {
        printf("Condition met\n");
    } else {
        printf("Condition not met\n");
    }
    return 0;
}
ACondition met
BCondition not met
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Both parts of the && operator must be true for the if block to run.
❓ Predict Output
advanced
2:00remaining
Output of if statement with assignment inside condition
What is the output of this program?
C
#include <stdio.h>

int main() {
    int x = 0;
    if (x = 5) {
        printf("x is %d\n", x);
    } else {
        printf("x is zero\n");
    }
    return 0;
}
Ax is zero
Bx is 5
CCompilation error
DNo output
Attempts:
2 left
💡 Hint
Remember that assignment inside if condition returns the assigned value.
❓ Predict Output
advanced
2:00remaining
Output of if statement with missing braces
What will this program print?
C
#include <stdio.h>

int main() {
    int n = 2;
    if (n > 1)
        printf("Greater than one\n");
    printf("Always printed\n");
    return 0;
}
ACompilation error
BAlways printed
CGreater than one
DGreater than one\nAlways printed
Attempts:
2 left
💡 Hint
Without braces, only the first statement is controlled by if.
🧠 Conceptual
expert
2:00remaining
Behavior of if statement with uninitialized variable
What happens when you run this code?
C
#include <stdio.h>

int main() {
    int x;
    if (x) {
        printf("x is true\n");
    } else {
        printf("x is false\n");
    }
    return 0;
}
AAlways prints "x is true"
BAlways prints "x is false"
CUndefined behavior due to uninitialized variable x
DCompilation error
Attempts:
2 left
💡 Hint
Local variables without initialization have unpredictable values.

Practice

(1/5)
1. What does an if statement do in C programming?
easy
A. It stores data in memory.
B. It repeats code multiple times.
C. It runs code only if a condition is true.
D. It defines a new function.

Solution

  1. Step 1: Understand the purpose of an if statement

    An if statement checks a condition and decides whether to run the code inside it.
  2. Step 2: Compare options with the if statement behavior

    Only It runs code only if a condition is true. correctly describes that the code runs only if the condition is true.
  3. Final Answer:

    It runs code only if a condition is true. -> Option C
  4. Quick Check:

    If statement = runs code if true [OK]
Hint: If condition true, run code inside braces [OK]
Common Mistakes:
  • Confusing if with loops
  • Thinking if stores data
  • Mixing if with function definition
2. Which of the following is the correct syntax for an if statement in C?
easy
A. if (condition) { printf("Hello"); }
B. if (condition) printf("Hello")
C. if condition: printf("Hello");
D. if condition { printf("Hello"); }

Solution

  1. Step 1: Recall correct if statement syntax in C

    The condition must be inside parentheses, and the code block inside braces.
  2. Step 2: Check each option

    if (condition) { printf("Hello"); } uses parentheses for condition and braces for code block, which is correct syntax.
  3. Final Answer:

    if (condition) { printf("Hello"); } -> Option A
  4. Quick Check:

    Parentheses + braces = correct if syntax [OK]
Hint: Use parentheses for condition and braces for code block [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using colon instead of braces
  • Missing semicolon after printf
3. What will be the output of this code?
int x = 5;
if (x > 3) {
    printf("Yes");
} else {
    printf("No");
}
medium
A. Yes
B. No
C. Error
D. No output

Solution

  1. Step 1: Evaluate the condition x > 3

    Since x is 5, 5 > 3 is true.
  2. Step 2: Determine which block runs

    The if block runs and prints "Yes" because the condition is true.
  3. Final Answer:

    Yes -> Option A
  4. Quick Check:

    5 > 3 is true, prints Yes [OK]
Hint: If condition true, prints inside if block [OK]
Common Mistakes:
  • Confusing else block execution
  • Assuming syntax error without checking code
  • Ignoring condition evaluation
4. Find the error in this code snippet:
int a = 10;
if a > 5 {
    printf("Big");
}
medium
A. Variable a is not declared
B. Missing semicolon after printf
C. Braces should be parentheses
D. Condition must be inside parentheses

Solution

  1. Step 1: Check if statement syntax

    The condition in an if statement must be inside parentheses in C.
  2. Step 2: Identify the error in the code

    The code has if a > 5 { ... } without parentheses around a > 5, which is incorrect.
  3. Final Answer:

    Condition must be inside parentheses -> Option D
  4. Quick Check:

    Condition needs parentheses [OK]
Hint: Always put condition inside parentheses [OK]
Common Mistakes:
  • Using braces instead of parentheses for condition
  • Forgetting semicolon after statements
  • Assuming variable not declared
5. Consider this code:
int n = 0;
if (n) {
    printf("True");
} else {
    printf("False");
}

What will be printed and why?
hard
A. True, because n is zero
B. False, because zero is false in C
C. Error, n must be boolean
D. No output, program crashes

Solution

  1. Step 1: Understand how C treats integers in conditions

    In C, zero means false, any nonzero means true.
  2. Step 2: Evaluate the condition if (n)

    Since n is 0, the condition is false, so else block runs.
  3. Step 3: Determine output

    The else block prints "False".
  4. Final Answer:

    False, because zero is false in C -> Option B
  5. Quick Check:

    Zero = false, prints False [OK]
Hint: Zero means false, nonzero means true in if conditions [OK]
Common Mistakes:
  • Thinking zero is true
  • Expecting boolean type only
  • Assuming program error
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