Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering C Conditional Statements: If, Else, and More

Mastering C Conditional Statements: If, Else, and More

This comprehensive guide walks you through the fundamentals of C’s if, else, and nested conditional statements, complete with clear examples and best‑practice tips.

Video: C if‑else Statement

Watch the concise tutorial below to see these concepts in action.

C if Statement

The if statement in C follows this syntax:

if (test expression) {
    // code to execute when the condition is true
}

How the if Statement Works

The test expression inside parentheses is evaluated. If the result is non‑zero (true), the block of code inside the braces executes; if zero (false), it is skipped.

Mastering C Conditional Statements: If, Else, and More

Learn more about truthy and falsy values in C’s relational and logical operators on Wikipedia or GeeksforGeeks.


Example 1: Simple if Statement

// Program to display a number if it is negative
#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");
    return 0;
}

Output 1

Enter an integer: -2
You entered -2.
The if statement is easy.

When the user enters -2, the expression number < 0 evaluates to true, so the message prints.

Output 2

Enter an integer: 5
The if statement is easy.

With 5, the expression evaluates to false, skipping the printf inside the block.


C if…else Statement

Adding an else block lets you specify alternate code for the false case. Syntax:

if (test expression) {
    // code when true
}
else {
    // code when false
}

How the if…else Statement Works

• If the test is true: executes the if block and skips else. • If the test is false: executes the else block and skips if.

Mastering C Conditional Statements: If, Else, and More

Example 2: Check Odd or Even

// Check whether an integer is odd or even
#include <stdio.h>
int main() {
    int number;
    printf("Enter an integer: ");
    scanf("%d", &number);

    // True if the remainder is 0
    if (number % 2 == 0) {
        printf("%d is an even integer.", number);
    }
    else {
        printf("%d is an odd integer.", number);
    }
    return 0;
}

Output

Enter an integer: 7
7 is an odd integer.

Here the modulus operator determines the parity, and the else block handles the odd case.


C if…else Ladder

When multiple conditions must be evaluated sequentially, an if…else if…else ladder is ideal.

Syntax of the Ladder

if (condition1) {
    // statements
}
else if (condition2) {
    // statements
}
else if (condition3) {
    // statements
}
else {
    // statements
}

Example 3: Relate Two Integers

// Program to compare two integers
#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 == number2) {
        printf("Result: %d = %d", number1, number2);
    }
    else if (number1 > number2) {
        printf("Result: %d > %d", number1, number2);
    }
    else {
        printf("Result: %d < %d", number1, number2);
    }
    return 0;
}

Output

Enter two integers: 12 23
Result: 12 < 23

Nested if…else

Embedding an if…else inside another allows fine‑grained branching without an extensive ladder.

Example 4: Nested Comparison

#include <stdio.h>
int main() {
    int number1, number2;
    printf("Enter two integers: ");
    scanf("%d %d", &number1, &number2);

    if (number1 >= number2) {
        if (number1 == number2) {
            printf("Result: %d = %d", number1, number2);
        }
        else {
            printf("Result: %d > %d", number1, number2);
        }
    }
    else {
        printf("Result: %d < %d", number1, number2);
    }
    return 0;
}

If an if…else block contains only one statement, braces {} are optional, but using them enhances readability and reduces errors.

Example:

if (a > b)
    printf("Hello");
printf("Hi");

C Language

  1. Mastering Conditional Logic in C#: If, If‑Else, If‑ElseIf, and Nested If Statements
  2. Mastering the C# Switch Statement: Syntax, Examples & Best Practices
  3. C# Break Statement: How & When to Use It
  4. Mastering the C# Continue Statement: How to Skip Loop Iterations Effectively
  5. Master C++ Conditional Statements: if, if...else, and Nested if...else Explained
  6. Mastering the C++ break Statement
  7. Mastering the C Switch Statement: Syntax, Flow, and Practical Example
  8. Mastering the C Goto Statement: Syntax, Practical Use, and Best Practices
  9. Mastering Python If…Else Statements
  10. Mastering Java if…else: Control Flow Explained