Professional Documents
Culture Documents
Lab 5 Done
Lab 5 Done
Submitted by:
Osama Abid
(MEEN 18111016)
Submitted to:
Engr. Faizan Shah
Date of Submission:
1
Computer Systems & Programming
Lab-5
For Loop
OBJECTIVE:
EQUIPMENT REQUIRED:
Laptop
Computer
TOOLS REQUIRED:
C++ IDE
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 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
and this ;
and this ;
}
Here’s an example, that displays the squares of the numbers from 0 to 14:
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>
int main()
int x,y;
cin>>x;
cout<<"Enter range"<<endl;
cin>>y;
4
Computer Systems & Programming
return 0;
Result
Figure 1Result# 1
5
Computer Systems & Programming
Code
#include<iostream>
main()
int y;
cout<<"Required series"<<endl;
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>
main()
int x;
if(x%2==0)
cout<<x<<" ";
return 0;
7
Computer Systems & Programming
Result
Figure 3 Result# 3
Rubric 1
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.