Decision making statements in c

Decision making statements in c (if, else, if-else, switch case statements)

Social Shares
  •  
  •  
  •  
  • 3
  •  
  •  
  •  
  •  

Decision making statements in c language

You can decide which statements you want to execute in your program and which statements you want to skip. It is called decision making statements in c language. Most decision making is done on a conditional basis.
If you come to a particular condition, you can execute the statements you want. For this, you use some built-in statements. Because these statements work with the conditions, they are also called conditional statements. And because they control execution in the statement program, they are also called control statements.

Before knowing about these statements, let’s try to understand decision making statements in c language with an example.

Suppose you want to print the name of any of the two students whose age is high. How can you do this? See the program below.

 

#include <stdio.h>
#include <conio.h>
void main()
{
    raviAge = 40;
    ankitAge = 32;
}

In the above example, 2 students have been stored in age 2 variables. But you only have to print the age of the student whose age is the highest. In this situation, you need to take a decision. You can do this by any decision making statements in c language.

There are 3 decision making statements in c language are as follow

  1. The If Statement
  2. The If else statement.
  3. Nested If else statements.
  4. Switch Case statement.

Let’s try to know about these statements by detail.

If Statement.

The If statement is the most popular decision making statements in c language. Here is the general syntax is given below.
if(condition)
{
    // statements
}
Decision making statements in c language

If statement defines a block by curly braces {}. ωhen the condition is true then the statement given in this block is executed. If the condition is false then skip this entire block compiler. Îf using the if statement in the above example, the program can be written as such.

#include <stdio.h>
int main()
{
   int raviAge = 40;
   int ankitAge = 32;
  if(raviAge > ankitAge)
  {
     printf(“Ravi is elder.”);

  }

return 0;

}

In the above example, a condition is applied while using conditional operator. If the age of Ravi is greater than Ankit then the printf () statement given in the if statement will execute. But if not, then this statement will not be executed. This program will generate the output given below.

Ravi is elder.

 

If-Else Statement

If else statement is considered to be part of an if statement in decision making statements in c language. But else block and add is done in it. The statement given in Else block is executed when the condition of if is false. The general syntax of the if-else statement is given below.

if(condition)
{
    //statements
}
else
{
     //statements
}

 

Decision making statements in c language

 

As you know if the statements given in the if a block is executed on if the condition is true. But you can also decide what to do if the condition is false. For this, you use else block. This block always comes after the if block. These statements are written in this block, which will be executed if the condition is false. If “if else statement” is used in the above example then you can write it like this.

#include <stdio.h>
void main()
{
    int raviAge = 20;
    int ankitAge = 32;
   if(raviAge > ankitAge)
   {
        printf(“Ravi is elder”);
   }
   else
   {
         printf(“Ankit is elder”);
   }
}

The above program produces the below given output.

Ankit is elder.

 

else if

If you want to put another condition between if and else, then you can do it by defining another if block. this is rarely used for decision making statements in c language

if(raviAge > ankitAge)
{
    printf(“Ravi is elder”);
}
else if(ankitAge > raviAge)
{
    printf(“Ankit is elder”);
}
else
{
    printf(“Both are equal”);
}

youtubeembedcode.com
Top 10 Geeks

Nested If else statements

Nested if statements are also similar to if else statement but in Nested if statements, the second statement can be started within a statement.

With the help of an example, you will better understand Nested if statements.

if ( age < 18 )
{
    printf(“You are Minor.\n”);
    printf(“Not Eligible to Work”);
}
else
{
    if (age >= 18 && age <= 60 )
     {
       printf(“You are Eligible to Work \n”);
   printf(“Please fill in your details and apply\n”);
      }
   else
     {
       printf(“You are too old to work as per the Government rules\n”);
   printf(“Please Collect your pension! \n”);
     }
}

In the above example we have written a second if else statement inside an if statement, this method is called Nested if statements.

Switch case Statement

The last decision making statements in c language is a switch case statement is also similar to if statement but it is used to check case instead of checking the condition. When you come to a particular case, you write the statements that you want to execute inside the case.

A case is matched to an integer variable. The same case goes to the execute case which matches the integer variable

The general syntax of the switch statement is given below.

int caseNumber = n;
switch(caseNumber)
{
case 1:
             //statements to be executed
             break;
case 2:
             //statements to be executed
             break;
default:
              //statements to be executed
              break;
}

 

Decision making statements in c language

 

When you set the caseNumber variable with an integer value and pass it in the switch case statement, the same number that matches this number will be executed.

For example, if you have passed 2 in case number then the case of the second number will be executed and all the statements before the break will be executed.

If no case matches, then default case is executed. If after each case the break statement is used then all the cases will be executed. You can also define the case with alphabets.

Let’s try to understand the switch statement through an example.

#include <stdio.h>

void main()
{
int caseNumber;
printf(“Enter a number “);
scanf(“%d”,&caseNumber);
switch(caseNumber)
{
     case 1:
                  printf(“\nFirst Case executed….”);
                  break;
     case 2:
                  printf(“\nSecond Case executed….”);
                  break;
     default:
                  printf(“\nNone of the case matched. You can only enter 1 or 2”);
                  break;
}
}

In the above example, a number is being entered from the user and matching case on the base of that number is being executed. This program generates the given output below.

Enter a number :
2
The second case executed…

Social Shares
  •  
  •  
  •  
  • 3
  •  
  •  
  •  
  •  

4 comments

Leave a Reply

Your email address will not be published. Required fields are marked *