0% found this document useful (0 votes)
1 views14 pages

05 - Loops

The document discusses loops in C++, explaining their purpose and types, including for loops, while loops, and do-while loops. It provides examples of each loop type, demonstrating their use in tasks such as counting, summing squares, and prompting for user input. Additionally, it includes exercises for calculating sums and generating multiplication tables.

Uploaded by

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

05 - Loops

The document discusses loops in C++, explaining their purpose and types, including for loops, while loops, and do-while loops. It provides examples of each loop type, demonstrating their use in tasks such as counting, summing squares, and prompting for user input. Additionally, it includes exercises for calculating sums and generating multiplication tables.

Uploaded by

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

ECE1101 - Programming Fundamentals & Problem Solving

Lecture 5

Loops in C++

Dr. Mona Nashaat


What are Loops?
- In programming, loops are used to execute a block of code
repeatedly based on a condition.
- This is useful when a task needs to be repeated multiple times,
such as reading inputs, processing arrays, or generating output
patterns.

2
Types of Loops in C++
- C++ provides three primary loop constructs:
1. for loop
2. while loop
3. do-while loop

3
for Loop

for (initialization; condition; update) {


// statements
}

- Example:

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


cout << "Count: " << i << endl;
}

- What does the code print?

4
Example
- Reverse counting

for (int i = 10; i >= 1; i--) {


cout << "Countdown: " << i << endl;
}

5
Example
- Print even numbers from 1 to 20

for (int i = 2; i <= 20; i += 2) {


cout << i << " ";
}

6
Example
- Sum of squares

int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i * i;
}
cout << "Sum of squares = " << sum;

7
while Loop

while (condition) {
// statements
}

- Example

int i = 1;
while (i <= 5) {
cout << "Count: " << i << endl;
i++;
}
- The number of iterations is not known in advance and depends
on a condition. 8
Example

int number;
cout << "Enter numbers (0 to stop): ";
cin >> number;

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

9
do-while Loop

do {
// statements
} while (condition);

- Example

int i = 1;
do {
cout << "Count: " << i << endl;
i++;
} while (i <= 5);

- The loop will execute at least once regardless of the condition.


10
Example
- Simple password prompt

string password;
do {
cout << "Enter password: ";
cin >> password;
} while (password != "admin123");

cout << "Access granted.\n";

11
Exercise
- Write a program to calculate the sum of numbers from 1 to n
using a loop.

12
Exercise
- Write a program to calculate the sum of numbers from 1 to n
using a loop.

int n, sum = 0;
cout << "Enter a number: ";
cin >> n;
for (int i = 1; i <= n; i++) {
sum += i;
}
cout << "Sum = " << sum;

13
Exercise
- Write a program to print the multiplication table of a number.
- Write a program to find the factorial of a given number using a
loop.

14

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy