C Decision Making

Decision making is an important aspect of programming, and it is essential to know how to control the flow of the program based on certain conditions or inputs. In C programming, decision making is primarily done using conditional statements and loops. In this article, we will discuss decision making in C programming and cover the following topics:

Conditional statements

The if statement

The if-else statement

The nested if statement

The switch statement

.

The if statement:

The if statement is the most basic conditional statement in C programming. It allows us to execute a block of code if a certain condition is true. The syntax for the if statement is as follows:

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

The condition can be any expression that evaluates to true or false. If the condition is true, the code inside the curly braces will be executed. If the condition is false, the code inside the curly braces will be skipped.

The if-else statement:

The if-else statement allows us to execute one block of code if a certain condition is true, and another block of code if the condition is false. The syntax for the if-else statement is as follows:

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

If the condition is true, the code inside the first set of curly braces will be executed. If the condition is false, the code inside the second set of curly braces will be executed.

The nested if statement:

The nested if statement allows us to use one if statement inside another. This is useful when we want to check multiple conditions. The syntax for the nested if statement is as follows:

if (condition1)
{
   if (condition2)
   {
      // code to be executed if both conditions are true
   }
   else
   {
      // code to be executed if condition1 is true and condition2 is false
   }
}
else
{
   // code to be executed if condition1 is false
}

In this example, the inner if statement is nested inside the outer if statement. If both conditions are true, the code inside the inner set of curly braces will be executed. If only the first condition is true, but the second condition is false, the code inside the else block will be executed.

The switch statement:

The switch statement is another way to perform decision making in C programming. It allows us to execute a block of code based on the value of a variable. The syntax for the switch statement is as follows:

switch (variable)
{
   case value1:
      // code to be executed if variable equals value1
      break;
   case value2:
      // code to be executed if variable equals value2
      break;
   ...
   default:
      // code to be executed if variable does not equal any of the values
      break;
}

In this example, the variable is checked against each value in the case statements. If the variable matches one of the values, the code inside that case statement will be executed. If the variable does not match any of the values, the code inside the default block will be executed.