0% found this document useful (0 votes)
34 views7 pages

Lab Manual 4

The lab manual for Fall 2021 at UMT Lahore focuses on implementing IF-Else statements, switch statements, and for loops in C++. It includes sample tasks and code for grading based on marks, determining the day of the week, and performing calculations. Additionally, it outlines specific lab tasks for students, such as age-based messages and character evaluation.

Uploaded by

groundedblogger1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

Lab Manual 4

The lab manual for Fall 2021 at UMT Lahore focuses on implementing IF-Else statements, switch statements, and for loops in C++. It includes sample tasks and code for grading based on marks, determining the day of the week, and performing calculations. Additionally, it outlines specific lab tasks for students, such as age-based messages and character evaluation.

Uploaded by

groundedblogger1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab Manual (Lab 03 + 04)

Session: Fall 2021

School of Systems and Technology


UMT Lahore Pakistan

LAB INSTRUCTOR: AYESHA MAJID ALI


Contact: [email protected] Room #: 504 Last cabin 4th floor SST
Objective
The objective of the lab is to understand the concept and how to implement IF-Else statements
and nested IF-Else in C++, switch and for loops.

Nested if–else and if–else-if Statements

Sample Task
Write a program which takes marks as input and then shows output as follows:
Marks Output
87 – 100 Grade A
80 - 87 Grade B+
72 – 80 Grade B
67 – 72 Grade C+
60 - 67 Grade C
Below 60 Fail
Sample Code:

#include<iostream>

using namespace std;

int main()

int marks;

cout<<"Enter marks";

cin>>marks ;

if (marks >= 87 && marks <=100 )

cout<<"Grade A\n";

else if (marks >= 80 && marks < 87)

cout<<"Grade B+\n";

else if (marks >= 72 && marks< 80)


cout<<"Grade B\n";

else if (marks >= 67 && marks < 72)

cout<<"Grade C+\n";

else if (marks >= 60 && marks< 67)

cout<<"Grade C\n";

else

cout<<"Fail\n";

return 0;

In the above example logical operator && is used and because of this && operator condition
will only be satisfied if both the conditions in a single if are true. If any of the conditions is false
because of && operator the whole condition will be treated as false.

C++ Switch Statements

Use the switch statement to select one of many code blocks to be executed.

Syntax
switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}

This is how it works:

● The switch expression is evaluated once


● The value of the expression is compared with the values of each case
● If there is a match, the associated block of code is executed
Sample Program
#include <iostream>

using namespace std;

int main() {

int day = 4;

switch (day) {

case 1:

cout << "Monday";

break;

case 2:

cout << "Tuesday";

break;

case 3:

cout << "Wednesday";

break;

case 4:

cout << "Thursday";

break;

case 5:

cout << "Friday";

break;

case 6:

cout << "Saturday";

break;
case 7:

cout << "Sunday";

break;

return 0;

For Loops

Syntax
General form of for loop is
for (initialization; loop condition ; updating )
{
Statement(s)
}

Initialization, loop condition and updating (called loop control statements) control the execution
of the body of the for loop.

Sample Task
Following program computes the sum of the first ten integers. It also computes the sum of squares of the
first ten integers. Run the following program:

Sample Code:

#include<iostream>
using namespace std;
int main(void)
{
int count;
// Display the numbers 1 through 10
for(count = 1; count <= 10; count++)
cout<< count;
cout<<endl;
return 0;
}
Output example:

1 2 3 4 5 6 7 8 9 10
Lab Tasks
Task 1:

Write a program, which takes age as input from user and prints appropriate message depending
upon the following conditions: (Use if else statements)

● If age less than 6 and greater than 1 then prints, “What a nice child!”
● If the age is between 6 and 9 then prints, “That’s a good age!”
● If age is between 9 and less than 20 then prints, “Ah! In the prime of life”
● If an age between 20 and less than 30 then prints, “Watch out, the younger ones are
gaining on you.”
● More than 30 then it prints, “Well, have fun, and don’t look back.”

Task 2:

Create a mini calculator that adds, subtracts, divides and multiplies using switch statements.

Task 3:

Display your name using a for loop five times.

Task 4:

Write a Program using if-else structure. The program will get a Single Character from the user
and Tell whether it is Vowel or Not.

Sample output:

Enter a character: A

A is a Vowel and it is Capital Letter.

Enter a character: y

y is not a Vowel and it is a small Letter.

You might also like