Decision Making in C++

Decision Making in C++

Decision making is about deciding which statement should be executed and which statement should be skipped from execution  based on certain conditions

Simple if-else

Syntax

if(test_ condition) //if block
{ 
True_stmts; 
} 
else //else block 
{ 
False_stmts; 
}
  • If the test condition is true, statements under if block gets executed
  • If the test condition is false,  statements under else block will get executed

C++ program to demonstrate if_else usage

main()
{
int n;
cout<<"enter a num:"; 
cin>>n; if(n%2==0)//4%2==0->0==0 gets true { cout<<"\n"<<n<<"is even"; } else { cout<<"\n"<<n<<"is odd"; //5%2==0->1==0 gtes false hence else executes } }
Decision making in C++

O/P

Enter a num :4 
4 is even

Enter a num:5 
5 is odd

Multiple if statements

The program may contain any number of if statements

C++ program to find the maximum of four numbers

main()
{

int a,b,c,d,max;
cout<<"enter 4 numbers:";
cin>>a>>b>>c>>d;

max=a;//compare max with the remaining three

if(max<b)//if b is greater than a, then b is  max from onwards
max=b;

if(max<c)
max=c;

if(max<d)
max=d;

cout<<"\n max among the 4 is "<<max;
}

O/P

enter 4 numbers 
1 2 3 4
max among 4 is 4

Nested if statements

A program can contain if-else construct  inside an if- block ok or an else block

C++ program to demonstrate nested if

main()
{

int x=20,y=30;

if(x==20)
{

if(y==30)//if inside an if
{
cout<<"value of x is 20, and value of y is 30";
}

}

}

if x==20 satisfies then only control goes to the if(y==30) if that is true then only statement prints otherwise empty O/P screen

Else_if Ladder

  • Generally when if condition fails statements under else block get executed, but instead of  transferring the control immediately to else block, if you want to check some more conditions after if block and if all those conditions fail then only execute else block at the end
  • This is the mechanism of else if ladder

Syntax:

if(test1)
{
---;
}
else if(test 2)
{
---;
}
.....
else if(test n)
{
----;
}
else
{
---;
}

According to syntax, if at the top and else at the bottom is mandatory in the case of else -if ladder

Execution flow

  • If test condition inside if gets true then if block gets executed and all other else if blocks and else block is skipped from execution
  • If test condition inside if gets false then condition inside else if gets checked, if it is true, that else if block gets executed and all the remaining [if, else if and else ] block gets skipped
  • If, [if & all the else- if blocks ] conditions fail then finally else get executed at the end
  • Among [if, n else ifs, else] only single block i.e if block, any one of the  else if block or else block gets executed

C++ program to check whether the given character is uppercase, lowercase, digit all special character

main()
{
char key;
cout<<"enter a char:";
cin>>key;

if(key>='A'&&key<='Z')//test for uppercase
cout<<"\nUPPERCSE";

else if(key>='a'&&key<='z')//test for lowercase
cout<<"\nLOWER CASE";

else if(key>=48&&key<=57)//test for digit
cout<<"\nDIGIT";

else//if it is not the above three then definitely a special char
cout<<"SPECIAL_CHARECTER";
}

O/P

enter a char
A
UPPERCASE

enter a char
9
DIGIT

enter a char
$
SPECICAL_CHARECTER

Points to Remember

  • Other than zero any value  is considered as true
if(66) //true always 
{
cout<<"trishaank";
}
  • Inappropriate use of = operator instead of == (comparison ) operator
main() 
{
int a=2;
if(a=3) //always true
{
cout<<"trishaank\n";
}
else
{
cout<<"rishvik";
}
}

O/P

trishaank

Here we have written as a=3 instead of a==3 now comparison is not performed directly  3 is returned i.e if(3) and we know that other than zero any number is considered as true. hence irrespective of the condition always if block executes

  • You cannot break the if-else chain i.e independent statements in between if and else will raise an error
main() 
{
int a=3;
if(a==3)
{
cout<<" trishaank";
}
//cout<<"I hate engineering"; //error
else
{
cout<<" rishwik";
}
}

O/P

Compiler error: Else without a previous if

Please Login/Signup to comment