-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathwhileloop.py
More file actions
29 lines (26 loc) · 1.2 KB
/
whileloop.py
File metadata and controls
29 lines (26 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
a = 0
while a <= 10:
print("Value of a:", a) # This will print values from 0 to 10
a += 1
print("Loop ended.")
# This is a simple while loop that increments the value of 'a' from 0 to 10 and prints it. After the loop ends, it prints "Loop ended." You can modify the condition and the increment value to see different behaviors of the loop.
# For example, changing the condition to 'a < 5' will make the loop run until 'a' is 4.
# Similarly, changing 'a += 1' to 'a += 2' will increment 'a' by 2 in each iteration.
# Experiment with these changes to understand how while loops work in Python.
# Note: Ensure that the loop has a terminating condition to avoid infinite loops.
# Always be cautious with while loops to prevent them from running indefinitely.
# You can also add an else block to the while loop which will execute when the loop condition becomes false.
# Example:
# while a <= 10:
# print("Value of a:", a)
# a += 1
# else:
# print("Loop ended.")
# This will print "Loop ended." after the loop finishes.
# Happy coding!
# This code demonstrates the use of a while loop in Python.
a = 10
while a >= 0:
print("Value of a:", a) # This will print values from 10 to 0
a -= 1
print("Loop ended.")