While Loop Slides
While Loop Slides
THINKING& CODING
CHAPTER ???
REPETITION (WHILE LOOP)
¡ Contents of lectures are based on the textbook, recommended text, and supplementary material
¡ Please read supplementary material from page
¡ 19 to 20
2
1. THE WHILE LOOP
7. EXERCISES
KEY TERMS
3
LECTURE OBJECTIVES
4
WHY DO WE NEED LOOPS?
We have already learned how to check a condition in a program. But what if certain actions are repeated several
times? Of course, you can write program code for each of these actions. But if these actions are repeated dozens,
hundreds, thousands, millions of times, then our program will be very long.
1. THE WHILE LOOP – KEY VOCABULARY
• while
loop - a programming construct used to repeat a set of
commands (loop) as long as (while) a boolean condition is true.
6
2. A PRECONDITIONED LOOP HAS THE FOLLOWING STRUCTURE:
7
3.WHILE CONDITION:
2.At the end of the while block, the computer automatically goes back to the while
CONDITION line
3.It checks if the CONDITION is True, then repeats the while block if it is
8
.
3. WHILE LOOP CONCEPTS
There are a few more important concepts to know:
• The body of the loop is the sequence of code that needs to be executed
several times.
• One-time execution is iteration.
9
4. FEATURES OF THE WHILE LOOP:
The while loop is used when the number of loop repetitions is not known in advance and cannot be calculated.
In this condition, you can use the signs of logical relations and operations, as in
the Conditional operator.
If the condition is incorrect initially, then the loop will not be executed even once.
10
4. FEATURES OF THE WHILE LOOP:(CONT)
If the condition never becomes false (false), then the loop will never end; in this
case, they say that the program is"infinite looped ").
In the C language, any number that is not equal to zero denotes a true condition,
and zero denotes a false word:
while True: # starts an infinite loop
...
while False: # the loop will not be executed even once
11
4. HOW DO WE LOOP COUNT?
12
¡ x=0
¡ while x < 5:
¡ print(“hello”)
¡ x = x + 1 #shortcut: x += 1
LOOP
¡ What’s happening above?
COUNTING
¡ 1.x is initialized to 0, which is less than 5
EXAMPLE ¡ 2.Check if x < 5 isTrue
¡ 3.while block runs (notice that x = x + 1 is indented)
¡ 4.x increases to 1 (because 0 + 1 = 1 is saved back in
x)
¡ 5.Go back up to check if the while condition isTrue
13
¡x=0
¡while x < 10:
¡print(x**2)
EXAMPLE ¡x += 1
¡# Execute above code
¡# What is the output? Why?
14
¡x=1
¡N = 1000
¡while x < N:
1.ANOTHER
¡print(x)
EXAMPLE ¡x *= 2
¡# Execute above code
¡# What is the output? Why?
15
¡ while True:
AN INFINITE ¡ print(“All work and no play makes Jack a dull boy”)
16
¡ You can use the keyword break Example:
¡ x=0
¡ while x < 1000000:
¡ print(x)
7. HOW DO WE ¡ if x == 5:
¡ x += 1
17
6.TASK 1
password after each input. For this, the password will be entered at the
beginning of the program and inside the loop.
¡ print ("Enter password:")
¡ password = input () # enter password, set the first value while
password! = "qwerty": # check the condition of the loop
¡ print ("The password is incorrect!") print ("Enter password:")
¡ password = input () # re-enter password
¡ print ("Welcome!") # output text when entering password "qwerty"
TASK 1
19
TASK 2
In this sequence, you can notice that each next term is increased by 2.
Let us denote the term by the variable i and will change it in the loop.
¡ sum = sum + i
¡ i=i+2
¡ summa = 0 # initial value of the sum
¡ i = 1 # initial value of the loop parameter
¡ n = int(input ()) # input of the final value of the loop parameter
¡ while i <= n: # loop condition "while i <= n"
¡ summa = summa + i # increase the sum by i
¡ i = i + 2 # increase the loop parameter by 2
¡ print(summa) # output the value of the sum
21
WHILE LOOP
ALGORITHM AND FLOW CHART
KEY TERMS
INFINITE LOOP
BREAK STATEMENT
22
FORMATIVE KAHOOT!
QUIZ1
ASSESSMENT
23
24