Bootstrap demo

Conditional Statements in C Language

Complete Guide with Examples and Best Practices

Table of Contents

1. Introduction to Conditional Statements

Conditional statements in C programming allow you to execute different blocks of code based on specified conditions. They are fundamental for implementing decision-making logic in programs, enabling your code to respond dynamically to different inputs and situations.

2. Types of Conditional Statements

2.1 if Statement

Definition: The if statement executes a block of code only if a specified condition is true.

Syntax:

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

Example:

#include <stdio.h>

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

Output:

You are eligible to vote.

2.2 if-else Statement

Definition: The if-else statement executes one block of code if a condition is true and another block if it's false.

Syntax:

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

Example:

#include <stdio.h>

int main() {
    int number = 15;
    
    if (number % 2 == 0) {
        printf("%d is even.\n", number);
    } else {
        printf("%d is odd.\n", number);
    }
    
    return 0;
}

Output:

15 is odd.

2.3 if-else if ladder Statement

Definition: The if statement executes a block of code only if a specified condition is true.

Syntax:

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

Example:

#include <stdio.h>

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

Output:

You are eligible to vote.

2.4 Nested if-else

Definition: The if statement executes a block of code only if a specified condition is true.

Syntax:

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

Example:

#include <stdio.h>

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

Output:

You are eligible to vote.

2.5 Switch Statement

Definition: The if statement executes a block of code only if a specified condition is true.

Syntax:

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

Example:

#include <stdio.h>

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

Output:

You are eligible to vote.

2.6 Ternary Operator

Definition: The if statement executes a block of code only if a specified condition is true.

Syntax:

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

Example:

#include <stdio.h>

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

Output:

You are eligible to vote.

3. Comparison Operators

Operator Description Example
== Equal to x == y
!= Not equal to x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

4. Logical Operators

Operator Description Example
&& AND (a>b && a>c)
|| OR (a>b && a>c)
! NOT !(a>b)
Note : To learn more about operators go to Operator Page.

5. Best Practices

  • Use meaningful variable names for better readability
  • Always use braces {} even for single-line statements
  • Avoid deep nesting - refactor complex conditions
  • Use comments to explain complex conditions
  • Be consistent with indentation and formatting
  • Use the most appropriate conditional statement for each scenario

6. Common Mistakes

1. Using assignment (=) instead of equality (==) operator

if (x = 5) { } // Wrong
if (x == 5) { } // Correct

2. Forgetting break statements in switch cases

switch (x) {
    case 1: printf("One"); // Missing break
    case 2: printf("Two"); break;
}

7. Real-world Examples

Example 1: Simple Calculator

#include <stdio.h>

int main() {
    char operator;
    double num1, num2, result;
    
    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operator);
    
    printf("Enter two numbers: ");
    scanf("%lf %lf", &num1, &num2);
    
    switch (operator) {
        case '+':
            result = num1 + num2;
            break;
        case '-':
            result = num1 - num2;
            break;
        case '*':
            result = num1 * num2;
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
            } else {
                printf("Error: Division by zero!\n");
                return 1;
            }
            break;
        default:
            printf("Error: Invalid operator!\n");
            return 1;
    }
    
    printf("Result: %.2lf\n", result);
    return 0;
}