0% found this document useful (0 votes)
4 views

Week 3_ Loops

The document provides an overview of loops in programming, including types such as while, do-while, and for loops, along with jump statements like break and continue. It explains the structure and functionality of each loop type, including examples and exercises for practice. Additionally, it covers concepts like infinite loops and nested loops, providing code snippets to illustrate their usage.

Uploaded by

lujainhesham04
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)
4 views

Week 3_ Loops

The document provides an overview of loops in programming, including types such as while, do-while, and for loops, along with jump statements like break and continue. It explains the structure and functionality of each loop type, including examples and exercises for practice. Additionally, it covers concepts like infinite loops and nested loops, providing code snippets to illustrate their usage.

Uploaded by

lujainhesham04
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/ 12

Loops

Agenda
1. Introduction.
2. Types of loops:
a. while loop.
b. do-while loop.
c. for loop.
3. Jump statements.
a. break.
b. continue.
4. Infinite loop.
5. Nested loops.
Exercise:

Print all numbers from 1 to 9.

Code:

int main() {
cout << 1 << endl;
cout << 2 << endl;
cout << 3 << endl;
cout << 4 << endl;
cout << 5 << endl;
cout << 6 << endl;
cout << 7 << endl;
cout << 8 << endl;
cout << 9 << endl;
}

Now print all numbers from 1 to 1000.

Sometimes you may need to run the same piece of code more than once. Normally,
the computer runs your code one line at a time, starting from the first line and
moving down to the last line.
● Programming languages have different tools that help you run your code in
more advanced ways.
● A loop statement lets you run a single statement or a group of statements many
times. Most programming languages follow a general structure for loop
statements.

Flowchart:
Type of Loops:

● While loop:

The while loop executes a block of code repeatedly while a certain condition
is true.

Syntax:

while (condition) {
//code
}

Example:

Print numbers from 1 to 10.

Solution:

#include <iostream>
using namespace std;

int main() {
int x = 1;
while (x <= 10) {
cout << x << " ";
x++;
}
cout << endl;
return 0;
}
● do-while loop:

The do-while loop executes the block of code once, before checking if the
condition is true, then it repeats the block of code as long as the condition is
true.

Syntax:

do {
//code
} while (condition);

Example:

#include <iostream>
using namespace std;

int main() {
int x = 1;
do {
cout << x << " ";
x++;
} while (x <= 10);
cout << endl;
return 0;
}

While loop vs Do-while loop:


● For loop:

The for loop is designed to iterate a number of times.

How it works:

Syntax:

int main() {
for (initialization; condition; inc/dec) {
// statement;
}
}

The for loop is similar to the while loop in that it repeats a statement as long
as a condition is true. However, the "for" loop has specific locations for the
initialization and the increase/decrease expression, which are executed
before the loop starts and after each iteration, respectively.
For loop flow:

1. The loop starts with an initialization step, where a counter variable is


declared and set to an initial value. This step is executed only once, at
the beginning of the loop.

2. The loop then checks a condition, which is usually a comparison


between the counter variable and some other value. If the condition is
true, the loop continues; otherwise, the loop ends and the program
moves on to the next statement after the loop.

3. If the condition is true, the loop executes a statement, which can be


either a single statement or a block of statements enclosed in curly
braces.

4. After the statement is executed, the loop executes an


increment/decrement operation on the counter variable. This step is
executed after each iteration of the loop.

5. The loop then goes back to step 2, where it checks the condition again.
If the condition is still true, the loop repeats steps 3-5. If the condition
is false, the loop ends and the program continues with the next
statement after the loop.

Overall, the for loop provides a compact way to repeat a block of code a
specific number of times, while also giving you control over the
initialization and increment/decrement of the loop counter variable.

Examples :

Print numbers from 1 to 5 using for loop:

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


cout << i << "\n";
}
Print even numbers between 0 and 10:

for (int i = 0; i <= 10; i = i + 2) {


cout << i << "\n";
}

Jump statements:

● break:

This statement ends the loop immediately and moves the program's
execution to the first statement right after the loop.

Example

int main() {
int a = 0;
do {
cout << a << " ";
a = a + 1;
if (a > 15) {
break;
}
} while (a < 20);
}

Output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

● continue:

The "continue" statement lets you skip the current iteration of a loop and
move on to the next one. You can use it with an "if" statement to check a
condition and decide whether to continue or not.

Example:

int main() {
int a = 0;
do {
if (a == 15) {
a = a + 1;
continue;
}
cout << a << " ";
a = a + 1;
}
while(a < 20);
}

Output: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 17 18 19

What is the output:

int main() {
int a = 0;
do {
cout << a << endl;
a = a + 1;
} while (true);
}
Infinite loop:

If a condition never becomes false, a loop can turn into an infinite loop, which runs
indefinitely and can cause a program to crash or freeze.

Nested Loops:

A nested loop is a programming construct in which a loop is contained within


another loop. Loops are used to execute a set of instructions repeatedly. When one
loop is nested inside another loop, the inner loop is executed repeatedly for each
outer loop iteration.

Example:

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


cout << "Outer: " << i << "\n";
for (int j = 1; j <= 3; ++j) {
cout << "Inner: " << j * i << "\n";
}
}

How it works:
Example :

Write a program to display 7 days of 3 weeks.

Code:

int main() {
int weeks = 3, days_in_week = 7;

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


cout << "Week: " << i << endl;

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


cout << " Day:" << j << endl;
}
}
return 0;
}

Output:

Week: 1
Day:1
Day:2
Day:3
Day:4
Day:5
Day:6
Day:7
Week: 2
Day:1
Day:2
Day:3
... ... ..
Exercise:

print all numbers from 2 to n that aren't primes. (2 <= n <= 100)

Input:
8
Output:
4 6 8

Code:

int main () {
int n;
cin >> n;
for (int i = 2; i <= n; i++) {
bool prime = true;
for(int j = 2; i < i; j++) {
if (i % j == 0) {
prime = false;
break;
}
}
if (!prime)
cout << i << " ";
}
}

To solve:
● Simple array sum

● ONE MORE

● Single use

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