You are on page 1of 2

Fundamentals of Programming Lab Week 2 Page 1

Fundamentals of Programming
Lab Practical week 3

Objective:
Understanding of Loops (For Loop)

Repetition Structure
Repetition structures are used to save us from typing statements again and again.

The statements inside the loops runs as many times as you want depending upon
the conditions you provide. One time running of loop is called one iteration.

There are three types of repetition structures in C++.

 For loop
 While loop
 Do while loop

Example:

Consider a user wants to show “Practicing for loop” ten times on screen.

For Loop
For loop is often used when you have a piece of code which you want to repeat "n"
number of times. It means that when you know exactly how many times you want
this statement to run.

Syntax:
for(initializationStatement; testExpression; updateStatement)

{ /*

1. The initialization statement is executed only once at the beginning.


2. Then, the test expression is evaluated.
3. If the test expression is false, for loop is terminated. But if the test expression is true,
codes inside body of for loop is executed and update expression is updated.
4. Again, the test expression is evaluated and this process repeats until the test
expression is false. */

City University of Sciences and Technology Peshawar Furqan Nasir


Fundamentals of Programming Lab Week 2 Page 2

Example
for(int i=0;i<5;i++)

cout<<”Practicing For loop”;

Tasks
1. Check the output of above program.
2. Change the statement to show statement for 10 times.
3. Add one more statement to the loop. And see the difference.
4. Output the value of i in each iteration.
5. Change the value of i to show the iteration number. i-e Start from 1
6. See the difference when i is initialized outside of loop.
7. See the difference when increment/decrement is done inside loop body.

City University of Sciences and Technology Peshawar Furqan Nasir

You might also like