0% found this document useful (0 votes)
22 views24 pages

While Loop Slides

This document contains lecture notes on repetition using while loops in Python. It begins with an outline of the lecture topics, which include the structure of while loops, using loop counters, and how to exit a loop using break. Examples are provided to illustrate how to use while loops to repeat code a set number of times or indefinitely. Tasks are included for students to practice writing while loop code, such as a program that repeatedly prompts for a password until the correct one is entered.

Uploaded by

Cherub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views24 pages

While Loop Slides

This document contains lecture notes on repetition using while loops in Python. It begins with an outline of the lecture topics, which include the structure of while loops, using loop counters, and how to exit a loop using break. Examples are provided to illustrate how to use while loops to repeat code a set number of times or indefinitely. Tasks are included for students to practice writing while loop code, such as a program that repeatedly prompts for a password until the correct one is entered.

Uploaded by

Cherub
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 24

FY-C-1004-COMPUTTIONAL

THINKING& CODING
CHAPTER ???
REPETITION (WHILE LOOP)

CHAPTER ??? CLO9: Develop computer


algorithms to solve real life
programming problems
LECTURE NOTES

¡ Contents of lectures are based on the textbook, recommended text, and supplementary material
¡ Please read supplementary material from page
¡ 19 to 20

¡ Please read textbook chapter from page


¡ 96 to 102

2
1. THE WHILE LOOP

2. FLOW CHART OF ALGORITHM

3. IMPLEMENT WHILE LOOP ALGORITHM

4. TRACE PROGRAM CODE LECTURE


5. SOLVE APPLIED PROBLEMS FROM VARIOUS SUBJECT AREAS OUTLINE
6. TASK

7. EXERCISES

KEY TERMS

3
LECTURE OBJECTIVES

¡ At the end of lecture, the student should be able to:


¡ Apply a while statement to repeat set of statements.
¡ Understand and apply different types of while loops.
¡ Write Python code to solve various computational problems that require the use of the while statement.
¡ Define Algorithms for Repetition to assist in writing code

4
WHY DO WE NEED LOOPS?

To repeat actions in the program several times, we use the loops.


A loop is an algorithm structure that executes a sequence of instructions multiple times.

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

• iterate - to repeat in order to achieve, or get closer to, a


desired goal.

• 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:

¡while [condition]: # checking the loop condition


¡ action1 # loop body (actions that are repeated)
¡ action2 # is executed WHILE the condition is met
¡ ... # each line in the body of the loop is
¡indented - 4 spaces
¡ actionN

7
3.WHILE CONDITION:

#code in while block


#code in while block

What’s happening above?


1.If the CONDITION is True, then the code in the while block runs

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.

The while loop consists of a head and a loop body.


In the heading, after the word while, a condition is written under which the loop continues to run in
parentheses. When this condition is violated (becomes false), the cycle ends.

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?

•How do we run our loop a specific number of times?

•Loop counters! ( It’s just a variable)


x=0
•Limit the while condition using the loop counter
while x < 5:

• The variable counts the number of times you run


x=x+1

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”)

LOOP ¡ # Execute above code


¡ # What is the output? Why?

16
¡ You can use the keyword break Example:
¡ x=0
¡ while x < 1000000:
¡ print(x)
7. HOW DO WE ¡ if x == 5:

EXIT A LOOP? ¡ break

¡ x += 1

¡ What’s happening above?


¡ Counter variable “x” increases, but if x == 5, then break
exits the loop

17
6.TASK 1

Write a program that asks for a password until "qwerty" is entered.


It is often, impossible to say in advance how many times an operation
needs to be performed, but it is possible to determine the condition
under which it should end.
In this program, the user can enter the password incorrectly; then, the
program will report an error and ask for it again until the correct
password is entered.
To solve this problem, we must use a loop condition to validate the
18

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

Calculate the sum of the sequence 1 + 3 + 5 + ... + n

You can use a loop to calculate the amount.

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.

The initial value of the variable i is 1, the final value is n.


20
TO CALCULATE THE AMOUNT, WE WILL USE
THE FORMULAS:

¡ 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

You might also like