Programming Fundamentals
(CL1002)
LABORATORY MANUAL
BS-CE, Spring 2025
LAB 01
REPETITION AND FLOW CONTROL STRUCTURES
________________________________________ __________ ____
STUDENT NAME ROLL NO SEC
______________________________________
LAB ENGINEER'S SIGNATURE & DATE
Maryam Zafar
MARKS AWARDED: /10
_____________________________________________________________________
NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES (NUCES), ISLAMABAD
Prepared by: Engr. Aamer Munir Version: 1.1
Date last
Last edited by: Engr. Azhar Rauf, Engr. Aamer Munir Sep 27, 2018
edited:
Date last
Verified by: Engr. Azhar Rauf Oct 1, 2023
edited:
Lab Learning Objectives:
In this lab session you will learn about:
1. Repetition Control Structures or Loops
2. Flow Control Structures
Repetition and Flow Control Structures LAB 07
Tools needed:
1. PC running Windows Operating System (OS)
2. DevC++ (installed)
Turn on the PC and log into it using your student account information.
Note:
The information and concepts discussed in this
lab manual are in sufficient detail but are
still not exhaustive. It is assumed that all
these concepts are already taught to you in your
PF theory class.
1. Repetition Control Structures or Loops
Whenever we need to do any task repetitively, we use loops. There are three types of
loops used in C++ -- while loop, for loop and do-while loop.
while loop:
The structure of a while loop is:
while(expression)
body of the loop
where the expression provides an entry condition to the loop, i.e., the loop will be
executed only when the expression evaluates to true. Once the body of the loop is
executed, the expression is checked again and if it evaluates to true the body of the loop
is executed again. This evaluation of the expression and execution of body of the loop
continues until the expression evaluates to false after which the code next to the body of
the loop executes.
Each execution of the body of the loop is called an iteration.
If expression always evaluates to true, such a loop is known as an infinite loop.
A while loop is usually one of the four main types:
i) Counter-Controlled while loop:
When we wish a while loop to execute or iterate N number of times, we use the
expression to test a counter variable value against N and increment the counter each
times the body of loop executes.
counter = 0;
while(counter < N)
{
body of the loop
counter++;
}
Example 1:
Find average of ten positive numbers entered by the user.
float num, sum = 0;
int i = 0;
PF LAB NUCES, ISLAMABAD Page 2 of 7
Repetition and Flow Control Structures LAB 07
while(i++< 10)
{
cin >> num;
sum += num;
}
cout << "Ave = " << sum/10 << endl;
ii) Sentinel-Controlled while loop:
When we do not know how many times to run a loop, but we know that the last entry will
be a unique value, called a sentinel, then we make our expression check that sentinel
value to decide when to stop a loop. In such a case we must read one value before the
while loop so that we have a valid value to test against the sentinel value.
cin >> variable;
while(variable != sentinel)
{
body of the loop
cin >> variable;
}
Example 2:
Find average of positive numbers entered by the user until the user enters -1.
float num, sum = 0;
int count = 0;
cin >> num;
while(num != -1)
{
sum += num;
count ++;
cin >> num;
}
cout << "Ave = " << sum/count<< endl;
iii) Flag-Controlled while loop:
Sometimes neither do we know the number of times to run a loop nor is there any
sentinel value present in the data to check for. However, there may a condition which if
true will signal the loop to stop. In this case, use a bool variable as a flag to indicate
when that condition has occurred so that the next time the loop is not executed.
isFound = false;
while(!isFound)
{
body of the loop
if(expression)
isFound = true;
remaining body of the loop
}
Example 3:
Find average of first ten positive even numbers entered by the user where the user may
continue entering both even and odd numbers. Display the average after the required
number of positive even numbers (i.e., 10) has been entered.
Code:
PF LAB NUCES, ISLAMABAD Page 3 of 7
Repetition and Flow Control Structures LAB 07
int num, sum = 0; //even and odd is linked to integers
int count = 0;
bool NumReached= false;
while(!NumReached)
{
cin >> num;
if (num>=0 && num%2==0) {
count++;
sum += num;
}
if(count == 10)
NumReached= true;
}
cout << "Ave = " <<static_cast<float>(sum)/count<< endl;
for loop:
A while loop can be used for any task that requires repetition. However, C++ often
provides optimized ways of writing code structures that are encountered very often by
programmers. A for loop is such an example. A for loop is just a specialized form of
writing a counter-controlled while loop.
for(initial statement; expression; update statement)
body of the loop
The initial statement is executed only once before the for loop is entered.
The expression is just like the expression in while loop which is evaluated and if true the
body of the loop is executed.
The update statement is executed at the end of each iteration.
For example, the following while loop code:
counter = 0;
while(counter < N)
{
body of the loop
counter++;
}
can be written as a for loop code as:
for(counter = 0; counter < N; counter++)
{
body of the loop
}
Hence, for loop is ideally suited where the number of iterations is known. This does not
mean it cannot be used in other situations.
do-while loop:
A do-while loop has the following syntax:
PF LAB NUCES, ISLAMABAD Page 4 of 7
Repetition and Flow Control Structures LAB 07
do
{
body of the loop
}
while(expression);
Note the reserved word 'do' and the semicolon at the end of while(expression).
In a do-while loop, the body of the loop is executed first and then expression is
evaluated. If the expression evaluates to true, the body of the loop is executed again,
else the loop is exited.
A do-while loop will be executed at least once because contrary to while and for loops, a
do-while loop does not have an entry condition, rather an exit condition.
Our example, DIVDO, invites the user to enter two numbers: a dividend (the top number
in a division) and a divisor (the bottom number). It then calculates the quotient (the
answer) and the remainder, using the / and % operators, and prints out the result.
int main()
{
long dividend, divisor;
char ch;
do //start of do loop
{ //do some processing
cout << “Enter dividend: “; cin >> dividend;
cout << “Enter divisor: “; cin >> divisor;
cout << “Quotient is “ << dividend / divisor;
cout << “, remainder is “ << dividend % divisor;
cout << “\nDo another? (y/n): “; //do it again?
cin >> ch;
}
while( ch != ‘n’ ); //loop condition
2. break and continue statements
Remember the break statement in a switch-case structure. It provides an exit from
switch structure. Similarly, break can be used to exit from a loop.
For example, instead of using a flag in a flag-controlled while loop:
bool flag = false;
while(!flag)
{
body of the loop
if(expression)
flag = true;
}
consider the following while loop with a break statement to do the same job:
PF LAB NUCES, ISLAMABAD Page 5 of 7
Repetition and Flow Control Structures LAB 07
while(true)
{
body of the loop
if(expression)
break;
}
Note the loop will iterate infinitely were it not for the conditional break which will terminate
the loop.
The continue statement when used within a loop means that the remaining part of the
body of the loop after the continue statement will be skipped and the next iteration
started.
For example, look at this odd use of the continue statement:
while(true)
{
body of the loop
if(!expression)
continue;
break;
}
When the expression evaluates to false, !expression is true and continue statement is
executed, which immediately takes execution to the start of the loop body skipping the
execution of break statement.
However, when expression evaluates to true, !expression is false and continue
statement is not executed and the break outside the body of if gets executed, which exits
from the loop. Thus, this code is akin to the previous two codes which used flag control
and break statement.
Exercises
Task # 1: Code file name lab01_1.cpp
Use a FOR loop to calculate factorial of any number that is input by user. Display the
result.
Task # 2: Code file name lab01_2.cpp
Perform Task 01 using While Loop
Task # 3: Code file name lab01_3.cpp
Using the program of question 1, use FOR loops to display the range of factorials.
Range can be taken from user e.g. if input is 4 then your program should display
factorials from 1 to 4. use setw for indentation of result
Number Factorial
1 1
PF LAB NUCES, ISLAMABAD Page 6 of 7
Repetition and Flow Control Structures LAB 07
2 2
3 6
Task # 4: Code file name lab01_4.cpp
Fibonacci Series is in the form of 0 1 1 2 3 5 8 and so on. Every next number is sum
of previous two numbers. Using For loops write a program to display range of
Fibonacci numbers. Range can be input of a user
Task # 05: Code file name lab01_5.cpp
A number can be stated as prime number using the following code
cout << “Enter a number: “;
cin >> n; //get number to test
for(j=2; j <= n/2; j++) //divide by every integer from
if(n%j == 0)
//2 on up; if remainder is 0,
{ //it’s divisible by j
cout << “It’s not prime; divisible by “ << j << endl;
exit(0); //exit from the program
}
cout << “It’s prime\n”;
Write a program in which user will enter the range e.g. 100 and your program should
display all the prime numbers from 1 to 100. Prime numbers start from 2.
Task # 6: Code file name lab1_6.cpp
Write a program that ask user to enter password. User is allowed to enter password five
times. Correct password is 12345. If he enters correct password, print the number of trials on
which he guessed right. Otherwise, terminate the program if he/she cannot guess in five
trials.
HINT: You can use break; to end a loop early.
PF LAB NUCES, ISLAMABAD Page 7 of 7