Professional Documents
Culture Documents
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.
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)
{ /*
Example
for(int i=0;i<5;i++)
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.