0% found this document useful (0 votes)
9 views6 pages

PF Week 4

This document provides an introduction to loops in C++, detailing their definition, purpose, and types including for, while, and do-while loops. It includes examples of each loop type with corresponding program solutions for tasks such as printing numbers, user input handling, and calculating summation and factorial. Additionally, it covers nested loops for multi-dimensional data processing, illustrated with a multiplication table example.

Uploaded by

adnanqaisar46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

PF Week 4

This document provides an introduction to loops in C++, detailing their definition, purpose, and types including for, while, and do-while loops. It includes examples of each loop type with corresponding program solutions for tasks such as printing numbers, user input handling, and calculating summation and factorial. Additionally, it covers nested loops for multi-dimensional data processing, illustrated with a multiplication table example.

Uploaded by

adnanqaisar46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

2. The for Loop

 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:

for (initialization; condition; update) {

// 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.

Example Program Question:

 Write a program that prints the numbers from 1 to 5.

Solution:

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 5; i++) {

cout << i << " ";


}

return 0;

3. The while Loop

 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.

Example Program Question:

 Write a program that continues to take user input until the user enters 0.

Solution:

#include <iostream>

using namespace std;

int main() {

int number;

cout << "Enter numbers (0 to stop): ";

cin >> number;

while (number != 0) {
cout << "You entered: " << number << endl;

cin >> number;

return 0;

4. The do-while Loop

 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.

Example Program Question:

 Write a program that keeps asking for a password until the correct one is entered.

Solution:

#include <iostream>

using namespace std;

int main() {

int password;

do {

cout << "Enter password (1234 to unlock): ";


cin >> password;

} while (password != 1234);

cout << "Access granted." << endl;

return 0;

5. Using Loops for Summation and Factorial Calculation

 Summation with a for Loop:

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>

using namespace std;

int main() {

int n, sum = 0;

cout << "Enter a number: ";

cin >> n;

for (int i = 1; i <= n; i++) {

sum += i;

cout << "Sum of first " << n << " natural numbers is: " << sum << endl;

return 0;

}
 Factorial Calculation with a while Loop:

o Example Program Question: Write a program to calculate the factorial of a number


(e.g., if n is 4, factorial is 432*1 = 24).

Solution:

#include <iostream>

using namespace std;

int main() {

int n, factorial = 1;

cout << "Enter a number: ";

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.

Example Program Question:


 Write a program to display a simple multiplication table from 1 to 5.

Solution:

#include <iostream>

using namespace std;

int main() {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= 5; j++) {

cout << i * j << "\t";

cout << endl;

return 0;

You might also like