1
Week-10: Loops inC++
Presented by: Farhan Nisar
Program: BS(IT)
Introduction to Programming
Language
2.
2
Overview
1. Types ofLoops
• Fixed Loop (Count-Controlled Loop)
• Non-Fixed Loop (Event-Controlled Loop)
2. Fixed Loop (for Loop)
3. Variations in for Loop
• Defining variable in for loop
• Multiple initializations in for loop
• Multiple increment/decrement expressions
• Initialization outside for loop
• Increment/decrement inside for loop
• No testing (infinite loop)
• Output statement inside for loop
• Nested for loop
3.
3
Loops in C++
Definition:Loops allow executing a block of code
multiple times until a specific condition is met.
Types of Loops:
1. Fixed Loop (Count-Controlled Loop) – Runs a
specific number of times.
2. Non-Fixed Loop (Event-Controlled Loop) – Runs
until a condition becomes false.
4.
4
Fixed Loop (Count-ControlledLoop)
• Definition: Executes a loop a predefined number of times.
• Common Example: The for loop.
Syntax:
for (initialization; condition; increment/decrement) {
// Loop body
}
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Iteration: " << i << endl;
}
return 0;
}
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
5.
5
Non-Fixed Loop (Event-ControlledLoop)
• Definition: Loop execution depends on a condition rather than a fixed count.
• Common Examples: while and do-while loops.
Example:
#include <iostream>
using namespace std;
int main() {
int num = 1;
while (num <= 5) {
cout << "Iteration: " << num << endl;
num++;
}
return 0;
}
6.
6
Defining Variable infor Loop
• Definition: The loop variable can be declared inside the
for loop.
Example:
for (int i = 1; i <= 5; i++) {
cout << "Iteration: " << i << endl;
}
8
Multiple Increment/Decrement Expressionsin for Loop
• Definition: Allows modifying multiple variables.
Example:
for (int i = 1, j = 5; i <= 5; i++, j--) {
cout << "i: " << i << " j: " << j << endl;
}
9.
9
Initialization Outside forLoop
• Definition: The loop variable can be initialized outside.
Example:
int i = 1;
for (; i <= 5; i++) {
cout << "Iteration: " << i << endl;
}
10.
10
Increment/Decrement Inside forLoop
• Definition: The increment or decrement can be placed
inside the loop body.
Example:
for (int i = 1; i <= 5;) {
cout << "Iteration: " << i << endl;
i++;
}
11.
11
No Testing (InfiniteLoop)
• Definition: A for loop without a condition results in an
infinite loop.
Example:
for (;;) {
cout << "This is an infinite loop" << endl;
}
12.
12
Output Statement Insidefor Loop
• Definition: Allows printing values directly inside the loop.
Example:
for (int i = 1; i <= 5; i++) {
cout << "Iteration " << i << endl;
}
13.
13
Nested for Loop
•Definition: A for loop inside another for loop.
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
cout << "(i, j): (" << i << ", " << j << ")t";
}
cout << endl;
}
return 0;
}
Output:
(i, j): (1, 1) (i, j): (1, 2) (i, j): (1, 3)
(i, j): (2, 1) (i, j): (2, 2) (i, j): (2, 3)
(i, j): (3, 1) (i, j): (3, 2) (i, j): (3, 3)
14.
14
Summary
• Fixed Loops:Execute a specific number of times.
• Non-Fixed Loops: Execute based on a condition.
• Variations in `` Loops: Flexible use of initializations,
increments, and nested structures.