You are on page 1of 18

Duhok Polytechnic University

Technical College of Engineering


Energy Engineering Department
Second Year

Introduction to Programming
(Lecture 6)
Batool Abdulsatar 2020-2021
Types of Loop in C++
 Types of Loop
 Counter loop (for loop)
 Conditional loops (while, do while)
The for loop
If a programmer wants
to perform "specific
operation" multiple
times then he has to
use a loop.
The for loop
The body of a loop gets executed repeatedly until the
condition becomes false or the programmer breaks the loop.
The for loop
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific
number of times.

The syntax of a for loop in C++ is:

for (initialization; condition ; increment-decrement)


{
statement(s);
}
The for loop
 Initialization: As the loop starts initialization expression
is executed once. This expression initializes the loop.
 Condition: If it is true, the body of the loop is executed.
If it is false, the body of the loop does not execute and
flow of control jumps to the next statement just after the
for loop.
 Increment-Decrement: With each iteration this
expression is executed. This expression can increment or
decrement the value.
Example 1: Write a program in C++ to print the numbers
between 1 to 4.
Example 2: Write a program in C++ to print the numbers
between 20 to 15.
Example 3: Find the sum of the numbers from 1 to 1000.
Solution: (arithmetically)
1+2+3+4+…+1000 = ???
Example 4: Write a program in C++ to find the sum of the
numbers from 1 to any number specified from the keyboard.
Example 5: Write a program in C++ to find the factorial of
any number specified from the keyboard.
The nested for loop
 The syntax of nested for loop is shown below:

for(initialization ; termination ; increment-decrement)


{
for(initialization ; termination ; increment-decrement)
outer
inner

{
statement(s);
}
statement(s);
}
The nested for loop
 A for loop can contain any kind of statement in its
body, including another for loop.
 The inner loop must have a different name for its loop
counter variable so that it will not conflict with the
outer loop.
 nested loop: Loops placed inside one another, creating
a loop of loops.
The nested for loop
Example 6: What is the output of the following program:
The nested for loop
Example 7: Write a program in C++ to calculate the
multiplication table from 1 to 12.
Review Questions
Q1: Write a C++ program to find the square of the numbers from 1
to 6.
Q2: Write a program that prints the cubes of the numbers from 1 to
10.
Q3: write a program in C++ that uses for-loop to sum the even
integers from 2 to 10,then print the result.
Q4: Write a program in C+ + to read 4 Numbers, then print the
average of them (use for-loop).
Q5: Write a program in C++ to print the average of the odd numbers
between 1 and 10.
Q6: Write a program in C++ to print the average of the even
numbers and the average of the odd numbers between the numbers 1
and 4
Q7: Write a program in C++ to find and print the average of even
numbers that are entered by a user. (the program accepts six
numbers)
Q8: Write a program in C++ to find y, where y= 10
𝑖=0 𝑖
Q9: Write a program in C++ to print the word “energy” ten times.

You might also like