Professional Documents
Culture Documents
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.
{
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.