PF Week 4
PF Week 4
1. Introduction to Loops
Definition: A loop is a control structure that allows a block of code to repeat multiple times.
Loops are essential for tasks that require repeated actions, such as processing lists or
performing calculations over a range of values.
Purpose: Loops help automate repetitive tasks, making programs more efficient and concise.
Instead of writing the same code multiple times, a loop enables it to run with varying conditions.
Types of Loops in C++: The three main types are for, while, and do-while loops. Each has its
specific use case based on the number of repetitions and conditions.
Definition: A for loop is a counter-controlled loop used when the number of iterations is known
in advance. It consists of three parts: initialization, condition, and increment/decrement.
Structure:
// code to repeat
Example Scenario: Use a for loop when you need to repeat an action a fixed number of times,
such as displaying numbers from 1 to 10.
Solution:
#include <iostream>
int main() {
return 0;
Definition: A while loop is an entry-controlled loop that repeats as long as a specified condition
is true. The loop checks the condition before each iteration.
Structure:
while (condition) {
// code to repeat
Example Scenario: Use a while loop when the number of repetitions isn’t known in advance, like
taking input until the user enters a specific value.
Write a program that continues to take user input until the user enters 0.
Solution:
#include <iostream>
int main() {
int number;
while (number != 0) {
cout << "You entered: " << number << endl;
return 0;
Definition: A do-while loop is an exit-controlled loop that guarantees the loop will execute at
least once, regardless of the condition. It checks the condition after each loop iteration.
Structure:
do {
// code to repeat
} while (condition);
Example Scenario: Use a do-while loop when you need the code to run at least once, like
displaying a menu that should appear before taking user input.
Write a program that keeps asking for a password until the correct one is entered.
Solution:
#include <iostream>
int main() {
int password;
do {
return 0;
o Example Program Question: Write a program to calculate the sum of the first n natural
numbers (e.g., if n is 5, the sum is 1+2+3+4+5 = 15).
Solution:
#include <iostream>
int main() {
int n, sum = 0;
cin >> n;
sum += i;
cout << "Sum of first " << n << " natural numbers is: " << sum << endl;
return 0;
}
Factorial Calculation with a while Loop:
Solution:
#include <iostream>
int main() {
int n, factorial = 1;
cin >> n;
int i = 1;
while (i <= n) {
factorial *= i;
i++;
cout << "Factorial of " << n << " is: " << factorial << endl;
return 0;
6. Nested Loops
Definition: Nested loops are loops inside another loop. They are commonly used for working
with multi-dimensional data, such as matrices or grids.
Example Scenario: Use nested loops when you need to iterate over multiple levels, like rows
and columns in a grid.
Solution:
#include <iostream>
int main() {
return 0;