Decision Making in C | What is Decision Making In C Programming

Share Your Love

Decision making specifies one or more conditions to be evaluated or tested by the program, if the statement is true then a particular condition is executed and if the statement is false then a particular statement is executed.

Structure Of Decision Making:

decision making statements
Decision Making Statements

There are different type of decision making statements provided in C, they are:

  • if statement
  • switch case
  • Conditional operator statement ( ? : )
  • go to statement

Let us explain them one by one.

If statement:

In this statement if the expression or the test case provided with the if statement is true then the condition inside the if block is executed. The if block is initialised with the if keyword.

Simple if Statement: 

Syntax:
if (test case)
{
//Statement to be executed if true
}

For example of if Statement:

#include<stdio.h>
int main()
{
int a=10,b=20;
if(b>a)
{
printf("B is greater than A");
}
return 0;
}

QNUI V5rImuLkxc794L

Credit Goes To Soham Malakar

Multiple if Statement:

Syntax:
if(test case)
{
//statement
}
if(test case)
{
//statement
}

For example if statement:

#include<stdio.h>
int main()
{
int a=20,b=20;
if(b>a)
{
printf("B is greater than A");
}
if(a==b)
{
printf("A is equal to B");
}
return 0;
}

VEg1hqNI0UB0 R7b4kwOlCKbT OMJjwfSctE mqD9jXg2OFp tYAUoLkOLGAGVMZjWtok9kd ACjtWy9iMQcMsFSIFWcF6SIGMX1nrKERToU LWiSOASaEz PWWmKasQf6ADgw

Credit Goes To Soham Malakar

If…..else statement in C Programming:

In this statement if the expression or the test case provided with the if statement is true then the condition inside the if block is executed and if the statement is false and else block is initiated, then the else block is executed.

The if block is initialized with the if keyword and the else block is initialized by else keyword.

Syntax Of if…..else:

Syntax:
If(test case)
{
//statement
}
else
{
//statement 
}

Example of if……else:

#include<stdio.h>
int main()
{
int a=20,b=10;
if(b>a)
{
printf("B is greater than A");
}
else
{
printf("A is greater than B");
}
return 0;
}

Credit Goes To Soham Malakar

Nested if…else ladder:

A nested if-else statement in C Programming means, one if a statement has an Inside if-else statement and else statement has an Inside if-else statement. It mostly used in case of taking multiple decisions.

Syntax of Nested if….else ladder:

if(test case)
{
if(test case)
{
//statement
}
else
{
//statement
}
}
else
{
//statement
} 

Example of Nested If….else Statement:

#include<stdio.h>
int main()
{
int a=10,b=20;
if(b>a)
{
if(b<50)
{
printf("B is less than 50");
}
else
{
printf("B is greater than 50");
}
}
else
{
printf("A is greater than B");
}
return 0;
}

hZUiRa3NVsXyT9PmIGULGnd11Rw9BcG1wHXd5Stqdv9SmwkNRFd7S6TXvzYtwHsnQLPs55ea5AOrsRBawo6NtEKV Y16n6AX5Kx GDnt1OoQO

Credit Goes To Soham Malakar

Else-if Ladder statement:

In this expression when there are multiple test expression to check we use the else-if ladder statement along with the if else block. This is initialized with the else-if keyword.

Syntax of Else-if Ladder Statement:

if(test case)
{	
//statement
}
else if(test case)
{
//statement
}
else if(test case)
{
//statement
}
else
{
//statement
}

Example Of Else-If Ladder Statement:

#include<stdio.h>
int main()
{
int a=20,b=20;
if(b>a)
{
printf("B is greater than A");
}
else if(a==b)
{
printf("A is equal to B");
}
else
{
printf("A is greater than B");
}
return 0;
}

kLaHILjYHgAduljWTCtRuBBcdJdmsiPrCQMUrQ m4rDZOxUplXx8XDhL246CvrIi ynpH6karLgJ3Mdg4pvHiJR8Hq CmPVuN9xweufd8ugK1iiwJeZt5vzjMIRjyW gLI nw

Credit Goes To Soham Malakar

NOTE: ELSE STATEMENT CANNOT TAKE ANY TEST CASES WHEREAS ELSEIF STATEMENTS CAN TAKE TEST CASES.

Switch Case:

This is a type of decision-making statement where u can check a condition among several other alternatives, yes u may think it can be done in if….else….elseif ladder but in case of switch case it is much easier.

Syntax of Switch Case:

switch(expression)
{
case 1:
//statement
break;
case 2:
//statement
break;
.
.
.
.
default:
//statement
}

Here after every case, we use break so that if the condition satisfies or results in true value then it should stop right at that moment and exits the switch case, so that the other cases are not executed. The default case does not have a break as it itself is the end of the switch case.

Example Of Switch Case:

#include<stdio.h>
int main()
{
int ch;
printf("Enter a number between 1 to 5 to print in words");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ONE");
break;
case 2:
printf("TWO");
break;
case 3:
printf("THREE");
break;
case 4:
printf("FOUR");
break;
case 5:
printf("FIVE");
break;
default:
printf("INVALID INPUT");
}
return 0;
}

Credit Goes To Soham Malakar

Conditional Operator Statement ( ? : )

This conditional operator is also known as the ternary operator which works on three operands. It can also be referred to as a short hand technique for decision making statement.

Syntax of Conditional Operator:

Data type variable = Expression1 ? Expression2 : Expression 3;

Example Of Condition Operator:

#include<stdio.h>
int main()
{
int a=20,b=20;
int res=(a>b)?a:b;
printf("The greatest value is = %d",res);
return 0;
}

Credit Goes To Soham Malakar

Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.

Share Your Love
Avatar photo
Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter