Lab Manual 05 - Decision Control Structure and Switch Statement
Lab Manual 05 - Decision Control Structure and Switch Statement
Lab- 05 Manual
Lab Instructor: Riaz Ahmad
Department of Computer Science
Email: riazahmad@umt.edu.pk
Lab 04: Decision Control Structures (if else, else if) and Switch
Table of Contents
Table of Contents............................................................................................................................1
5.1 Objective.................................................................................................................................................2
5.2 Scope......................................................................................................................................................2
5.3 Useful concepts
IfStatement:...........................................................................................................................................2
If-else statements............................................................................................................................................2
else...............................................................................................................................................................2
Multiple if-else-if statements.........................................................................................................................3
Switch statement:...........................................................................................................................................3
5.4 Examples:..............................................................................................................................................3
5.5 Exercise for Lab....................................................................................................................................9
5.6 Home Task.............................................................................................................................................9
Lab Rubrics
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.
statement;
…
}
Next statement;
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.
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.1 Examples:
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<<"THANK
YOU."<<endl;
return 0;
}
int main()
{
int marks,total,percent=0;
#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 5.4: 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;
}
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;
}
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.
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
# include <iostream>
using namespace std;
int main()
{
int grade, // one grade
aCount = 0, // number of A's
bCount = 0, // number of B's
cCount = 0, // number of C's
dCount = 0, // number of D's
fCount = 0; // number of F's
break;
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.
Press 1 to calculate
percentage. Press 2 to
display grade.
If the user presses 1 then percentage should be displayed and if the user presses 2 the
grade against the marks should be displayed. (Hint: use switch statement for menu
selection and else if to display the grade).
2. Prompt the user to enter 3 values. For any equal values, the program should display the
numbers that are equal. (For example user input 34,6,34 the program should display the
message that the 1st and 3rd values are equal).