0% found this document useful (0 votes)
10 views23 pages

WHILE LOOP Jam 1

The document explains while loops, a programming structure used to repeat statements based on a condition's truth value. It details the syntax, how to increment and decrement variables in Python, and provides examples of while loops, including handling user input and infinite loops. Additionally, it discusses the use of break statements to exit loops and common errors in loop conditions.

Uploaded by

jbl.libra009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

WHILE LOOP Jam 1

The document explains while loops, a programming structure used to repeat statements based on a condition's truth value. It details the syntax, how to increment and decrement variables in Python, and provides examples of while loops, including handling user input and infinite loops. Additionally, it discusses the use of break statements to exit loops and common errors in loop conditions.

Uploaded by

jbl.libra009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

WHILE LOOP

Presented by: Jomar Lazar


What is While Loop?
While loops are very
powerful programming
structures that you can
use in your programs to
repeat a sequence of
statements.
How While Loops
Work?
Let's break this down in more detail:
• The process starts when a while loop is found
during the execution of the program.
• The condition is evaluated to check if it's True
or False.
• If the condition is True, the statements that
belong to the loop are executed.
• The while loop condition is checked again.
• If the condition evaluates to True again, the
sequence of statements runs again and the
process is repeated.
• When the condition evaluates to False, the loop
stops and the program continues beyond the
loop.
General Syntax of While
Loops
These are the main elements (in
order):
• The while keyword (followed by a space).
• A condition to determine if the loop will
continue running or not based on its
truth value (True or False)
• A colon (:) at the end of the first line.
• The sequence of statements that will be
repeated. This block of code is called the
"body" of the loop and it has to be
indented. If a statement is not indented,
it will not be considered part of the loop
(please see the diagram below).
Increment and Decrement
operators in Python
Python does not allow using the “(++ and –)” operators. So, the “++” and “–” symbols
do not exist in Python.
• In python, if you want to increment a variable we can use “+=” or we can simply
reassign it “x=x+1” to increment a variable value by
x = 21
x += 1 or x = x + 1
print(x)
Output: 22

• To decrement a variable in python we can use “-=” or “x=x-1” operators in python to


reduce the value of a variable by 1.
x = 21
x -= 1 or x = x - 1
print(x)
Output: 20
Examples of While
Syntax:
Loops
How a Basic While Loop i=4
Works while i < 8:
• Here we have a basic print(i)
while loop that prints the Output:
value of i while i is less i += 1
than 8 (i < 8): 4
5
6
7
Let's see what happens behind the
scenes when the code runs:
• Iteration 1: initially, the value of i is 4, so the condition i <
8 evaluates to True and the loop starts to run. The value
of i is printed (4) and this value is incremented by 1. The
loop starts again.
• Iteration 2: now the value of i is 5, so the condition i <
8 evaluates to True. The body of the loop runs, the value
of i is printed (5) and this value i is incremented by 1. The
loop starts again.
• Iterations 3 and 4: The same process is repeated for the
third and fourth iterations, so the integers 6 and 7 are
printed.
• Before starting the fifth iteration, the value of i is 8. Now
the while loop condition i < 8 evaluates to False and the
loop stops immediately.
User Input Using a While Loop
• Now let's see an example of a while loop in a program that takes user Output:
input. We will the input() function to ask the user to enter an integer Enter an integer: 3
and that integer will only be appended to list if it's even. Enter an integer: 4

This is the code: Enter an integer: 2


# Define the list
nums = [] Enter an integer: 1
# The loop will run while the length of the
# list nums is less than 4 Enter an integer: 7
while len(nums) < 4: Enter an integer: 6
# Ask for user input and store it in a variable as an integer.
user_input = int(input("Enter an integer: ")) Enter an integer: 3
# If the input is an even number, add it to the list Enter an integer: 4
if user_input % 2 == 0:
nums.append(user_input)
This table summarizes what happens behind the
scenes when the code runs:
Tips for the Condition in
While Loops
For example, common errors include:

•Using < (less than) instead of <= (less


than or equal to) (or vice versa).
•Using > (greater than) instead
of >= (greater than or equal to) (or vice
versa).

This can affect the number of iterations


of the loop and even its output.
Let's see an example:

If we write this while loop with the


condition i < 9:
This table illustrates what happens
behind the scenes when the code
i=6 runs:
while i < 9:
print(i)
i += 1

Output:
6
7
8

The loop completes three iterations and it


stops when i is equal to 9.
Example:

i=6 This table illustrates what


while i <= 9: happens behind the scenes:
print(i) i += 1

We see this output:


6
7
8
9

The loop completes one more iteration


because now we are using the "less than
or equal to" operator <= , so the
condition is still True when i is equal to 9.
Infinite While Output:

Loops Hello, World!


Hello, World!
Hello, World!
Infinite loops are typically the Hello, World!
result of a bug, but they can also Hello, World!
Hello, World!
be caused intentionally when we
Hello, World!
want to repeat a sequence of Example: Hello, World!
statements indefinitely until # Define a variable Hello, World!
Hello, World!
a break statement is found. i=5
Hello, World!
# Run this loop while i is less
Hello, World!
If we don't do this and the than 15 Hello, World!
while i < 15: Hello, World!
condition always evaluates to True, # Print a message Hello, World!
then we will have an infinite loop, print("Hello, World!") Hello, World!
which is a while loop that runs Hello, World!
.
indefinitely.
.
.
# Continues indefinitely
This is one possible solution,
incrementing the value of i by 2 on
To stop the program, we will need
every iteration:
to interrupt the loop manually by
pressing CTRL + C. i=5
When we do, we will see while i < 15:
a KeyboardInterrupt error similar to this print("Hello, World!")
one:
# Update the value of i
i += 2

Output:
Hello, World!
Hello, World!
Hello, World!
Hello, World!
Hello, World!
How to Make an Infinite Loop
with While True
We can generate an infinite
loop intentionally using while
True. In this case, the loop will run
indefinitely until the process is
stopped by external intervention
(CTRL + C) or when
a break statement is found (you
will learn more about break in just
a moment).
How to Make an Infinite Loop with While
True
This is the basic logic of the break statement:
• The while loop starts only if the condition evaluates to True.
• If a break statement is found at any point during the execution of the
loop, the loop stops immediately.
• Else, if break is not found, the loop continues its normal execution and
it stops when the condition evaluates to False.
• We can use break to stop a while loop when a condition is met at a
particular point of its execution, so you will typically find it within a
conditional statement, like this:
while True:
# Code
if <condition>:
break
# Code
• This stops the loop immediately if the condition is True.
Here we have an example of break in
a while True loop:
Output:
Enter an integer: 4
This number is even
while True:
Enter an integer: 6
user_input = int(input(“Enter an
This number is even
integer: “))
Enter an integer: 8
if user_input % 2 != 0:
This number is even
print(“This number is odd”)
Enter an integer: 3
break
This number is odd
print(“This number is even”)

Process finished with exit code 0


Another examples of while loop
1. While loop Condition 2. While loop Condition with list
y=0 b = [9, 5, 2, 1]
while (y < 4): while b:
y=y+1 print(b.pop())
print("George")
Output:
Output: 1
George 2
George 5
George 9
George
Process finished with exit code 0
Process finished with exit code 0
Examples of while loop
4. Python while loop string condition
3. Python while loop condition at the end
Examples of while
loops 6. Python while loop double condition

5. Python while loop exit condition


Examples of while
loops 8. Python assignment in while loop condition

7. Python while loop check condition


Examples of while
loops
10. Python while loop boolean
9.Python while loop break condition condition
Thank you 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy