Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi
NAME OF STUDENT: ID No:
Lab # 065
Programming Fundamentals
Objectives:
Getting familiar with loops.
The for loop
Structure of the for loop
Initialization expression
Test expression
Increment expression
Operational flow chart of for loop
C++ Loops
In computer programming, loops are used to repeat a block of code.
For example, let's say we want to show a message 100 times. Then instead of writing the print
statement 100 times, we can use a loop.
There are 3 types of loops in C++.
for loop
while loop
do...while loop
C++ for loop
The syntax of for-loop is:
for (initialization; condition; update) {
// body of-loop
}
Here,
initialization - initializes variables and is executed only once
condition - if true, the body of for loop is executed
if false, the for loop is terminated
update - updates the value of initialized variables and again checks the condition
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi
NAME OF STUDENT: ID No:
Structure of for Loop
Flow Chart of for loop
Practice Program-1(Printing Numbers From 1 to 10)
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi
NAME OF STUDENT: ID No:
Practice Program-2(Display a text 5 times)
Practice Program-3(Find the sum of first n natural numbers)
LAB TASK:
Q1) Print a diamond, pyramid and half pyramid star pattern, by using for loop.