University of Management and Technology
Knowledge Unit of Science and
Technology
Laboratory Manual
[CC1021]: [Programming Fundamentals]
[Semester Fall-2021]
Class: [BSCS, BSIT]
Lab [5]: [Switch statement in C++ and introduction to Loops]
Instructor: [Ms. Hifza Munir]
[CC1021]: [Programming Fundamentals] Page 1
University of Management and Technology
Switch statement in C++
A switch statement allows a variable to be tested for equality against a list of values. Each value
is called a case, and the variable being switched on is checked for each case.
[CC1021]: [Programming Fundamentals] Page 2
University of Management and Technology
[CC1021]: [Programming Fundamentals] Page 3
University of Management and Technology
Loops
In the programming world, the loop is a control structure that is used when we want to execute a
block of code, multiple times. It usually continues to run until and unless some end condition is
fulfilled.
Without Loops:
we have three types of Loops in C++:
For Loop
While Loop
Do While Loop
[CC1021]: [Programming Fundamentals] Page 4
University of Management and Technology
For Loop
For loop is a counter control loop.
Syntax
for ( init; condition; increment )
{
statement(s);
}
Example
[CC1021]: [Programming Fundamentals] Page 5
University of Management and Technology
Lab Task:
1. Write a C++ program to input an alphabet and check whether it is vowel or
consonant using switch case.
2. Write a C++ program print total number of days in a month using switch case.
3. Write a program in C++ to accept a grade and declare the equivalent description.
Using Switch Statement.
Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail
Test Data :
Input the grade :A
Expected Output :
You have chosen : Average
[CC1021]: [Programming Fundamentals] Page 6
University of Management and Technology
4. Write a program in C++ to display n terms of natural number.
Sample Output:
Input a number of terms: 7
The natural numbers upto 7th terms are:
1234567
5. Write a program in C++ to print a square pattern with # character.
Sample Output:
Print a pattern like square with # character:
--------------------------------------------------
Input the number of characters for a side: 4
####
####
####
####
6. Write a C++ program to input a table and print its table.
7. Write a C++ program to print the square of the first 20 odd positive integers
[CC1021]: [Programming Fundamentals] Page 7