0% found this document useful (0 votes)
15 views5 pages

Understanding Python While Loops

A while loop in Python repeatedly executes a block of code as long as a specified condition is true. The document explains the syntax, provides examples of printing numbers, user input, and demonstrates the use of break and continue statements. It also warns about infinite loops and includes practice questions for further understanding.

Uploaded by

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

Understanding Python While Loops

A while loop in Python repeatedly executes a block of code as long as a specified condition is true. The document explains the syntax, provides examples of printing numbers, user input, and demonstrates the use of break and continue statements. It also warns about infinite loops and includes practice questions for further understanding.

Uploaded by

alihamamoon622
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python While Loop

1. What is a While Loop?

A while loop in Python allows you to repeat a block of code as long as a


certain condition is true.
It’s like saying, “Keep doing this until something changes.”

For example: If you are told to keep jumping until your teacher says
stop that’s how a while loop works!

2. Syntax of While Loop

The basic structure of a while loop is:

while condition:
# code to repeat

- while : This keyword starts the loop.


- condition : The loop keeps running while this condition is True.
- code to repeat : The code inside the loop runs again and again.

3. Example 1: Printing numbers from 1 to 5

Let’s print numbers from 1 to 5 using a while loop.

Example:
i=1
while i <= 5:
print(i)
i=i+1

Explanation:
- We start with `i = 1`.
- The loop runs while `i <= 5` is true.
- Each time, we print the value of `i` and increase it by 1.
- When `i` becomes 6, the condition becomes false, and the loop stops.

4. Example 2: Printing Even Numbers

We can use the while loop to print even numbers between 1 and 10.

Example:

i=2
while i <= 10:
print(i)
i=i+2

Explanation:
- We start from 2 (the first even number).
- The loop runs as long as `i <= 10`.
- We add 2 every time to get the next even number.
5. Example 3: Asking user input

We can also use while loops to ask questions until we get the right
answer.

Example:

password = ""
while password != "python":
password = input("Enter the password: ")
print("Access Granted!")

Explanation:
- The loop continues until the user types "python".
- When the user types the correct password, the condition becomes
false, and the loop stops.

6. Infinite Loops

If the condition in a while loop **never becomes False**, the loop will
run forever.
This is called an **infinite loop**.

Example:

while True:
print("This will never stop!")
⚠️ Be careful! Infinite loops can make your computer program hang
or freeze.

7. Using Break in While Loop

The **break** statement is used to stop the loop even if the condition is
still true.

Example:

i=1
while i <= 10:
if i == 5:
break
print(i)
i=i+1

Explanation:
- The loop stops when `i` becomes 5, even though the condition allows it
to run until 10.

8. Using Continue in While Loop

The continue statement skips the rest of the code inside the loop for that
turn and moves to the next one.

Example:
i=0
while i < 5:
i=i+1
if i == 3:
continue
print(i)

Explanation:
- When `i` equals 3, the loop skips the print statement.
- So the output will be 1, 2, 4, 5.

9. Practice Questions

Try solving these questions on your own!

1. Print numbers from 10 to 1 using a while loop.


2. Print the table of 5 using a while loop.
3. Find the sum of numbers from 1 to 100 using a while loop.
4. Print all odd numbers from 1 to 20 using a while loop.
5. Keep asking the user to enter their name until they type your name.

You might also like