ITC LAB 11 - For Loop
ITC LAB 11 - For Loop
LAB # 11
To Understand the Control Structure Using FOR LOOP
Objective
This lab demonstrates the use of repetition structure named “for loop”.
Loops
There are many situations where a certain operation is repeated number of time, this repetition is
termed „loop‟ in programming languages. Loops are broadly divided into two categories (1)
counted loops (2) conditional loops. Counted loops are those in which number of iterations can
be predicted or counted whereas in conditional loops one cannot count the number of iterations
and loop terminates when condition is met. Consider a situation in which user is prompted to
type any string and program gets input until user hits the Enter key. In this lab only for loop is
discussed which is usually considered a counted loop.
For Loop
There are different loop statements in C++ language and „For‟ is one of them. These statements
also allow one or more statements to be repeated. The „For loop’ is considered most flexible
loop because it allows number of variations. In its most common form, the „for‟ loop is used to
repeat a statement or block of statements for some specified number of times.
General Form
Its general form for repeating statement(s) is shown here:
for ( initial_value ; condition_expr ; increment/decrement expr )
statement ;/ Block
Taking the following For statement for description;
Program 1
#include <iostream>
using namespace std;
int main ()
{
for ( int count=1 ; count<=10 ; count++ )
cout << “count= ” << count << endl;
}
1
Introduction to Computing Lab 11: Understand the FOR loop
Initialization
count variable is initialized by expression count=1. The initialization expression is always
executed only once at the top of the loop. Loop variable count can be initialized with any integer
value and here in our example it is initialized with integer 1.
Conditional Expression
The expression count<=10 test each time through the loop to see if the count is less than equal to
10 or not. To do this it is using the relational operator <= (less than or equal to). If the test is true
the body of the loop is executed each time after the test, otherwise exit the loop and control is
passed to the statement(s) that are written just after the loop.
Increment / Decrement Statements
The expression, count++, adds 1 into the value of count variable on each iteration of the loop. A
programmer can also use decrement operator as well instead of the increment operator.
Remember that loop will become infinite if test condition always remains true i.e. count is not
increment so it will always have value 1.
Body of the Loop
The statement, which is repeated during the loop operation, is termed as the „body of the loop‟.
Here the statement cout<< “count = ”<< count <<endl; is in the body of the loop. The
statement is terminated with a semi colon whereas For statement is not, this is because the entire
combination of the „For‟ keyword, the loop expression and the statement continuing the body of
the loop are considered to be a single C++ statement.
Program 2
This program executes a prints the sum of the following series.
1+2+3+… 10
#include <iostream>
using namespace std;
int main ()
{
int sum=0;
for ( int count = 1 ; count<=10 ; count++ )
sum = sum + count;
cout << “Sum of the series is: ” << sum << endl;
}
Write and compile the program and observe the output generated.
2
Introduction to Computing Lab 11: Understand the FOR loop
Program 3
This program prints the table of 5 on the display screen.
#include<iostream>
using namespace std;
int main ( )
{
cout << “Table of 5” << endl;
cout << “ --------- ” << endl;
for (int i=1; i <= 10; i++)
cout << “5 x ” << i << “ = ” << i*5 << endl;
}
Write and compile the program and observe the output generated.
Program 4
This program draws a pyramide of given numbers of rows of stars.
#include <iostream>
using namespace std;
int main()
{
int row, c, n, temp;
cout << "Enter the number of rows in pyramid of stars you wish to see ";
cin >> n;
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
cout << " ";
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
cout << "*";
cout <<"\n";
}
}
Write and compile the program and observe the output generated.
3
Introduction to Computing Lab 11: Understand the FOR loop
Lab Task
Lab Task 11.1) Write a program to calculate the factorial of the input number.
( For example: 5! = 1*2*3*4*5 = 120 )
Factorial of a number:
******************
Enter any number: 5
5! = 1*2*3*4*5 = 120
Lab Task 11.2) Write a program to generate a Fibonacci Series of given number of terms.
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,……