Decision Control Structure (2)
Decision Control Structure (2)
5.1 Objective
The student should practice the following statements:
1. if statements
2. if-else statements
3. switch statement
5.2 Scope
By the end of this lab a student should know:
1. Syntax of if and if-else statements.
2. Syntax of switch statements.
Here, the condition is any expression that returns a Boolean value. If there is only a single
statement following the if-clause, the use of braces is optional. However, a compound statement cannot
be used without curly braces.
If-else statements
In case you want to have an alternative action to be taken if the condition is not satisfied, then use
an if-else statement. The general form is:
if (condition)
Statement1;
else
Statement2;
Here, statement may be a single statement or a compound statement enclosed in curly braces (i.e.,
a block). The condition is any expression that results in a Boolean value.
Multiple if-else-if statements
The if-else-if statement can be used to choose one block of statements from many blocks of statements. It
is used when there are many options and only one block of statements should be executed on the basis of
a condition.
Switch statement:
Another useful statement in C++ is the switch statement. This statement is somehow similar to if
statement in giving you multiple options and do actions accordingly. But its behaviour is different. This
statement tests whether an expression matches one of a number of constant integer values labelled as
cases. If one matches the expression, execution starts at that case. This is the general structure of the
switch statement:
switch (expression)
{
case constant: statement(s);
break;
case constant: statement(s);
break;
case constant: statement(s);
break;
default: statement(s);
}
The default clause is optional. If it is not there and none of the cases matches, no action is taken. The
break keyword is used to skip the switch statement. For example if a case matches the expression and no
break key words are used, the execution will go for all the statements in all cases.
5.4 Examples:
#include<iostream>
using namespace std;
int main()
{
int marks,total,percent=0;
cout<<"Enter the marks obtained in PF:";
cin>>marks;
cout<<"Enter the total marks:";
cin>>total;
percent = (marks*100)/total;
if (percent >= 50)
{
cout<<"Congragulations you have passed the subject."<<endl;
cout<<"WELL DONE!"<<endl;
}
else
{
cout<<"Sorry you could not pass the course."<<endl;
cout<<"Better luck next time. "<<endl;
}
cout<<"THANK YOU."<<endl;
return 0;
}
Example: Input a number from user and display whether it is a positive or negative number
#include<iostream>
using namespace std;
int main()
{
int input;
cout<<"Enter a number:";
cin>>input;
if (input>0)
cout<<"A positive number was entered."<<endl;
cout<<"Thank You."<<endl;
return 0;
}
Output for the program might be one of the following:
Example 5.5: Input five values from the user and display the maximum number from the list.
#include<iostream>
using namespace std;
int main()
{
int a,b,c,d,e;
cout<<"enter 1st number: ";
cin>>a;
cout<<"enter 2nd number: ";
cin>>b;
cout<<"enter 3rd number: ";
cin>>c;
cout<<"enter 4th number: ";
cin>>d;
cout<<"enter 5th number: ";
cin>>e;
if(a>b && a>c && a>d && a>e)
cout<<a<<" is the greatest"<<endl;
else if(b>a && b>c && b>d && b>e)
cout<<b<<" is the greatest"<<endl;
else if(c>a && c>b && c>d && c>e)
cout<<c<<" is the greatest"<<endl;
else if(d>a && d>b && d>c && d>e)
cout<<d<<" is the greatest"<<endl;
else if(e>a && e>b && e>c && e>d)
cout<<e<<" is the greatest"<<endl;
return 0;
}
The output for this program is:
Example 5.6: Prompt the user to enter the salary and grade of an employee. If the employee has a grade
greater than 15 then add 50% bonus to the employee’s salary. Otherwise if the employee’s grade is less
than 15 then add 25% bonus to the employee’s salary.
#include<iostream>
using namespace std;
int main()
{
int grad;
double sal,bonus;
cout<<"Enter salary :";
cin>>sal;
cout<<"Enter grade :";
cin>>grad;
if (grad > 15)
{
bonus = sal*(50.0/100.0);
cout<<"Total salary with 50 percent bonus:"
<<(bonus+sal)<<endl;
}
else if (grad <= 15)
{
bonus = sal* (25.0/100.0);
cout<<"Total salary with 25 percent bonus:"
<<(bonus+sal)<<endl;
}
return 0;
}
Here each condition is tested one by one starting from the first one. If one of them is true, then the
statement associated with that condition is executed and the rest are ignored.
Example 5.7: Demonstration of switch statement.
#include<iostream>
using namespace std;
int main() {
char grade;
cout<<"Enter a grade in capital letters: ";
cin>>grade;
switch(grade)
{
case 'A':
cout<<"You have scored 90% marks."<<endl;
break;
case 'B':
cout<<"You have scored 80% marks."<<endl;
break;
case 'C':
cout<<"You have scored 70% marks."<<endl;
break;
case 'D':
cout<<"You have scored 60% marks."<<endl;
break;
case 'E':
cout<<"You have scored less than 50%."<<endl;
}
return 0;
}
Exercise 5.1: Prompt the user to input 5 values and display the minimum number amongst them.
Exercise 5.2: Input five values through the user and display the number of positives, the number of
negatives and the number of zeros amongst the 5 values.
Exercise 5.3: Prompt the user to input a character and display whether it is a vowel or consonant using
switch statement.