if Statement in C
So far, every program ran all its lines from top to bottom. Real programs need to make decisions: show "Access granted" only if the password is right, give a discount only if the bill is above ₹1000.
The if statement is the simplest way to make a decision in C. In this lesson you will learn its syntax, flowchart and how C checks the condition.
What You Will Learn
- What decision making (control flow) means.
- The syntax and flowchart of the
ifstatement. - How C decides if a condition is true.
- When curly braces
{ }are needed. - The semicolon mistake that silently breaks an if.
if Statement in C Explained
Exam definition: The if statement is a decision-making statement that executes a block of code only when a given condition is true. If the condition is false, the block is skipped.
- The condition goes inside brackets
( ). - In C, any non-zero value is true and 0 is false.
- The
ifstatement is part of control flow: statements that change the normal top-to-bottom order.
Memory trick: if is a gatekeeper. If your condition is true, the gate opens and you go inside the block. If false, you walk past the gate and continue.
Syntax of if Statement in C
if (condition)
{
/* statements run only when condition is true */
}
/* next statement always runs */
If the block has only one statement, the braces are optional:
if (marks >= 40)
printf("Pass\n");
Flowchart of the if statement
[ Start ]
|
< condition true? >
/ \
Yes No
| |
[ if block ] |
| |
\ /
[ next statement ]
Example 1: Check if a Number is Positive
#include <stdio.h>
int main(void)
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (n > 0)
{
printf("%d is positive.\n", n);
}
printf("Program finished.\n");
return 0;
}
Output
Enter a number: 15 15 is positive. Program finished.
If you enter -4 instead, the output is only Program finished., because the if block is skipped.
Code Explanation
scanf("%d", &n);: reads the number (15).if (n > 0): checks the condition. 15 > 0 is true (1).- Because it is true, the statement inside
{ }runs and prints "15 is positive." printf("Program finished.\n");is outside the if, so it runs every time.
How the if Statement Works
- C evaluates the expression inside the brackets.
- If the result is non-zero (true), it runs the statement or block right after the if.
- If the result is 0 (false), it jumps over that block.
- Either way, the program continues with the next statement after the block.
- Without braces, only the first statement after
ifbelongs to it. Lines after that always run.
Example 2: The Semicolon Bug
#include <stdio.h>
int main(void)
{
int age = 15;
if (age >= 18); /* BUG: semicolon ends the if */
printf("Can vote (wrong!)\n");
if (age >= 18) /* correct */
printf("Can vote\n");
printf("Age checked.\n");
return 0;
}
Output
Can vote (wrong!) Age checked.
A semicolon right after if (...) makes an empty statement the body of the if. The next printf is no longer part of the if, so it always runs, even for a 15-year-old. The correct if (without ;) prints nothing because 15 < 18.
Important Points to Remember
ifruns its block only when the condition is true.- Non-zero = true, 0 = false.
- No semicolon after
if (condition). - Without braces, only one statement belongs to the if.
- Always use braces when the block may grow; it prevents bugs.
Common Mistakes Beginners Make
| Mistake | What happens | Fix |
|---|---|---|
if (x > 0); | The block always runs | Remove the semicolon |
if (x = 5) | Assigns 5; always true | if (x == 5) |
| Two statements without braces | Only the first is inside the if | Use { } |
if x > 0 (no brackets) | Syntax error | if (x > 0) |
IF (x > 0) | Error: C is case-sensitive | Lowercase if |
if Statement Practice Questions
- What is an if statement? Write its syntax.
- What happens if the condition of an if is false?
- What is the output of
if (0) printf("A"); printf("B");? - Why is
if (x > 5);a mistake? - Is
if (-3)true or false?
Show answers
- A decision statement that runs a block only when the condition is true.
if (condition) { statements } - The block is skipped, and the program continues with the next statement.
B(only the first statement belongs to the if, and 0 is false).- The semicolon becomes the empty body of the if, so the following statement always runs.
- True (any non-zero value is true).
Coding Exercise
Read a shopping bill amount. If it is more than ₹1000, print "You get a 10% discount!" and the discounted amount. Always print "Thank you for shopping."
Show solution
#include <stdio.h>
int main(void)
{
float bill;
printf("Enter bill amount: ");
scanf("%f", &bill);
if (bill > 1000)
{
printf("You get a 10%% discount!\n");
printf("Pay: %.2f\n", bill * 0.9f);
}
printf("Thank you for shopping.\n");
return 0;
}
Enter bill amount: 1500 You get a 10% discount! Pay: 1350.00 Thank you for shopping.
Quick Quiz: if Statement in C (MCQ)
-
The if block runs when the condition is:
A) 0 B) Non-zero C) Negative only D) Always
-
Output of
if (5 > 10) printf("Yes"); printf("Done");A) YesDone B) Yes C) Done D) Nothing
-
Which is correct?
A)
if x > 5B)if (x > 5)C)if [x > 5]D)if {x > 5} -
Without braces, how many statements belong to an if?
A) 0 B) 1 C) 2 D) All until the end
-
if (x = 0)is:A) Always true B) Always false C) A comparison D) An error
Show answers
- B: non-zero.
- C: Done.
- B:
if (x > 5) - B: 1.
- B: always false (it assigns 0).
Quick Revision for Exams and Interviews
Read this before your exam. The highlighted words are the answer.
Must-remember facts
| if statement | Runs a block only when the condition is true |
|---|---|
| Syntax | if (condition) { statements } |
| True / false | Non-zero / 0 |
| No braces | Only 1 statement belongs to the if |
if (x); | Empty body bug |
| Type | Decision-making (selection) statement |
Frequently asked questions
- What is the use of the if statement?
- To run code only when a condition is true, so the program can make decisions.
- Are braces compulsory in an if statement?
- No, only for more than one statement, but using them always is safer.
- What happens with a semicolon after if?
- The semicolon is the empty body, so the next statement always runs.
- What values are true in C?
- Any non-zero value, including negative numbers.
Remember
- if = gatekeeper: true → enter the block, false → walk past.
- No
;afterif (...).
Next Topic
Next lesson: if-else Statement in C. You will learn how to do one thing when the condition is true and another when it is false.