Home Tutorials C Tutorial if...else Statement
if...else Statement

if...else Statement


The if...else Statement

What is an if...else Statement?

The if...else statement is used when you have two possible paths of execution. It allows the program to perform one action if a condition is True and a different action if the condition is False.

  • The if block: Executes only if the condition is met.
  • The else block: Acts as a "catch-all" that executes if the condition is not met.
[Image of if-else flowchart in C programming]

Syntax

if (condition) {
    // Code runs if condition is TRUE
} else {
    // Code runs if condition is FALSE
}

Practical Examples

Example 1: Pass or Fail Logic

int marks = 25;

if (marks >= 35) {
    printf("Result: Passed\n");
} else {
    printf("Result: Failed\n");
}

In this case, since 25 is less than 35, the "Failed" message will be displayed.

Example 2: Even or Odd Checker

int num = 10;

if (num % 2 == 0) {
    printf("The number is Even.\n");
} else {
    printf("The number is Odd.\n");
}
Important Note: Only one of the two blocks can ever run. The computer evaluates the condition once and chooses the path immediately—it never executes both!

🏋️ Test Yourself With Exercises

Take our quiz on if...else Statement to test your knowledge.

Browse Quizzes »