---
# System prepended metadata

title: 'Beginner : Iteration - While Loops '

---

# Iteration - while loops

[Class
recording](https://www.scaler.com/meetings/i/beginner-iteration-while-loops-6/archive)

[lecture
notes](https://drive.google.com/file/d/1aNoe7EzTVerq4CZsmtaHgT5UhG-avXMW/view?usp=share_link)





## **Content**

1.  Need for loops
2.  While loop - structure
3.  Example questions





## Meanwhile when people are joining (5-7 mins)

-   Instructor's welcome.
-   Breaking ice with students.
-   casual conversation to bond.


---
title: Motivation behind while loops
description: 
duration: 200
card_type: cue_card
---

## Motivation behind while loops





### **Number Lock**





-   In a number lock with 3 digit key, one would have to iterate digit
    by digit repetitively until the lock opens, loops can be used here
    with appropriate condition.





![download_1.png](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/009/original/download_%2810%29.png?1689591517)





-   On using only conditional statements we would have to check 1000
    conditions(from 000 -999)

```python=
While lock is not open:
    keep trying
```

-   Start with 000, if it doesnt work, increase by 1, try again, until
    lock opens.





![download.png](https://d2beiqkhq929f0.cloudfront.net/public_assets/assets/000/040/010/original/download_%2811%29.png?1689591537)


---
title: Explaining While loop
description: 
duration: 200
card_type: cue_card
---


### **Initialization**

-   where the loop has to start from.
-   if we know the password is \>500 we wuld start trying from 501.

``` python=
combnation=0
```





### **Condition**

-   check the condition where the loop has to stop

``` python=
while(lock still closed): #combination!= actual password
  try this combination
```





### **Updation** update the iterator variable.

``` python=
  combination+=1
```


---
title: Using While loop
description: 
duration: 600
card_type: cue_card
---


## **Use while loop to print numbers from 1 to 5**
Code:
``` python=
num=1
while(num<=5):
  print(num)
  num+=1
```
>Output
```bash=
1
2
3
4
5
```



## **Question**: Take an input from user and print all the alternate

Code
``` python=
number = int(input("Please input a number - "))
i = 1
while i <= number:
    print(i)
    i += 2
```

>Output
```bash=
Please input a number - 10
1
3
5
7
9
```


## **Question**: Take an input from the user for start and end values and print every third number
Code:
``` python=
start = int(input())
end = int(input())
while start <= end:
    print(start)
    start += 3
```

>Output
```bash=
10
20
10
13
16
19
```







## **Question**: Take an input from the user for start and end values. Print all even numbers between them.

-   Start at the number and print if the number is divisible by 2,
    increment number by 1

Code:
``` python=
start = int(input())
end = int(input())
loop_count = 0
while start <= end:
    loop_count += 1
    if start % 2 == 0:
        print(start)
    start += 1
print("Loop was run", loop_count,"times!")
```

>Output
```bash=
10
20
10
12
14
16
18
20
Loop was run 11 times!
```


-   If number is even, start from number, if odd, start from number+1,
    print number, increment by 2

Code
``` python=
start = int(input())
end = int(input())
if start % 2 == 0:
    i = start
else:
    i = start +1
loop_count = 0
while i <= end:
    loop_count += 1
    print(i)
    i += 2
print("Loop was run", loop_count,"times!")
```

>Output
```bash=
10
20
10
12
14
16
18
20
Loop was run 6 times!
```

-   Second answer is more optimized, since the loop runs less number of
    times.


## **Question**: You are a bowler and you have to bowl 1 over. (6 balls) Print "Bowling ball number - ball_number" where ball_number is the number of current ball

Code
``` python=
ball_number = 1
while ball_number <= 6:
    print("Bowling ball number -", ball_number)
    ball_number += 1
```


>Output
```bash=
Bowling ball number - 1
Bowling ball number - 2
Bowling ball number - 3
Bowling ball number - 4
Bowling ball number - 5
Bowling ball number - 6
```



## **Question**: Print all numbers from 1 to N (input from user) in a single line.

-   Use `end =" "` to print space seperated values.




Code
``` python=
number = int(input())
i = 1
while i <= number:
    print(i, end=" ")
    i += 1
```


>Output
```bash=
7
1 2 3 4 5 6 7 
```

---
title: Explaining end in print statement
description: 
duration: 300
card_type: cue_card
---

## `end` in print





-   `end` parameter can take values such as `" "`, `\t`, `\n` etc.



Code

``` python=
print("random", end="ANYTHING_AT_ALL")
print("print", end="ANYTHING_AT_ALL")
print("statements!",end="ANYTHING_AT_ALL")
```

>Output
```bash
randomANYTHING_AT_ALLprintANYTHING_AT_ALLstatements!ANYTHING_AT_ALL
```


---
title: Explaining sep in print statement
description: 
duration: 300
card_type: cue_card
---


## `sep` in print

-   Seperator works like end but for values in the same print statement.


Code
``` python=
print("these", "are", "multiple", "strings", 45, 67.8, sep="!WHATEVER!")
```

>Output
```bash    these!WHATEVER!are!WHATEVER!multiple!WHATEVER!strings!WHATEVER!45!WHATEVER!67.8
```

---
title: Revising previous concepts
description: 
duration: 300
card_type: cue_card
---


## **Question**: Given a number, print it's multiplication table!




Code
``` python=
number = int(input())
i = 1
while i <= 10:
    print(number*i, end=" ")
    i += 1
```


>Output
```bash=
10
10 20 30 40 50 60 70 80 90 100 
```







## **Question**:

1.  Take number of test_cases (count) from a user. (Count of numbers)
2.  Take int(count) inputs from the user
3.  Print multiplication table for all these inputs!


``` python=
# 3

# 4
# multiplication table of 4
# 5
# multiplication table of 5
# 6
# multiplication table of 6
```



Code
``` python=
test_cases = int(input())
t = 1
while t <= test_cases:
    number = int(input("Please enter the number -"))
    i = 1
    print("Printing multiplication table of -", number)
    while i <= 10:
        print(number*i, end=" ")
        i += 1
    print("\n")
    t += 1
```


>Output
```bash=
2
Please enter the number -12
Printing multiplication table of - 12
12 24 36 48 60 72 84 96 108 120 

Please enter the number -4
Printing multiplication table of - 4
4 8 12 16 20 24 28 32 36 40 
```



---
title: Quiz-1
description: Quiz-1
duration: 60
card_type: quiz_card
---

# Question


**Which of the following statements is true about while loops in
Python?**
# Choices
- [ ] A while loop executes a specific number of times
- [x] A while loop executes until a specific condition is met
- [ ] While loops are not used in Python





---
title: Quiz-2
description: Quiz-2
duration: 60
card_type: quiz_card
---

# Question


**What will be the output of the following code?**
```python=
x = 1
while x <= 100:
    if x % 3 == 0 and x % 5 == 0:
        print("FizzBuzz")
    elif x % 3 == 0:
        print("Fizz")
    elif x % 5 == 0:
        print("Buzz")
    else:
        print(x)
    x += 1
```
# Choices

- [ ] FizzBuzz
- [ ] Fizz
- [ ] Buzz
- [x] A combination of Fizz, Buzz, and/or integers






---
title: Quiz-3
description: Quiz-3
duration: 60
card_type: quiz_card
---

# Question

**What is the output of the following code?**
```python=
count = 0
while True:
    if count == 5:
        break
    print(count)
    count += 1
```
# Choices
A:
```bash=
0
1
2
3
4
```

B: 5

C: an infinite loop

- [x] A
- [ ] B
- [ ] C