C Programs Tutorials | IT Developer
IT Developer

C Programming - C Decision Making



Share with a Friend

C Programming - C Decision Making

C Decision Making

In C programming, decision-making statements allow the program to execute different statements based on conditions. These statements evaluate an expression or condition and make decisions accordingly.

Types of Decision Making Statements in C

  1. if statement
  2. if-else statement
  3. else-if ladder
  4. switch statement
  5. ternary operator (? :)
  1. if Statement

The if statement evaluates a condition and, if the condition is true, executes the block of code inside the curly braces {}.

  • Syntax:

C

if (condition) {

    // Code to be executed if condition is true

}

  • Example:

C

int a = 10;

if (a > 5) {

    printf("a is greater than 5\n");

}

// Output: a is greater than 5

  1. if-else Statement

The if-else statement evaluates a condition and executes one block of code if the condition is true and another block if the condition is false.

  • Syntax:

C

if (condition) {

    // Code to be executed if condition is true

} else {

    // Code to be executed if condition is false

}

  • Example:

C

int a = 3;

if (a > 5) {

    printf("a is greater than 5\n");

} else {

    printf("a is less than or equal to 5\n");

}

// Output: a is less than or equal to 5

  1. else-if Ladder

The else-if ladder is used when you need to test multiple conditions. If the first condition is false, it checks the second condition, and so on.

  • Syntax:

C

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 all the above conditions are false

}

  • Example:

C

int a = 10;

if (a > 15) {

    printf("a is greater than 15\n");

} else if (a > 5) {

    printf("a is greater than 5 but less than or equal to 15\n");

} else {

    printf("a is less than or equal to 5\n");

}

// Output: a is greater than 5 but less than or equal to 15

  1. switch Statement

The switch statement is used to check a variable against a set of values. It is a cleaner alternative to a series of if-else if statements when you need to evaluate the same variable or expression.

  • Syntax:

C

switch (expression) {

    case value1:

        // Code to be executed if expression == value1

        break;

    case value2:

        // Code to be executed if expression == value2

        break;

    default:

        // Code to be executed if none of the cases match

}

  • Example:

C

int day = 3;

switch (day) {

    case 1:

        printf("Monday\n");

        break;

    case 2:

        printf("Tuesday\n");

        break;

    case 3:

        printf("Wednesday\n");

        break;

    case 4:

        printf("Thursday\n");

        break;

    case 5:

        printf("Friday\n");

        break;

    default:

        printf("Invalid day\n");

}

// Output: Wednesday

  • Note :
    • break is used to exit the switch block.
    • The default case is optional but is used when no cases match.
  1. Ternary Operator (? :)

The ternary operator is a shorthand for the if-else statement. It allows you to assign a value based on a condition in a concise way.

  • Syntax:

C

condition ? expression_if_true : expression_if_false;

  • Example:

C

int a = 10, b = 5;

int max = (a > b) ? a : b;  // If a > b, max gets the value of a, else b

printf("Max value: %d\n", max);  // Output: Max value: 10

Decision-Making Example:

C

#include <stdio.h>

int main() {

    int a = 10, b = 20, choice;

    printf("Enter your choice (1 for addition, 2 for subtraction): ");

    scanf("%d", &choice);

    if (choice == 1) {

        printf("Addition result: %d\n", a + b);

    } else if (choice == 2) {

        printf("Subtraction result: %d\n", a - b);

    } else {

        printf("Invalid choice\n");

    }

    return 0;

}

Output (example):

Enter your choice (1 for addition, 2 for subtraction): 1

Addition result: 30

Summary of Decision Making in C

  1. if statement: Executes a block of code if a condition is true.
  2. if-else statement: Executes one block of code if the condition is true, and another block if false.
  3. else-if ladder: Allows testing multiple conditions sequentially.
  4. switch statement: Selects one of many code blocks to execute based on the value of an expression.
  5. Ternary operator (? :): A concise way to implement a simple if-else statement.

Decision-making statements are fundamental to controlling the flow of a program based on conditions in C. They enable the program to make choices and execute the relevant code accordingly.