C Programming Language Tutorial

Variables and Data Types

Input/Output

Looping and Selection Structures

Array

Functions

Preprocessing Command

Pointer

Structure

File Operations

Important Knowledge

if else Statements in C Programming Language

In this tutorial, we will learn about the if, else if, and else statements in C programming language. These statements are used for making decisions in your code based on the conditions you specify.

1. if statement:

The if statement is used to execute a block of code if a specified condition is true. The syntax for the if statement is:

if (condition) {
  // Code to be executed if the condition is true
}

Here's an example:

#include <stdio.h>

int main() {
  int age = 18;

  if (age >= 18) {
    printf("You are eligible to vote.\n");
  }

  return 0;
}

2. if...else statement:

The if...else statement is used to execute one block of code if the specified condition is true, and another block of code if the condition is false. The syntax for the if...else statement is:

if (condition) {
  // Code to be executed if the condition is true
} else {
  // Code to be executed if the condition is false
}

Here's an example:

#include <stdio.h>

int main() {
  int age = 16;

  if (age >= 18) {
    printf("You are eligible to vote.\n");
  } else {
    printf("You are not eligible to vote.\n");
  }

  return 0;
}

3. if...else if...else statement:

The if...else if...else statement is used when you need to test multiple conditions and execute different blocks of code based on which condition is true. The syntax for the if...else if...else statement is:

if (condition1) {
  // Code to be executed if condition1 is true
} else if (condition2) {
  // Code to be executed if condition2 is true
} else {
  // Code to be executed if none of the conditions are true
}

Here's an example:

#include <stdio.h>

int main() {
  int grade = 75;

  if (grade >= 90) {
    printf("You got an A.\n");
  } else if (grade >= 80) {
    printf("You got a B.\n");
  } else if (grade >= 70) {
    printf("You got a C.\n");
  } else if (grade >= 60) {
    printf("You got a D.\n");
  } else {
    printf("You got an F.\n");
  }

  return 0;
}

In summary, if, else if, and else statements are used to make decisions in your code based on specified conditions. By using these statements, you can control the flow of your program and execute different blocks of code based on the given conditions.

  1. Conditional Branching with if-else in C Language:

    #include <stdio.h>
    
    int main() {
        int number = 10;
    
        // If-else statement
        if (number > 0) {
            printf("Positive\n");
        } else {
            printf("Non-positive\n");
        }
    
        return 0;
    }
    

    The if-else statement allows the execution of different code blocks based on a specified condition.

  2. Nested if-else Statements in C Programming:

    #include <stdio.h>
    
    int main() {
        int number = 0;
    
        // Nested if-else statements
        if (number > 0) {
            printf("Positive\n");
        } else if (number < 0) {
            printf("Negative\n");
        } else {
            printf("Zero\n");
        }
    
        return 0;
    }
    

    Nested if-else statements allow handling multiple conditions based on the evaluation of each.

  3. C Code Examples Demonstrating if-else Usage:

    #include <stdio.h>
    
    int main() {
        int marks = 75;
    
        // if-else usage example
        if (marks >= 60) {
            printf("Pass\n");
        } else {
            printf("Fail\n");
        }
    
        return 0;
    }
    

    The if-else statement is commonly used for decision-making in various scenarios, such as grading in this example.

  4. Using Logical Operators with if-else in C:

    #include <stdio.h>
    
    int main() {
        int age = 25;
        char gender = 'M';
    
        // Using logical operators with if-else
        if (age >= 18 && gender == 'M') {
            printf("Adult male\n");
        } else {
            printf("Not an adult male\n");
        }
    
        return 0;
    }
    

    Logical operators (&&, ||, !) can be used to combine multiple conditions in an if-else statement.

  5. Switching to Ternary Operator vs if-else in C:

    #include <stdio.h>
    
    int main() {
        int number = 10;
    
        // Ternary operator vs if-else
        (number % 2 == 0) ? printf("Even\n") : printf("Odd\n");
    
        return 0;
    }
    

    The ternary operator (condition ? expr1 : expr2) provides a concise way to express a simple if-else statement.

  6. Error Handling with if-else Statements in C:

    #include <stdio.h>
    
    int main() {
        int dividend = 20;
        int divisor = 0;
    
        // Error handling with if-else
        if (divisor != 0) {
            int result = dividend / divisor;
            printf("Result: %d\n", result);
        } else {
            printf("Error: Division by zero\n");
        }
    
        return 0;
    }
    

    if-else statements are used for error handling, ensuring that certain conditions are met before executing specific code.

  7. Common Mistakes with if-else in C Programming:

    #include <stdio.h>
    
    int main() {
        int x = 5;
    
        // Common mistake: missing braces for code blocks
        if (x > 0)
            printf("Positive\n");
            printf("This line always executes\n");
    
        return 0;
    }
    

    A common mistake is forgetting to use braces for code blocks, leading to unintended behavior.