# **Iteration - Nested loops + Pattern Printing** (2 hours)
------------------------------------------------------------------------
[Class
recording](https://www.scaler.com/meetings/i/beginner-iteration-problem-solving-6/archive)
[lecture
notes](https://drive.google.com/file/d/1A53_p_kS_j7YpqCe--02NJXh9FBkFBH8/view?usp=share_link)
## **Content**
1. Jump statements
2. Pass
3. continue
4. break
5. Nested loops
6. GCD LCM
## **Meanwhile when people are joining (5-7 mins)**
- Instructor's welcome.
- Breaking ice with students.
- casual conversation to bond.
---
title: Questions
description:
duration: 600
card_type: cue_card
---
#### Question: Write a loop to print the following numbers - 1,4,7,10,13,16
Code:
``` python=
for i in range(1,17,3):
if i == 16:
print(i, end = "")
else:
print(i, end = ", ")
```
>Output:
```
1, 4, 7, 10, 13, 16
```
#### Question: Write a loop to print the following numbers - 1,3,7,13,21,31,43
Code:
``` python=
number = 1
i = 1
while number <= 43:
print(number, end = " ")
number = number + 2*i
i += 1
```
>Output:
```
1 3 7 13 21 31 43
```
---
title: Jump statements
description:
duration: 1400
card_type: cue_card
---
### Jump statements
#### `pass`
``` python
if True:
pass # represents an empty block / statement in python
```
- Pass acts as a placeholder.
- It acts as an empty block.

Code:
``` python=
for i in range(5):
if i == 3:
pass
print(i) #0 1 2 3 4
```
>Output:
```
0
1
2
3
4
```
#### `continue`
- Disregards the code after the 'continue' statement and goes to the next iteration.
- If used with 'if' it effectively used to skip a specific iteration.
Code:
``` python=
for i in range(5):
if i == 3:
continue
print(i) #0 1 2 4
```
>Output:
```
0
1
2
4
```


#### `break`
- When 'break' is encountered , the control goes out of the loop, the loop terminates, regardless of the code that follows.
Code:
``` python=
for i in range(5):
if i == 3:
break
print(i)
```
>Output:
```
0
1
2
```

#### Question: Write a program that continuously asks the user to provide an input number.The program should stop only when the user provides 5.Once the user provides 5. The program should print the number of times an input was provided. Assume that user always provides a number!
Code:
``` python=
counter = 0
while True:
number = int(input())
counter += 1
if number == 5:
break
print("You provided", counter, "inputs!")
```
>Output:
```
3
4
5
You provided 3 inputs!
```
---
title: Quiz-1
description:
duration: 60
card_type: quiz_card
---
# Question
Which statement is used to skip the current iteration of a loop and move on to the next iteration?
# Choices
- [ ] `break`
- [x] `continue`
- [ ] `pass`
- [ ] None of the above
---
title: Quiz-2
description:
duration: 60
card_type: quiz_card
---
# Question
What will be the output of the following code snippet?
```
x = 0
while x < 5:
x += 1
if x == 3:
continue
print(x)
```
# Choices
- [ ] 1
2
3
4
5
- [x] 1
2
4
5
- [ ] 1
2
3
4
- [ ] 0
1
2
3
4
5
---
title: Quiz-3
description:
duration: 60
card_type: quiz_card
---
# Question
What will be the output of the following code snippet?
```
x = 0
while x < 5:
x += 1
if x == 3:
break
print(x)
else:
print("Loop finished")
```
# Choices
- [x] 1
2
- [ ] 1
2
Loop finished
- [ ] 1
2
3
- [ ] 1
2
3
Loop Finished
---
title: Nested loops
description:
duration: 700
card_type: cue_card
---
### Nested loops
#### Structure
</div>
Code:
``` python=
for i in range(3):
for j in range(3):
print(i, j)
```
>Output:
```
0 0
0 1
0 2
1 0
1 1
1 2
2 0
2 1
2 2
```
- For every value of i there will be corresponding j values.
- In the above code, for `i = 0`, `j` runs from **0** to **2**, then for `i = 1`, `j` runs from **0** to **2**, then for `i = 2`, j runs from **0** to **2** again. Therefore there is **9** (i, j) pairs.


#### Question: Write a program to print a `N x N` matrix of `*`
```
Example Input:
3
Example Output:
***
***
***
```
Code:
``` python=
n = int(input())
for i in range(n):
for j in range(n):
print("*", end = " ")
print()
```
Output:
```
3
* * *
* * *
* * *
```
#### Question: Write a program to print a multiplication table till an input **N**.
```
Example Input:
3
Example Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
```
Code:
``` python=
number = int(input())
for i in range(1, number + 1):
for j in range(1, 11):
print(i*j, end = " ")
print()
```
>Output:
```
3
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
```
---
title: GCD and LCM
description:
duration: 800
card_type: cue_card
---
### GCD and LCM
#### Question: Given two integer inputs, write a program to calculate the GCD of these two numbers
```
Example Input:
16 24
Example Output:
8
```

Code:
``` python=
A = int(input())
B = int(input())
X = min(A, B)
for i in range(X, 0, -1):
if A % i == 0 and B % i == 0:
print(i)
break
```
>Output:
```
16
24
8
```
#### Question: Given two integer inputs, write a program to calculate the LCM of these two numbers.
```
Example Input:
6 8
Example Output:
24
```
Code:
``` python=
A = int(input())
B = int(input())
X = max(A, B)
while True:
if X % A == 0 and X % B == 0:
print(X)
break
X += 1
```
>Output:
```
6
8
24
```
---
title: Quiz-4
description:
duration: 60
card_type: quiz_card
---
# Question
What will be the output of the following code snippet?
```
import math
x = 12
y = 18
print(math.gcd(x, y))
```
# Choices
- [ ] 1
- [ ] 2
- [x] 6
- [ ] 12
---
title: Patten Printing
description:
duration: 1400
card_type: cue_card
---
# Patten Printing
## Identifying equations and using them to generate pattern
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/036/789/original/Screenshot_2023-06-14_at_2.12.09_PM.png?1686732178">
Code
```python=
# Example
n = 5
for i in range(n):
for j in range(n):
if i == 0 or j == 0 or i == n-1 or j == n-1:
print("*", end="")
else:
print(" ", end="")
print()
```
Output
*****
* *
* *
* *
*****
Code
```python=
# Example
n = 10
for i in range(n):
for j in range(n):
if i == 0 or j == 0 or i == n-1 or j == n-1 or i == j:
print("*", end="")
else:
print(" ", end="")
print()
```
Output
**********
** *
* * *
* * *
* * *
* * *
* * *
* * *
* **
**********
<img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/036/793/original/Screenshot_2023-06-14_at_2.25.42_PM.png?1686732937">
### Example
*
**
***
****
*****
- Input provided would be number of rows.
- Print `n-i-1` 'spaces' for each row
- Print `i+1` `'*'` following the spaces.
Code
```python=
n = int(input())
for i in range(n):
for j in range(n):
if i + j >= n-1: # using the equation here
print("*", end="")
else:
print(" ", end="")
print()
```
Output
5
*
**
***
****
*****