Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lab Manual 05 - Decision Control Structure and Switch Statement

The Lab Manual for Programming Fundamentals at the University of Management and Technology covers decision control structures including if, if-else, and switch statements. It provides objectives, useful concepts, examples, exercises, and home tasks to enhance students' understanding of these programming structures. The manual aims to equip students with the necessary skills to implement these concepts in programming.

Uploaded by

rajabfan555
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab Manual 05 - Decision Control Structure and Switch Statement

The Lab Manual for Programming Fundamentals at the University of Management and Technology covers decision control structures including if, if-else, and switch statements. It provides objectives, useful concepts, examples, exercises, and home tasks to enhance students' understanding of these programming structures. The manual aims to equip students with the necessary skills to implement these concepts in programming.

Uploaded by

rajabfan555
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab Manual: Programming Fundamentals

University of Management and Technology,


Lahore Campus

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

Department of Computer Science, UMT, Lahore. 1 Riaz Ahmad


Lab Manual: Programming Fundamentals

Lab Rubrics

Identify key programming


Areas Problem Understanding structures such as Design/ Development of Solutions
Assessed variables, loops, and
functions.
(CLO 1) (CLO 2) (CLO 3)
No able to identify key
Not able to understand Not able to Model a solution for a given
Poor programming structures such
Problem problem
as variables, loops, and
functions.

Partially able identify Modeled an average solution for


Able to partially understand
Average key programming a given problem using
the problem
structures such as programming principles
variables, loops, and
functions.

Thoroughly identify key


Modeled a proper solution for a given
Very Good Fully Understand the problem programming structures
problem using programming principles
such as variables, loops,
and functions.

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.

5.3 Useful concepts


If Statement:
The if statement allows your program to execute a single statement or a block of
statements
enclosed between braces if a given condition true. The general form for single selection structure is:
if (condition){

Department of Computer Science, UMT, Lahore. 2 Riaz Ahmad


Lab Manual: Programming Fundamentals

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.

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;

Department of Computer Science, UMT, Lahore. 3 Riaz Ahmad


Lab Manual: Programming Fundamentals

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:

Example 5.1: Demonstration of if statement. You want to display a


congratulatory message in case a student has passed a course. The passing marks
are 50%.
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<<"THANK
YOU."<<endl;

return 0;
}

Output for the above program is

Example 5.2: Demonstration of if statement.


#include<iostream> using
namespace std;

int main()
{
int marks,total,percent=0;

Department of Computer Science, UMT, Lahore. 4 Riaz Ahmad


Lab Manual: Programming Fundamentals

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;
}
cout<<"THANK YOU."<<endl;
return 0;
}

Output for the above program is

Example 5.3: Demonstration of if-else statement.

Department of Computer Science, UMT, Lahore. 5 Riaz Ahmad


Lab Manual: Programming Fundamentals

#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;
}

Output for the program might be one of the following

Department of Computer Science, UMT, Lahore. 6 Riaz Ahmad


Lab Manual: Programming Fundamentals

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:

Department of Computer Science, UMT, Lahore. 7 Riaz Ahmad


Lab Manual: Programming Fundamentals

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;
}

The output of this program is

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.

Department of Computer Science, UMT, Lahore. 8 Riaz Ahmad


Lab Manual: Programming Fundamentals

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;
case 'B': break;

cout<<"You have scored 80%


case 'C': marks."<<endl;
break;

case 'D': cout<<"You have scored 70%


marks."<<endl;
break;
case 'E':
cout<<"You have scored 60%
marks."<<endl;
case 'F': break;

cout<<"You have scored less than


50%."<<endl;
default:
break;

cout<<"You have entered an invalid


}
grade."<<endl;
return 0;
}
break;

Output for this


program is shown
below:

Example 5.8: Basic Calculator using Switch statement.

Department of Computer Science, UMT, Lahore. 9 Riaz Ahmad


Lab Manual: Programming Fundamentals

Example 7: Basic Calculator using Switch statement.


# include <iostream>
using namespace std;
int main()
{
char op;
float num1, num2;
cout << "Enter operator: +, -, *, /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op)
{
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;

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;
}

Example 5.9: Count grades using Switch statement.

Department of Computer Science, UMT, Lahore. 10 Riaz Ahmad


Lab Manual: Programming Fundamentals

# 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

cout << "Enter the letter grades." << endl


<< "Enter the EOF character to end input." << endl;

while ( ( grade = cin.get() ) !='1' ) {

// EOF is system dependent. Try CTRL-Z

switch ( grade ) { // switch nested in while

// case and default need a colon :,


// not a semicolon.

case 'A': // grade was uppercase 'A'


case 'a': // or lowercase 'a'
++aCount;
break; // necessary to exit switch

case 'B': // grade was uppercase 'B'


case 'b': // or lowercase 'b'
++bCount;
break;

case 'C': // grade was uppercase 'C'


case 'c': // or lowercase 'c'
++cCount;
break;

case 'D': // grade was uppercase 'D'


case 'd': // or lowercase 'd'
++dCount;
break;

case 'F': // grade was uppercase 'F'


case 'f':// or lowercase 'f'
++fCount;

Department of Computer Science, UMT, Lahore. 11 Riaz Ahmad


Lab Manual: Programming Fundamentals

break;

// for additional protection

case '\n': // ignore newlines,


case '\t': // tabs,
case ' ': // and spaces in input
break;

default: // catch all other characters

cout << "Incorrect letter grade entered."


<< "Enter a new grade." << endl;
break; // optional
}
}
cout << "\n\nTotals for each letter grade are:"
<< "\nA: " << aCount
<< "\nB: " << bCount
<< "\nC: " << cCount
<< "\nD: " << dCount
<< "\nF: " << fCount << endl;
return 0;
}

5.1 Exercise for Lab

Department of Computer Science, UMT, Lahore. 12 Riaz Ahmad


Lab Manual: Programming Fundamentals

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.

5.2 Home Task


1. Ask the user to enter marks obtained in a course and the total marks of the course. Then
display a menu

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).

Department of Computer Science, UMT, Lahore. 13 Riaz Ahmad


Lab Manual: Programming Fundamentals

Department of Computer Science, UMT, Lahore. 14 Riaz Ahmad

You might also like