C Notes
Learn the if statement in C programming with syntax, examples, flowchart, nested if, common mistakes, and interview questions. Complete beginner-friendly guide.
The if statement is the most fundamental decision-making tool in C programming. It allows your program to execute a block of code only when a specific condition is true. Think of it like a traffic signal — if the light is green, you go; otherwise, you stay put.
In real-world programming, almost every application needs to make decisions. Whether you're checking if a user entered a valid password, if a number is positive, or if a student passed an exam — the if statement is what makes it possible.
What is the if Statement?
The if statement evaluates a condition (an expression) and executes the code inside its body only if that condition evaluates to a non-zero value (true). If the condition is false (zero), the program simply skips the if block and moves on to the next statement.
Syntax of if Statement
if (condition) {
// statements to execute when condition is true
}Here's what each part means:
| Component | Description |
|---|---|
if | Keyword that starts the conditional statement |
condition | Any expression that evaluates to true (non-zero) or false (zero) |
{ } | Block of code executed when condition is true |
Important: If there's only one statement inside the if block, the curly braces are optional. However, it's considered best practice to always use them.
Flowchart of if Statement
| Execute | |
|---|---|
| Statement |
Simple Example
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
printf("Program continues...\n");
return 0;
}You are eligible to vote. Program continues...
In this example, since age is 20 (which is greater than or equal to 18), the condition is true and the message gets printed.
How Conditions Work in C
In C, there is no dedicated boolean type in C89/C90. The language treats any non-zero value as true and zero as false. This is a crucial concept to understand:
#include <stdio.h>
int main() {
if (1) {
printf("This always executes (1 is true)\n");
}
if (0) {
printf("This never executes (0 is false)\n");
}
if (-5) {
printf("Negative numbers are also true!\n");
}
return 0;
}This always executes (1 is true) Negative numbers are also true!
Practical Examples
Example 1: Check if a Number is Positive
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0) {
printf("%d is a positive number.\n", num);
}
if (num < 0) {
printf("%d is a negative number.\n", num);
}
if (num == 0) {
printf("The number is zero.\n");
}
return 0;
}Enter a number: 15 15 is a positive number.
Example 2: Check Even Number
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is an even number.\n", num);
}
return 0;
}Enter a number: 8 8 is an even number.
Example 3: Multiple Statements Inside if
#include <stdio.h>
int main() {
int marks = 85;
if (marks >= 80) {
printf("Excellent performance!\n");
printf("You scored %d marks.\n", marks);
printf("Grade: A+\n");
}
return 0;
}Excellent performance! You scored 85 marks. Grade: A+
if Statement Without Braces
When there's only a single statement, you can skip the curly braces. But be careful — this is a common source of bugs.
#include <stdio.h>
int main() {
int x = 10;
if (x > 5)
printf("x is greater than 5\n"); // Only this is inside if
printf("This always prints\n"); // This is NOT inside if
return 0;
}x is greater than 5 This always prints
Common Mistakes with if Statement
Mistake 1: Using = Instead of ==
// WRONG - This assigns 5 to x, doesn't compare!
if (x = 5) {
printf("This always executes!\n");
}
// CORRECT - This compares x with 5
if (x == 5) {
printf("x is equal to 5\n");
}Mistake 2: Semicolon After if Condition
// WRONG - Semicolon terminates the if statement
if (x > 5); // Empty if body!
{
printf("This always executes regardless of condition\n");
}
// CORRECT
if (x > 5) {
printf("x is greater than 5\n");
}Mistake 3: Forgetting Braces for Multiple Statements
// WRONG - Only first printf is inside if
if (x > 5)
printf("Line 1\n");
printf("Line 2\n"); // This ALWAYS executes!
// CORRECT
if (x > 5) {
printf("Line 1\n");
printf("Line 2\n");
}Using Logical Operators with if
You can combine multiple conditions using logical operators:
Eligible for credit card. You are an adult.
if Statement with Characters
#include <stdio.h>
int main() {
char grade = 'A';
if (grade == 'A') {
printf("Outstanding! You topped the class.\n");
}
return 0;
}Outstanding! You topped the class.
Best Practices
- Always use curly braces — even for single statements
- Use meaningful conditions — make code readable
- Avoid assignment in conditions — use
==not= - Put constants on the left —
if (5 == x)catches typos at compile time - Keep conditions simple — break complex conditions into variables
Interview Questions
Q1: What happens if you put a semicolon after the if condition?
The if statement becomes an empty statement. The block after it executes unconditionally because it's treated as a separate compound statement, not as the body of the if.
Q2: Can we use a function call as the condition in an if statement?
Yes. Any expression that returns a value can be used. For example, if (scanf("%d", &n) == 1) checks if scanf successfully read one item.
Q3: What is the difference between if(x) and if(x == 1)?
if(x) is true for any non-zero value of x (could be 1, -5, 100, etc.), while if(x == 1) is true only when x is exactly 1.
Q4: Is the if statement a compile-time or runtime construct?
The if statement is a runtime construct. The condition is evaluated during program execution, not during compilation. However, if the condition is a constant expression, the compiler might optimize it away.
Q5: What is a dangling else problem?
When you have nested if statements without braces, it becomes ambiguous which if an else belongs to. C resolves this by associating the else with the nearest unmatched if.
Summary
The if statement is the building block of decision-making in C. It evaluates a condition and executes code only when that condition is true. Remember that C treats any non-zero value as true and zero as false. Always use braces, always use == for comparison, and never put a semicolon right after the condition. Mastering the if statement is essential before moving on to more complex control structures like if-else, switch, and loops.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for if Statement in C Programming.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, control, statements, statement, if statement in c programming
Related C Programming Topics