Lesson 24 of 60 – if-else Statement in C
40%

if-else Statement in C

The if-else statement is a decision-making statement in C. It allows a program to execute one block of code when a condition is true and another block when the condition is false.

Note: Use if when you want to execute code for a true condition and else when you want to execute alternative code for a false condition.

1. What is if-else?

The if-else statement provides two possible paths in a program. One path executes when the condition is true, and the other executes when the condition is false.

if (condition)
{
    // true block
}
else
{
    // false block
}

2. Syntax of if-else

if (condition)
{
    statement1;
}
else
{
    statement2;
}

Only one of the two blocks is executed for each evaluation of the if-else statement.

3. Simple if-else Example

int age = 16;

if (age >= 18)
{
    printf("Adult");
}
else
{
    printf("Minor");
}

Since the age is less than 18, the else block executes.

4. How if-else Works

The execution process is:

  1. The condition is evaluated.
  2. If the condition is true, the if block executes.
  3. If the condition is false, the else block executes.
  4. After that, program execution continues with the next statement.

5. Checking Pass or Fail

int marks = 65;

if (marks >= 40)
{
    printf("Pass");
}
else
{
    printf("Fail");
}

Because the marks are 65, the condition is true and Pass is displayed.

6. Checking Even or Odd

int number = 7;

if (number % 2 == 0)
{
    printf("Even");
}
else
{
    printf("Odd");
}

The remainder of 7 divided by 2 is not zero, so the program prints Odd.

7. Checking Positive or Negative

int number = -10;

if (number >= 0)
{
    printf("Positive");
}
else
{
    printf("Negative");
}

This example treats zero as non-negative.

8. Comparing Two Numbers

int a = 20;
int b = 10;

if (a > b)
{
    printf("A is greater");
}
else
{
    printf("B is greater or equal");
}

The else block executes whenever a > b is false.

9. Checking Equal or Not Equal

int a = 10;
int b = 10;

if (a == b)
{
    printf("Both are equal");
}
else
{
    printf("Both are different");
}

The equality operator == is used to compare the two values.

10. if-else with User Input

#include <stdio.h>

int main()
{
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

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

    return 0;
}

The result depends on the age entered by the user.

11. Checking Voting Eligibility

int age;

printf("Enter your age: ");
scanf("%d", &age);

if (age >= 18)
{
    printf("Eligible to vote.");
}
else
{
    printf("Not eligible to vote.");
}

The program selects one message based on the age condition.

12. if-else with Two Conditions

int age = 25;
int marks = 70;

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

The && operator requires both conditions to be true.

13. if-else with Logical OR

int number = 5;

if (number == 5 || number == 10)
{
    printf("Valid number");
}
else
{
    printf("Invalid number");
}

The condition is true when at least one of the two comparisons is true.

14. if-else with Character

char grade = 'A';

if (grade == 'A')
{
    printf("Excellent");
}
else
{
    printf("Keep improving");
}

Characters can also be used in conditions.

15. if-else with Floating-Point Values

float price = 150.50f;

if (price > 100.0f)
{
    printf("Expensive");
}
else
{
    printf("Affordable");
}

Floating-point values can be used in comparisons.

16. Checking a Number Using if-else

int number;

printf("Enter a number: ");
scanf("%d", &number);

if (number > 0)
{
    printf("Positive number");
}
else
{
    printf("Zero or negative number");
}

This example has two possible outcomes.

17. Finding the Larger Number

int a = 50;
int b = 30;

if (a > b)
{
    printf("A is larger");
}
else
{
    printf("B is larger or equal");
}

The condition determines which value is larger.

18. Finding the Smaller Number

int a = 20;
int b = 40;

if (a < b)
{
    printf("A is smaller");
}
else
{
    printf("B is smaller or equal");
}

The < operator is used to compare the two values.

19. Checking Divisibility

int number = 20;

if (number % 5 == 0)
{
    printf("Divisible by 5");
}
else
{
    printf("Not divisible by 5");
}

If the remainder is zero, the number is divisible by 5.

20. if-else with Multiple Statements

int marks = 75;

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

Both blocks can contain multiple statements.

21. Using Braces with if-else

Curly braces clearly identify which statements belong to each branch. They are recommended even when a branch currently contains only one statement.

if (number > 10)
{
    printf("Greater");
}
else
{
    printf("Not greater");
}

22. if-else Without Curly Braces

When each branch contains exactly one statement, braces can technically be omitted.

if (number > 0)
    printf("Positive");
else
    printf("Not positive");

Using braces is usually clearer, especially when code is later modified.

23. Common Mistake: Using = Instead of ==

Incorrect:

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

The = operator performs assignment. For comparison, use ==.

Correct:

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

24. Common Mistake: Semicolon After if

Avoid:

if (number > 10);
{
    printf("Greater than 10");
}
else
{
    printf("10 or less");
}

A semicolon immediately after the condition ends the if statement and causes a syntax problem with the following else.

Correct:

if (number > 10)
{
    printf("Greater than 10");
}
else
{
    printf("10 or less");
}

25. if-else with Zero and Nonzero Values

In C, zero is treated as false and a nonzero value is treated as true.

int value = 0;

if (value)
{
    printf("True");
}
else
{
    printf("False");
}

Output:

False

26. Nested if Inside else

An if statement can also appear inside an else block.

int number = -5;

if (number > 0)
{
    printf("Positive");
}
else
{
    if (number == 0)
    {
        printf("Zero");
    }
    else
    {
        printf("Negative");
    }
}

More complex alternatives are often easier to manage using an else-if ladder, which will be covered in the next lesson.

27. Complete Pass-Fail Program

#include <stdio.h>

int main()
{
    int marks;

    printf("Enter your marks: ");
    scanf("%d", &marks);

    if (marks >= 40)
    {
        printf("Result: Pass");
    }
    else
    {
        printf("Result: Fail");
    }

    return 0;
}

This is a simple real-world example of using if-else for decision making.

28. Complete Even-Odd Program

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    if (number % 2 == 0)
    {
        printf("Even number");
    }
    else
    {
        printf("Odd number");
    }

    return 0;
}

The remainder operator % is used to determine whether the number is divisible by 2.

29. Complete Largest Number Program

#include <stdio.h>

int main()
{
    int a, b;

    printf("Enter two numbers: ");
    scanf("%d %d", &a, &b);

    if (a > b)
    {
        printf("%d is larger", a);
    }
    else
    {
        printf("%d is larger or equal", b);
    }

    return 0;
}

This program compares two numbers and selects one of the two output messages.

30. Practice Programs Using if-else

Practice the following programs:

  1. Check whether a number is positive or negative.
  2. Check whether a number is even or odd.
  3. Check whether a student has passed or failed.
  4. Check whether a person is an adult or minor.
  5. Find the larger of two numbers.
  6. Find the smaller of two numbers.
  7. Check whether a number is divisible by 5.
  8. Check whether two numbers are equal.
  9. Check whether a number is greater than 100.
  10. Create a simple eligibility program using two conditions.

Practice writing the condition first, then create the true and false blocks.

📌 Key Points

  • The if-else statement provides two possible execution paths.
  • The if block executes when the condition is true.
  • The else block executes when the condition is false.
  • Only one branch of a normal if-else statement executes.
  • Use == for comparison and = for assignment.
  • Logical operators can be used to combine conditions.
  • Zero is treated as false and nonzero values are treated as true.
  • Curly braces are recommended for clear and maintainable code.
  • The else block does not have a condition of its own.
  • Nested decision statements can be used for more complex logic.

🧠 Quick Quiz

Question: Which block is executed when the if condition is false?