What is Decision making?

The ability to change the behavior of a piece of code which is based on certain information in the environment is known as conditional code flow.
 
if Statement
If statement is used to test a condition, if condition is true then the code inside the if statement is executed otherwise that code is not executed.
Syntax

if(Boolean_expression)
{
// Body of if
}

if statemente.g.

#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
clrscr();
if(i<=10){
printf(“value of i is less than or equal to 10”);
}
getch();
}

Output
value of i is less than or equal to 10
 
if else Statement
If Else is also used to test a condition, if condition is true then the code inside the if statement is executed otherwise else part is executed.
Syntax

if(Boolean_expression){
// Body of if statement
}else{
// Body of else statement
}

if else statement
 
e.g.

#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
clrscr();
if(i<=10){
printf(“value of i is less than or equal to 10”);
}
else
{
printf(“value of i is greater than 10”);
}
getch();
}

Output
value of i is less than or equal to 10
 
 if…else if…else Statement
After if statement else if is used to check the multiple conditions.
Syntax

if(Boolean_expression1){
// Body of if statement
 
}
else if(Boolean_expression2){
//Body of else if
}
else{
// Body of else statement
}

e.g.

#include<stdio.h>
#include<conio.h>
void main(){
int i=10;
clrscr();
if(i<10){
printf(“value of i is less than 10”);
}
else if(i==10)
{
printf(“value of i is equal to10”);
}
else
{
printf(“value of i is greater than 10”);
}
getch();
}

Output
value of i is equal to10
 
Nested If Statement
It contains multiple if else condition. It is used to check the multiple conditions. This statement is like executing an if statement inside an else statement.
Syntax

if(Boolean_expression1){
//Body of if statement
if(Boolean_expression2)
{
//Body of nested if
}
}
…
else {
//Body of else statement
}

nestedif statemente.g.

#include<stdio.h>
#include<conio.h>
void main(){
int i = 20;
int j = 10;
if ( i < 30 ){
printf ("Value of i is less than 30");
if ( j == 10 ){
printf ("Value of j is equal to 10");
}
}
else
{
printf ("Value of i is not less than 30");
}
getch();
}

Output
Value of i is less than 30
Value of j is equal to 10
 
Switch Statement
It contains a number of cases with different conditions. When a variable value is matched with the case, then that case is executed.
Syntax

switch(expression){
case value1 :
//Statements
break;
case value2 :
//Statements
break;
case valuen :
//Statements
break;
default : //Optional
//Statements
}

switch

Recommended Videos

Leave a Reply

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