Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Programming Fundamentals

Lab Manual (Lab 6)

Topic: for loop, while loop, switch

Course Instructor:
Lab Instructors:

Session: Fall 2018

School of Systems and Technology


UMT Lahore Pakistan
Objectives
The objective of the lab is to understand the concept and how to implement For loop, while loop and Case
Control Structure. At the end of this lab students will be able to use all type of loops.

1. While Loop
General form of while loop is

Initialization
While (loop condition)
{
Statement(s)
Updating
}
Where expression acts as a decision maker. The statements from the body of the loop. Body of loop is
executed as long as the expression evaluates to true.
2. For Loop
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 body
of the for loop.

3. The Case Control Structure (Switch):

The control statement that allows us to make a decision from the number of choices is called a switch, or
more correctly a switchcase-default, since these three keywords go together to make up the control
statement. They most often appear as follows:

switch ( integer expression )


{
case constant 1 :
do this ;
case constant 2 :
do this ;
case constant 3 :
do this ;
default :
do this ;
}
Sample Program 1:
Following program computes the sum of first ten integers. It also computes the sum of squares of
first ten integers. Run the following program:
Sample Code:

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

Output example:

1 2 3 4 5 6 7 8 9 10

Sample Program 2:

While loop
#include<iostream>
using namespace std;

int main ()
{
/* local variable definition */
int a = 0;

/* while loop execution */


while (a<10)
{
cout<<"Value of a:"<<a<<endl;
a++;
}
return 0;
}
OUTPUT
Value of a: 0
Value of a: 1
Value of a: 2
Value of a: 3
Value of a: 4
Value of a: 5
Value of a: 6
Value of a: 7
Value of a: 8
Value of a: 9

Sample Program 3(Switch):

int main()
{
Int i = 2;
switch(i)
{
case 1:
cout<<”One”;
break;
case 2:
cout<<”Two”;
break;
case 3:
cout<<”Three”;
break;
default:
cout<<”Four”;
break;
}
return 0;
}
Task 1
Calculate the sum of number from one to ten using for and while loop.

Task 2
Write for statements that print the following sequences of values
a) 1, 2, 3, 4, 5, 6, 7
b) 3, 8, 13, 18, 23
c) 20, 14, 8, 2, –4, –10

Task 3
Write a program that calculates and print the average of even numbers from 2 -30, using
all loops.

Task 4
Write a program that takes initial and ending values and print all even and odd numbers
between the range in a tabular form.

Sample Output

Enter initial Value: 13


Enter last Value: 24

Even Odd
13
14 15
16 17
18 19
20 21
22 23
24
Task 5
Write a program for calculator using switch Control structure which takes a mathematical
expression with two operands and one operator between them, e.g. 3+4, 4*7, 32/4 and
20-13.
Note: You are supposed to enter the expression without spaces.

Sample Outpur 1.

Enter an expression: 13*4


The answer is: 52

Sample Outpur 2.

Enter an expression: 32/4


The answer is: 88

Sample Outpur 3.

Enter an expression: 13+27


The answer is: 40

You might also like