Lesson 23 of 60 – if Statement in C
38%

if Statement in C

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.

Note: In C, a condition is considered true when its value is non-zero and false when its value is zero.

1. What is an if Statement?

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.

2. Syntax of if Statement

if (condition)
{
    statement;
}

The condition is written inside parentheses. The statements that should execute when the condition is true are written inside curly braces.

3. Simple if Example

int age = 20;

if (age >= 18)
{
    printf("You are an adult.");
}

Since age >= 18 is true, the message is displayed.

4. How the if Statement Works

The execution process is:

  1. The program evaluates the condition.
  2. If the condition is non-zero, the block is executed.
  3. If the condition is zero, the block is skipped.
  4. The program continues with the statement after the block.
int number = 10;

if (number > 5)
{
    printf("Number is greater than 5.");
}

5. if Statement with Greater Than

int marks = 80;

if (marks > 50)
{
    printf("Marks are greater than 50.");
}

The condition marks > 50 is true, so the message is printed.

6. if Statement with Less Than

int age = 15;

if (age < 18)
{
    printf("You are under 18.");
}

Because 15 is less than 18, the statement inside the block executes.

7. if Statement with Equal To

int number = 10;

if (number == 10)
{
    printf("Number is 10.");
}

The == operator compares two values. It should not be confused with the assignment operator =.

8. if Statement with Not Equal

int number = 15;

if (number != 10)
{
    printf("Number is not 10.");
}

The condition is true because 15 is not equal to 10.

9. if Statement with Greater Than or Equal To

int marks = 40;

if (marks >= 40)
{
    printf("You passed.");
}

The condition includes both possibilities: greater than 40 and exactly 40.

10. if Statement with Less Than or Equal To

int age = 18;

if (age <= 18)
{
    printf("Age is 18 or below.");
}

The condition is true because the value is equal to 18.

11. Using Variables in an if Condition

int salary = 30000;

if (salary > 20000)
{
    printf("Salary is above 20000.");
}

Variables can be used directly in conditions.

12. if Statement with User Input

#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.

13. if Statement with Positive Number

int number = 25;

if (number > 0)
{
    printf("Number is positive.");
}

The program prints the message only when the number is greater than zero.

14. if Statement with Negative Number

int number = -10;

if (number < 0)
{
    printf("Number is negative.");
}

The condition checks whether the number is less than zero.

15. Checking an Even Number

int number = 20;

if (number % 2 == 0)
{
    printf("Number is even.");
}

If the remainder after dividing by 2 is zero, the number is even.

16. Checking an Odd Number

int number = 15;

if (number % 2 != 0)
{
    printf("Number is odd.");
}

If the remainder is not zero, the number is odd.

17. if Statement with Logical AND

int age = 25;
int marks = 70;

if (age >= 18 && marks >= 40)
{
    printf("Eligible.");
}

The block executes only when both conditions are true.

18. if Statement with Logical OR

int number = 5;

if (number == 5 || number == 10)
{
    printf("Number is 5 or 10.");
}

The block executes when at least one condition is true.

19. if Statement with Logical NOT

int loggedIn = 0;

if (!loggedIn)
{
    printf("Please log in.");
}

The ! operator reverses the logical value of its operand.

20. Using Multiple Statements

int marks = 85;

if (marks >= 40)
{
    printf("You passed.\n");
    printf("Congratulations!\n");
}

An if block can contain multiple statements.

21. Using Braces with if

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.

22. if Without Curly Braces

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.

23. Common Mistake: Using = Instead of ==

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.");
}

24. Common Mistake: Semicolon After if

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.");
}

25. if Statement with Character

char grade = 'A';

if (grade == 'A')
{
    printf("Excellent grade.");
}

Characters can be compared using relational and equality operators.

26. if Statement with Floating-Point Value

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.

27. if Statement with Nonzero Value

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.

28. if Statement with Zero

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.

29. Complete if Statement Program

#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.

30. Practice Programs Using if

Practice the following programs:

  1. Check whether a number is positive.
  2. Check whether a number is negative.
  3. Check whether a number is even.
  4. Check whether a number is odd.
  5. Check whether a person is eligible by age.
  6. Check whether marks are greater than 50.
  7. Check whether a number is equal to 100.
  8. Check whether a number is greater than or equal to 10.
  9. Check whether two conditions are true using &&.
  10. Create a simple eligibility program using an if statement.

Start with simple conditions and then combine relational and logical operators as you become more comfortable.

📌 Key Points

  • The if statement is used for decision making.
  • The code inside the if block executes when the condition is true.
  • In C, zero is treated as false.
  • Any nonzero value is treated as true.
  • Use == for comparison and = for assignment.
  • Relational operators can be used to create conditions.
  • Logical operators can combine multiple conditions.
  • Curly braces are recommended for clear and safe code.
  • Do not place an unnecessary semicolon immediately after the if condition.
  • The if statement can be used with numbers, characters, variables, and expressions.

🧠 Quick Quiz

Question: Which statement is used to execute a block of code only when a condition is true?