Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Computer Systems & Programming

Khawaja Fareed University of Engineering and Information


Technology, Rahimyar Khan
Department of Mechanical Engineering
KFUEIT

Programming Lab: Lab manual 5

Submitted by:
Osama Abid
(MEEN 18111016)

Submitted to:
Engr. Faizan Shah

Date of Submission:

28𝑡ℎ June, 2020

1
Computer Systems & Programming

Lab-5

For Loop

OBJECTIVE:

To demonstrate the use of for Loop in C++.

EQUIPMENT REQUIRED:
 Laptop
 Computer

TOOLS REQUIRED:
 C++ IDE

Theory & Procedure:

Loops
The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This
involves repeating some portion of the program either a specified number of times or until a
particular condition is being satisfied. This repetitive operation is done through a loop control
instruction.
There are three methods by way of which we can repeat a part of a program. They are:
(a) Using a for statement
(b) Using a while statement
(c) Using a do-while statement
Each of these methods is discussed in the following pages

The for Loop

The for loop is (for many people, anyway) the easiest C++ loop to understand. All its loop
control elements are gathered in one place, while in the other loop constructions they are
scattered about the program, which can make it harder to unravel how these loops work.
The for loop executes a section of code a fixed number of times. It‟s usually (although not
always) used when you know, before entering the loop, how many times you want to execute the
code.
The for allows us to specify three things about a loop in a single line:
(a) Setting a loop counter to an initial value.
(b) Testing the loop counter to determine whether its value has reached the number of repetitions
desired.
(c) Increasing the value of loop counter each time the program segment within the loop has been
executed.

2
Computer Systems & Programming

The general form of for statement is as under:


for (initialization expression; test expression; increment expression)
{
do this ;

and this ;
and this ;
}

Here’s an example, that displays the squares of the numbers from 0 to 14:

// demonstrates simple FOR loop


#include <iostream>
using namespace std;
int main()
{
int j; //define a loop variable
for(j=0; j<15; j++) //loop from 0 to 14,
cout << j * j << “ “; //displaying the square of j
cout << endl;
return 0;
}

Here’s the output:


0 1 4 9 16 25 36 49 64 81 100 121 144 169 196

How does this work? The for statement controls the loop. It consists of the keyword for,
followed by parentheses that contain three expressions separated by semicolons:
for(j=0; j<15; j++)
Figure below shows the syntax of a for loop

3
Computer Systems & Programming

Exercise:

1. Write a program that inputs table number and length of table and then displays the table
using for loop.

Code
#include<iostream>

using namespace std;

int main()

int x,y;

cout<<"Enter any number"<<endl;

cin>>x;

cout<<"Enter range"<<endl;

cin>>y;

for (int i=1; i<=y; ++i)

cout <<x<<" * "<<i<<" = "<< x * i <<endl;

4
Computer Systems & Programming

return 0;

Result

Figure 1Result# 1

5
Computer Systems & Programming

2. Write a program to print the following series


1 4 7 10….47

Code
#include<iostream>

using namespace std;

main()

int y;

cout<<"Required series"<<endl;

for (y=1; y<=47; y=y+3)

cout<<y<<" ";

return 0;

Figure 2 Result# 2

6
Computer Systems & Programming

3. Use for loop so that they print only the even numbers between 1 and 20, that is, 2 4 6 8
10 12 14 16 18 20.

Code
#include<iostream>

using namespace std;

main()

int x;

cout<<"First 20 even numbers ";

for (x=1; x<=20; x=x+1)

if(x%2==0)

cout<<x<<" ";

return 0;

7
Computer Systems & Programming

Result

Figure 3 Result# 3

Rubric 1

Marks CLO2 – Level C2 mapped to PLO2 (Problem Analysis)

02 Is not able to write the code in C++ using decision making statements
and interpret it. Major help is required in writing the program.

06 Can write and interpret C++ codes using decision making statements with
minor error help.

10 Can write and interpret C++ codes using decision making statements
effectively and confidently.

You might also like