Home C if Statement in C tutorial

if Statement in C tutorial

In C programming, the if statement is used to execute a block of code based on a specified condition.

It’s one of the fundamental tools for decision-making, allowing you to control the flow of your program.

1. Basic Syntax of the if Statement

if (condition) {
    // Code to execute if the condition is true
}
  • Condition: The expression that is evaluated. If the condition is true (non-zero), the code inside the if block executes. If the condition is false (zero), the code inside the if block is skipped.

2. Basic Example of if Statement

Here’s a simple example using the if statement to check if a number is positive.

#include <stdio.h>

int main() {
    int num = 10;

    if (num > 0) {
        printf("The number is positive.\n");
    }

    return 0;
}

Explanation:

  • num > 0 checks if num is positive.
  • Since num is 10, which is greater than 0, the message “The number is positive” is printed.

Output:

The number is positive.

3. Using if-else Statement

The if-else statement lets you execute one block of code if the condition is true and another if it’s false.

#include <stdio.h>

int main() {
    int num = -5;

    if (num > 0) {
        printf("The number is positive.\n");
    } else {
        printf("The number is not positive.\n");
    }

    return 0;
}

Explanation:

  • If num is greater than 0, it prints “The number is positive.”
  • Otherwise, it prints “The number is not positive.”

Output:

The number is not positive.

4. if-else if-else Statement

The if-else if-else statement lets you test multiple conditions sequentially.

#include <stdio.h>

int main() {
    int num = 0;

    if (num > 0) {
        printf("The number is positive.\n");
    } else if (num < 0) {
        printf("The number is negative.\n");
    } else {
        printf("The number is zero.\n");
    }

    return 0;
}

Explanation:

  • The program checks if num is positive, negative, or zero and prints the corresponding message.

Output:

The number is zero.

5. Using if with Logical Operators

You can combine multiple conditions in an if statement using logical operators like && (and) and || (or).

Example: Checking if a Number is Within a Range

#include <stdio.h>

int main() {
    int num = 15;

    if (num >= 10 && num <= 20) {
        printf("The number is within the range 10 to 20.\n");
    } else {
        printf("The number is outside the range.\n");
    }

    return 0;
}

Explanation:

  • num >= 10 && num <= 20 checks if num is between 10 and 20 (inclusive).
  • Since num is 15, it falls within this range, so the first message is printed.

Output:

The number is within the range 10 to 20.

6. Nested if Statements

You can nest if statements to test more complex conditions.

Example: Checking if a Number is Positive, Negative, or Zero

#include <stdio.h>

int main() {
    int num = -3;

    if (num != 0) {
        if (num > 0) {
            printf("The number is positive.\n");
        } else {
            printf("The number is negative.\n");
        }
    } else {
        printf("The number is zero.\n");
    }

    return 0;
}

Explanation:

  • The outer if checks if num is not zero.
  • Inside the outer if, another if-else checks if num is positive or negative.

Output:

The number is negative.

7. Using if with Ternary Operator

The ternary operator (? 🙂 provides a shorthand for simple if-else statements.

Example: Using Ternary Operator for Conditional Assignment

#include <stdio.h>

int main() {
    int num = 10;
    int isPositive = (num > 0) ? 1 : 0;

    printf("Is the number positive? %d\n", isPositive);

    return 0;
}

Explanation:

  • (num > 0) ? 1 : 0 checks if num is positive. If true, isPositive is set to 1; otherwise, it’s set to 0.

Output:

Is the number positive? 1

8. if Statement for User Input Validation

The if statement can be used to validate user input and ensure it meets certain criteria.

#include <stdio.h>

int main() {
    int age;

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

    if (age >= 1 && age <= 120) {
        printf("You entered a valid age.\n");
    } else {
        printf("Invalid age! Please enter an age between 1 and 120.\n");
    }

    return 0;
}

Explanation:

  • The if statement checks if the entered age is between 1 and 120.
  • If it’s within this range, it prints a success message; otherwise, it prints an error message.

Sample Output:

Enter your age: 130
Invalid age! Please enter an age between 1 and 120.

9. if Statement for Finding the Maximum of Two Numbers

The if statement is useful for determining the maximum (or minimum) value between two numbers.

#include <stdio.h>

int main() {
    int a = 10, b = 20;
    int max;

    if (a > b) {
        max = a;
    } else {
        max = b;
    }

    printf("The maximum value is: %d\n", max);

    return 0;
}

Explanation:

  • The if-else statement assigns the larger of a and b to max.

Output:

The maximum value is: 20

10. Using if with return for Early Exit

The if statement can be used to exit a function early based on a condition.

#include <stdio.h>

void checkNumber(int num) {
    if (num < 0) {
        printf("The number is negative. Exiting...\n");
        return; // Exit the function if num is negative
    }
    printf("The number is non-negative.\n");
}

int main() {
    checkNumber(-5);
    checkNumber(10);

    return 0;
}

Explanation:

  • The if statement checks if num is negative.
  • If true, it exits the function early using return.

Output:

The number is negative. Exiting...
The number is non-negative.

Summary Table of if Statement Examples

Use Case Description Example Code
Basic if statement Executes code if condition is true if (x > 0) { … }
if-else statement Executes alternative code if condition is false if (x > 0) { … } else { … }
if-else if-else statement Tests multiple conditions sequentially if (x > 0) { … } else if (x < 0) { … } else
Using logical operators Combines conditions with && or `
Nested if statements Tests more complex conditions if (x != 0) { if (x > 0) { … } else { … } }
Ternary operator Shorthand for if-else statements (x > 0) ? a : b;
Input validation Ensures user input meets criteria if (age >= 1 && age <= 120) { … }
Maximum of two numbers Finds the larger of two numbers if (a > b) max = a; else max = b;
Early exit from function Exits function if condition is met if (x < 0) return;

Complete Example: Multiple Uses of if Statement

This example demonstrates using if statements for decision-making based on user input and conditions.

#include <stdio.h>

int main() {
    int num;

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

    if (num > 0) {
        printf("The number is positive.\

n");
    } else if (num < 0) {
        printf("The number is negative.\n");
    } else {
        printf("The number is zero.\n");
    }

    if (num % 2 == 0) {
        printf("The number is even.\n");
    } else {
        printf("The number is odd.\n");
    }

    return 0;
}

Explanation:

  • The first if-else if-else block checks if num is positive, negative, or zero.
  • The second if-else block checks if num is even or odd.

Sample Output:

Enter a number: -3
The number is negative.
The number is odd.

The if statement in C provides flexible decision-making capabilities, allowing you to handle different scenarios based on conditions.

You may also like