If else

C Programming Tutorial: if-else Statement

Welcome to the Code With Pankaj "if-else Statement in C Programming" tutorial! This tutorial will guide you through the usage of the if-else statement in C for decision-making.

Table of Contents


1. Introduction to if-else Statement

The if-else statement is used in C programming to execute a block of code based on a specified condition. It allows the program to make decisions and choose between alternative courses of action.

The flowchart illustrates the decision-making process of the if-else statement:

  1. The program evaluates a condition.

  2. If the condition is true, the code block within the if part is executed.

  3. If the condition is false, the code block within the else part is executed (if it exists).

  4. Regardless of which block is executed, the program continues with the code that follows the if-else construct.

2. Syntax of if-else Statement

if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}

3. Example: Simple if-else Statement

#include <stdio.h>

int main() {
    int num = 10;
    if (num > 0) {
        printf("Positive\n");
    } else {
        printf("Non-positive\n");
    }
    return 0;
}

4. Nested if-else Statement

#include <stdio.h>

int main() {
    int num = 10;
    if (num > 0) {
        if (num % 2 == 0) {
            printf("Positive and Even\n");
        } else {
            printf("Positive and Odd\n");
        }
    } else {
        printf("Non-positive\n");
    }
    return 0;
}

5. The else-if ladder

#include <stdio.h>

int main() {
    int num = 10;
    if (num > 0) {
        printf("Positive\n");
    } else if (num < 0) {
        printf("Negative\n");
    } else {
        printf("Zero\n");
    }
    return 0;
}

6. Ternary Operator as a Shortcut

The ternary operator (? :) provides a shortcut for the if-else statement when only simple conditional expressions are involved.

#include <stdio.h>

int main() {
    int num = 10;
    printf("%s\n", (num > 0) ? "Positive" : "Non-positive");
    return 0;
}

7. Best Practices

  • Use meaningful conditions to improve code readability.

  • Keep the code inside if-else blocks concise and clear.

  • Avoid deeply nested if-else structures to maintain code simplicity.

8. Exercises

Try these exercises to practice if-else statements in C:

  1. Exercise 1: Write a program to check if a number is even or odd.

  2. Exercise 2: Implement a program to determine if a year is a leap year or not.

  3. Exercise 3: Create a program to find the largest among three numbers.

  4. Exercise 4: Write a program to classify a triangle as equilateral, isosceles, or scalene based on its sides.

  5. Exercise 5: Implement a program to determine the grade of a student based on their marks.


We hope this tutorial has helped you understand the if-else statement in C programming. Practice with the exercises provided to reinforce your understanding. Happy coding!

For more tutorials, visit www.codeswithpankaj.com.

Last updated