Day 4 - While and For Loops
Day 4 - While and For Loops
loop
num = 1
while num < 8:
num = num *2 num = num * 2 print(count)
count = count + 1
print(count)
Output:
3 count = count +1 End
FOR LOOP for num in
End of loop
SYNTAX FLOW CHAT [2,4,6] when the list
for </condition>:
• num starts with 2 is exhausted
</action code>
Example:
for num in [2, 4, 6]:
print( num*10 )
Output:
LOOPING
20 # for the 1st loop num = 2. num*10 = 20
40 # for the 2nd loop num = 4. num*10 = 40 num takes up a
60 # for the 3rd loop num = 6. num*10 = 60 next value prints out num
* 10
4 or 6
NB: num is a variable whose value changes per loop.
For loops are relevant when you want o repeat an
action or code for a specific number of times.
KEYWORD – BREAK & CONTINUE
➢The “break” key word is Code example
used to stop or terminate an
entire looping sequence. words = [ “an”, 67, “mouse”, “head”, “the”, 90, 0.1 “apple” ]
➢The “continue” key word is
for word in words:
used to terminate a current
loop so that the next loop
if (word is int) or (word is float):
could begin
continue
if len( word ) == 3:
EXAMPLE: print(word)
Find the first occurrence of a break
3 letter word in the given list
of words. >> ‘the’
VIDEO ON ITERATIVE PROGRAMMING – LOOPS
The video will be made available on YouTube and our official Telegram group.
NESTED LOOPS Code example:
lst = [ [2, 4], [78, 10, 34], [0.9, 2, 34, 50]]
Similarly, let assume that a user enters the value 23 into our program.
18 19 20 21 22 23 24 25 26 27 26 29 30
Both 19 and 29 are the prime numbers around 23 however 19 is the nearest.
SOLUTION TO TUTOR EXAMPLE 1, 2 &3 FOR DAY 4
The video will be made available on YouTube and our official Telegram group.
WATCH OUT FOR PROJECT 4
Project will be posted in our official Telegram group.
Only qualified participants will have access to the project.
THANK YOU
YO U H AV E C O M P L E T E D
D AY 4
OF OUR 2023 BOOTCAMP 3.0