An if statement helps your program make decisions by choosing what to do based on a condition.
If statement in C
Introduction
Syntax
C
if (condition) { // code to run if condition is true }
The condition is a test that is either true or false.
If the condition is true, the code inside the braces runs.
Examples
x is greater than zero and prints a message if true.C
if (x > 0) { printf("x is positive\n"); }
age is 18 or more to allow voting.C
if (age >= 18) { printf("You can vote\n"); }
score equals 100 exactly.C
if (score == 100) { printf("Perfect score!\n"); }
Sample Program
This program checks if the variable number is positive and prints a message if it is.
C
#include <stdio.h> int main() { int number = 10; if (number > 0) { printf("The number is positive.\n"); } return 0; }
Important Notes
Always use parentheses around the condition.
Curly braces { } are needed to group the code that runs when the condition is true.
If you forget braces and write only one statement, it still works but can cause bugs if you add more later.
Summary
An if statement lets your program choose actions based on conditions.
Use parentheses for the condition and braces for the code block.
The code inside runs only if the condition is true.
Practice
1. What does an
if statement do in C programming?easy
Solution
Step 1: Understand the purpose of an if statement
An if statement checks a condition and decides whether to run the code inside it.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.Final Answer:
It runs code only if a condition is true. -> Option CQuick 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
Solution
Step 1: Recall correct if statement syntax in C
The condition must be inside parentheses, and the code block inside braces.Step 2: Check each option
if (condition) { printf("Hello"); } uses parentheses for condition and braces for code block, which is correct syntax.Final Answer:
if (condition) { printf("Hello"); } -> Option AQuick 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
Solution
Step 1: Evaluate the condition x > 3
Since x is 5, 5 > 3 is true.Step 2: Determine which block runs
The if block runs and prints "Yes" because the condition is true.Final Answer:
Yes -> Option AQuick 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
Solution
Step 1: Check if statement syntax
The condition in an if statement must be inside parentheses in C.Step 2: Identify the error in the code
The code has if a > 5 { ... } without parentheses around a > 5, which is incorrect.Final Answer:
Condition must be inside parentheses -> Option DQuick 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:
What will be printed and why?
int n = 0;
if (n) {
printf("True");
} else {
printf("False");
}What will be printed and why?
hard
Solution
Step 1: Understand how C treats integers in conditions
In C, zero means false, any nonzero means true.Step 2: Evaluate the condition if (n)
Since n is 0, the condition is false, so else block runs.Step 3: Determine output
The else block prints "False".Final Answer:
False, because zero is false in C -> Option BQuick 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
10 questions ~5 mineasy hard
