0% found this document useful (0 votes)
12 views5 pages

PF Lab Manual 5

The document outlines an experiment for a B.S. Computer Systems course at Riphah International University, focusing on developing C++ programs using loops, specifically while loops, for loops, and do-while loops. It includes descriptions of loop types, their syntax, and examples of their usage. Additionally, it lists tasks for students to complete, such as displaying statements and calculating numbers using loops.

Uploaded by

hexadive
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)
12 views5 pages

PF Lab Manual 5

The document outlines an experiment for a B.S. Computer Systems course at Riphah International University, focusing on developing C++ programs using loops, specifically while loops, for loops, and do-while loops. It includes descriptions of loop types, their syntax, and examples of their usage. Additionally, it lists tasks for students to complete, such as displaying statements and calculating numbers using loops.

Uploaded by

hexadive
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/ 5

Department of Electrical and Computer

Engineering
Faculty of Engineering & Applied Sciences
Riphah International University, Islamabad, Pakistan

Program: B.S Computer Systems Semester II


Subject: CMSL-103 Programming Fundamentals Date:
…………

Experiment 5: Developing C++ programs for Iterations in C++ using


Loops
 While Loop
 For Loop

Name: ……………………………………. Roll No:


…………………………

Performance Lab Report


Description Total Marks Description Total Marks
Marks Obtained Marks Obtained
Ability to conduct 5 Organization/Structure 5
Experiment Data Presentation 5
Work Ethics Individual and Team Performance
Description Total Marks Description Total Marks
Marks Obtained Marks Obtained
Adherence to Team Collaboration and Contribution
Safety Guidelines Individual Task Performance

Remarks (if any): ………………………………….

Name & Signature of faculty: …………………………………


Loops
In Loop, the statement needs to be written only once and the loop will be executed 10 times as
shown below. In computer programming, a loop is a sequence of instructions that is repeated until
a certain condition is reached.
There are mainly two types of loops:
1. Entry Controlled loops: In this type of loop, the test condition is tested before entering the
loop body. For Loop and While Loop is entry-controlled loops.
2. Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the end
of the loop body. Therefore, the loop body will execute at least once, irrespective of whether
the test condition is true or false. the do-while loop is exit controlled loop.

S.No. Loop Type and Description

while loop
1. – First checks the condition, then executes
the body.

for loop
2. – firstly initializes, then, condition check,
execute body, update.

do-while
loop
3.
– firstly, execute the body then condition
check

For Loop-
A For loop is a repetition control structure that allows us to write a loop that is executed a specific
number of times. The loop enables us to perform n number of steps together in one line.

Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
Explanation of the Syntax:
 Initialization statement: This statement gets executed only once, at the beginning of the for
loop. You can enter a declaration of multiple variables of one type, such as int x=0, a=1, b=2.
These variables are only valid in the scope of the loop. Variable defined before the loop with
the same name are hidden during execution of the loop.
 Condition: This statement gets evaluated ahead of each execution of the loop body, and abort
the execution if the given condition get false.
 Iteration execution: This statement gets executed after the loop body, ahead of the next
condition evaluated, unless the for loop is aborted in the body (by break, goto, return or an
exception being thrown.)
Example
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++) {
cout << "Hello World\n";
}
return 0;
}
While Loop-
While studying for loop we have seen that the number of iterations is known beforehand, i.e. the
number of times the loop body is needed to be executed is known to us. while loops are used in
situations where we do not know the exact number of iterations of the loop beforehand. The loop
execution is terminated on the basis of the test conditions.
We have already stated that a loop mainly consists of three statements – initialization expression,
test expression, and update expression. The syntax of the three loops – For, while, and do while
mainly differs in the placement of these three statements.
Syntax:
initialization expression;
while (test_expression)
{
// statements

update_expression;
}
Example
#include <iostream>
using namespace std;
int main()
{
// initialization expression
int i = 1;
// test expression
while (i < 6) {
cout << "Hello World\n";
// update expression
i++;
}
return 0;
}
Tasks :

1. Display on screen the following statement 5 times using while loop.


a. “I will be a great human being”.

2. Write a program to calculate the square of first ten even numbers.

3. Write a program in C++ to display n terms of natural numbers and their sum.

4. Police is searching for a criminal whose ID card number is 101.Develop a program


that take an ID card number from every user who entered into a building. Keep
checking all users. For all other users, display on screen "ok you can go ".For the
criminal (whose card number is 101), display on screen "stop ".

You might also like