C Programs Tutorials | IT Developer
IT Developer

C Programming - if statement in C



Share with a Friend

C Programming - if statement in C

if Statement in C

The if statement in C is used to evaluate a condition and execute a block of code if the condition is true. It is one of the most basic control flow statements in programming. If the condition evaluates to true, the code inside the if block will run. If it evaluates to false, the code inside the if block will be skipped.

Syntax of if Statement:

C

if (condition) {

    // Code to be executed if the condition is true

}

  • condition: An expression that evaluates to true (non-zero) or false (zero).
  • Code block: The statements inside {} that execute when the condition is true.

Example 1: Basic if Statement

C

#include <stdio.h>

int main() {

    int a = 10;

    // Check if a is greater than 5

    if (a > 5) {

        printf("a is greater than 5\n");

    }

    return 0;

}

Explanation:

  • The condition a > 5 is true because a is 10.
  • Therefore, the statement printf("a is greater than 5\n"); is executed.

Output:

a is greater than 5

Example 2: if Statement with a False Condition

C

#include <stdio.h>

int main() {

    int a = 3;

    // Check if a is greater than 5

    if (a > 5) {

        printf("a is greater than 5\n");

    } else {

        printf("a is not greater than 5\n");

    }

    return 0;

}

Explanation:

  • The condition a > 5 is false because a is 3.
  • Since the condition is false, the program jumps to the else block, which prints "a is not greater than 5".

Output:

a is not greater than 5

Example 3: if with Multiple Conditions (Logical Operators)

You can combine multiple conditions using logical operators like && (AND) and || (OR).

C

#include <stdio.h>

int main() {

    int a = 10, b = 5;

    // Check if a is greater than 5 and b is less than 10

    if (a > 5 && b < 10) {

        printf("a is greater than 5 and b is less than 10\n");

    }

    return 0;

}

Explanation:

  • The condition a > 5 && b < 10 is true because both conditions are true (a is 10 and b is 5).
  • Thus, the statement printf("a is greater than 5 and b is less than 10\n"); is executed.

Output:

a is greater than 5 and b is less than 10

Example 4: if Statement Without {} (Single Statement Block)

If there is only one statement inside the if block, curly braces {} can be omitted, but it's good practice to use them for clarity.

C

#include <stdio.h>

int main() {

    int a = 10;

    // Check if a is greater than 5 (without using braces)

    if (a > 5)

        printf("a is greater than 5\n");

    return 0;

}

Explanation:

  • Since there is only one statement in the if block, the curly braces are optional.
  • The condition a > 5 is true, so the message "a is greater than 5" is printed.

Output:

a is greater than 5

Important Points to Remember:

  • Condition: The condition inside the if statement must evaluate to a boolean value (true or false). In C, any non-zero value is considered true, and 0 is considered false.
  • Scope: If you omit the {} around the block of code, only the first statement after the if condition will be considered part of the if block.
  • Nesting: You can nest if statements inside each other to handle more complex decision-making.

Summary of if Statement:

  • The if statement is used to check if a condition is true, and execute a block of code accordingly.
  • If the condition is true, the block of code inside the if statement is executed.
  • If the condition is false, the program skips the block and continues executing the remaining code.