The if statement is one of the most important decision-making statements in C. It allows a program to execute a block of code only when a specified condition is true.
The if statement is used to make a decision in a program.
If the specified condition is true, the statements inside the
if block are executed.
if (condition)
{
// statements
}
If the condition is false, the statements inside the block are skipped.
if (condition)
{
statement;
}
The condition is written inside parentheses. The statements that should execute when the condition is true are written inside curly braces.
int age = 20;
if (age >= 18)
{
printf("You are an adult.");
}
Since age >= 18 is true, the message is displayed.
The execution process is:
int number = 10;
if (number > 5)
{
printf("Number is greater than 5.");
}
int marks = 80;
if (marks > 50)
{
printf("Marks are greater than 50.");
}
The condition marks > 50 is true, so the message is printed.
int age = 15;
if (age < 18)
{
printf("You are under 18.");
}
Because 15 is less than 18, the statement inside the block executes.
int number = 10;
if (number == 10)
{
printf("Number is 10.");
}
The == operator compares two values. It should not be confused
with the assignment operator =.
int number = 15;
if (number != 10)
{
printf("Number is not 10.");
}
The condition is true because 15 is not equal to 10.
int marks = 40;
if (marks >= 40)
{
printf("You passed.");
}
The condition includes both possibilities: greater than 40 and exactly 40.
int age = 18;
if (age <= 18)
{
printf("Age is 18 or below.");
}
The condition is true because the value is equal to 18.
int salary = 30000;
if (salary > 20000)
{
printf("Salary is above 20000.");
}
Variables can be used directly in conditions.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("You can vote.");
}
return 0;
}
The user enters an age, and the program checks the condition.
int number = 25;
if (number > 0)
{
printf("Number is positive.");
}
The program prints the message only when the number is greater than zero.
int number = -10;
if (number < 0)
{
printf("Number is negative.");
}
The condition checks whether the number is less than zero.
int number = 20;
if (number % 2 == 0)
{
printf("Number is even.");
}
If the remainder after dividing by 2 is zero, the number is even.
int number = 15;
if (number % 2 != 0)
{
printf("Number is odd.");
}
If the remainder is not zero, the number is odd.
int age = 25;
int marks = 70;
if (age >= 18 && marks >= 40)
{
printf("Eligible.");
}
The block executes only when both conditions are true.
int number = 5;
if (number == 5 || number == 10)
{
printf("Number is 5 or 10.");
}
The block executes when at least one condition is true.
int loggedIn = 0;
if (!loggedIn)
{
printf("Please log in.");
}
The ! operator reverses the logical value of its operand.
int marks = 85;
if (marks >= 40)
{
printf("You passed.\n");
printf("Congratulations!\n");
}
An if block can contain multiple statements.
Curly braces are recommended when an if statement contains
multiple statements.
if (marks >= 40)
{
printf("Pass\n");
printf("Good job\n");
}
Braces clearly show which statements belong to the if block.
If there is only one statement, braces may be omitted.
if (number > 0)
printf("Positive");
However, using braces is generally clearer and helps prevent mistakes when more statements are added later.
Incorrect comparison:
if (number = 10)
{
printf("Number is 10.");
}
The statement above performs an assignment rather than an equality comparison.
Correct comparison:
if (number == 10)
{
printf("Number is 10.");
}
Avoid writing:
if (number > 10);
{
printf("Number is greater than 10.");
}
The semicolon ends the if statement. This can cause the block
following it to execute independently of the condition.
Correct form:
if (number > 10)
{
printf("Number is greater than 10.");
}
char grade = 'A';
if (grade == 'A')
{
printf("Excellent grade.");
}
Characters can be compared using relational and equality operators.
float price = 150.50f;
if (price > 100.0f)
{
printf("Price is above 100.");
}
Floating-point variables can also be used in conditions. For exact floating-point comparisons, be careful because many decimal values cannot be represented exactly in binary floating-point.
In C, a nonzero value is treated as true.
int value = 5;
if (value)
{
printf("Condition is true.");
}
Since value is 5, the block is executed.
Zero is treated as false in a C condition.
int value = 0;
if (value)
{
printf("This statement will not execute.");
}
Because the condition is zero, the block is skipped.
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 40)
{
printf("You have passed.\n");
printf("Congratulations!");
}
return 0;
}
This program accepts marks and displays a message when the student has scored at least 40 marks.
Practice the following programs:
&&.if statement.Start with simple conditions and then combine relational and logical operators as you become more comfortable.
if statement is used for decision making.if block executes when the condition is true.== for comparison and = for assignment.if condition.if statement can be used with numbers, characters, variables, and expressions.Question: Which statement is used to execute a block of code only when a condition is true?