C Programming Tutorial

C Programming Tutorial

Decision Making in C Programming

In this article, we will explore the Decision Making in C programming. You will learn how to use different control structures like if statements and switch cases to make your programs respond to various conditions. Whether you’re new to programming or looking to strengthen your understanding, this guide will help you grasp the basics of Decision Making in C Programming.

Decision making is an essential part of any program, including those written in C programming language. In C, decision-making is done using conditional statements, which allow the program to execute specific code based on certain conditions.

The two most common conditional statements in C are the “if” statement and the “switch” statement.

The “if statement” is used to execute a block of code if a certain condition is true. Here is an example:

int x = 10;
if (x > 5) {
    printf("x is greater than 5\\n");
}

In this example, the code inside the curly braces will only be executed if the condition in the parentheses is true. Since x is greater than 5, the message “x is greater than 5” will be printed.

The “switch statement” is used to execute different blocks of code depending on the value of a variable. Here is an example:

int day = 3;
switch (day) {
    case 1:
        printf("Monday\\n");
        break;
    case 2:
        printf("Tuesday\\n");
        break;
    case 3:
        printf("Wednesday\\n");
        break;
    default:
        printf("Invalid day\\n");
        break;
}

In this example, the code will execute the block of code associated with the case that matches the value of the variable “day”. In this case, since “day” is equal to 3, the message “Wednesday” will be printed.

The “default” statement is executed if none of the cases match the value of the variable.

Overall, decision making is an important part of programming in C, as it allows programs to make intelligent choices based on specific conditions.

Share This Article