Week 3_ Loops
Week 3_ 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:
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;
}
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:
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;
}
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:
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 :
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
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:
Example:
How it works:
Example :
Code:
int main() {
int weeks = 3, days_in_week = 7;
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