0
0
Cprogramming~20 mins

If statement in C - Practice Problems & Coding Challenges

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