# Loops 2 - Nested Loops --- title: Agenda description: duration: 300 card_type: cue_card --- ### Agenda 1. Jump Statements 1. Pass 1. Continue 1. Break 2. Nested Loops 3. GCD 4. LCM --- title: Quick Revision of Loops 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: Pass description: duration: 900 card_type: cue_card --- ### `pass` - `pass` acts as a placeholder. - It acts as an empty block. Code ```python= if True: pass # represents an empty block / statement in Python ``` <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/060/229/original/download.png?1703660197" width=600 height=300> \ Code ```python= for i in range(5): if i == 3: pass print(i) #0 1 2 3 4 ``` > Output 0 1 2 3 4 --- title: Continue description: duration: 900 card_type: cue_card --- ### `continue` - Disregards the code after the `continue` statement and goes to the next iteration. - It is effectively used `if` 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 <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/060/230/original/download_%281%29.png?1703660215" width=600 height=200> <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/060/231/original/download_%282%29.png?1703660256" width=600 height=100> --- title: Quiz-1 description: Quiz-1 duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ```python= x = 0 while x < 5: x += 1 if x == 3: continue print(x) ``` A. ``` 1 2 3 4 5 ``` B. ``` 1 2 4 5 ``` C. ``` 1 2 3 4 ``` D. ``` 0 1 2 3 4 5 ``` # Choices - [ ] A - [x] B - [ ] C - [ ] D --- title: Break description: duration: 900 card_type: cue_card --- ### `break` - When `break` is encountered, the control goes out of the loop and 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 <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/060/232/original/download_%283%29.png?1703660277" width=600 height=350> ### **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 the 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-2 description: Quiz-2 duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ```python= x = 0 while x < 5: x += 1 if x == 3: break print(x) print("Loop finished") ``` A. ``` 1 2 ``` B. ``` 1 2 Loop finished ``` C. ``` 1 2 3 ``` D. ``` 1 2 3 Loop Finished ``` # Choices - [ ] A - [x] B - [ ] C - [ ] D --- title: Quiz-3 description: Quiz-3 duration: 60 card_type: quiz_card --- # Question What will be the output of the following code? ```python= count = 0 while True: if count == 5: break print(count) count += 1 ``` A. ``` 0 1 2 3 4 ``` B. 5 C. An infinite loop # Choices - [x] A - [ ] B - [ ] C --- title: Break & Doubt Resolution description: duration: 600 card_type: cue_card --- #### Quiz-2 Explanation - The given code defines a while loop with the condition `True`, which means it will run indefinitely until explicitly broken out of. - Inside the loop, there is an `if` statement that checks if the count variable is equal to **5**. - If this **condition is true**, the `break` statement is executed, which terminates the loop. - Initially, the `count` variable is set to **0**. - The loop iterates, printing the current value of count and then increments it by **1** in each iteration. - This process continues until the value of count becomes 5. At that point, the `break` statement is executed, and the loop is exited. ### Break & Doubt Resolution `Instructor Note:` * Kindly take this time (up to 5-10 mins) to give a short break to the learners. * Meanwhile, you can ask the them to share their doubts (if any) regarding the topics covered so far. --- title: Quiz-4 description: Quiz-4 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 these --- title: Nested Loops description: duration: 900 card_type: cue_card --- ### Nested Loops 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 **6** (i, j) pairs. <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/060/233/original/download_%284%29.png?1703660299" width=600 height=350> ### **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 the provided input. 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: Quiz-5 description: Quiz-5 duration: 60 card_type: quiz_card --- # Question What will be the output of the following code snippet? ```python= for i in range(3): for j in range(i, 3): print("*", end=" ") print() ``` A. ``` * * * * * * ``` B. ``` * * * * * * * * * ``` B. ``` * * * * * * ``` # Choices - [x] A - [ ] B - [ ] C - [ ] None of these --- title: GCD of two numbers description: duration: 900 card_type: cue_card --- ## **GCD** ### **Question:** Given two integer inputs, write a program to calculate the GCD of these two numbers. Example Input: 16 24 Example Output: 8 <img src="https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/060/234/original/download_%285%29.png?1703660510" width=600 height=450> \ 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 --- title: LCM of two numbers description: duration: 900 card_type: cue_card --- ## **LCM** ### **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: Practice Coding Question(s) description: duration: 600 card_type: cue_card --- ### Practice Coding Question(s) You can pick the following question and solve it during the lecture itself. This will help the learners to get familiar with the problem solving process and motivate them to solve the assignments. <span style="background-color: pink;">Make sure to start the doubt session before you start solving the question.</span> Q. https://www.scaler.com/hire/test/problem/96306/