The while-else loop in Python is a control flow structure that allows a block of code to be executed repeatedly based on a given Boolean condition. The else part is executed if the condition in the while loop evaluates to False. The while-else statement can be used with the break keyword to run the else block only when the while condition becomes False, and not when the while loop is terminated by a break statement.
Here’s the basic syntax:
Structure
while condition:
# code to execute while condition is True
else:
# code to execute when condition becomes False
In this structure, condition is an expression that Python evaluates as either True or False. If the condition is True, the code block under the while statement (indented code) is executed. This process repeats until the condition becomes False. At that point, the code block under the else statement is executed.
Here’s an example:
While-else simple program
count = 0
while count < 5:
print(count)
count += 1 # equivalent to 'count = count + 1'
else:
print("Count is no longer less than 5")
Output
0
1
2
3
4
Count is no longer less than 5
In this example, the condition is count < 5. Python checks whether count is less than 5. If it is, it prints the count and then increments count by 1. This process repeats until count is no longer less than 5. At that point, Python prints “Count is no longer less than 5”.
The else part after the while loop is a special feature in Python. The else part is executed if the while loop terminated due to a False condition. The else part is not executed if the while loop is terminated by a break statement.
Here are some examples of using while-else in Python:
Countdown Timer
Countdown Timer example using while-else in python
n = 10
while n > 0:
print(n)
n -= 1
else:
print("Countdown complete!")
Output
10
9
8
7
6
5
4
3
2
1
Countdown complete!
Checking for Prime Numbers
Checking for Prime Numbers uisng while-else example
num = 17
i = 2
while i * i <= num:
if num % i:
i += 1
else:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
Output
17 is a prime number
Finding the Square Root
Finding the Square Root using while-else example
num = 16
guess = 0.5 * num
while abs(guess * guess - num) > 1e-6:
guess = 0.5 * (guess + num / guess)
else:
print("The square root of", num, "is approximately", guess)
Output
The square root of 16 is approximately 4.000000000000004
Guessing Game
Guessing number Game using while-else in python
import random
number_to_guess = random.randint(1, 10)
guess = None
while guess != number_to_guess:
guess = int(input("Guess a number between 1 and 10: "))
else:
print("Congratulations! You guessed the number.")
Output
Guess a number between 1 and 10:
1
Guess a number between 1 and 10:
4
Guess a number between 1 and 10:
5
Guess a number between 1 and 10:
6
Guess a number between 1 and 10:
7
Congratulations! You guessed the number.
code executed successfully with code 0
Password Validation
Password Validation using while-else example in python
correct_password = "python123"
input_password = input("Enter your password: ")
while input_password != correct_password:
input_password = input("Wrong password. Try again: ")
else:
print("Access granted.")
Output
Enter your password:
pass
Wrong password. Try again:
python123
Access granted.
Finding Factorial
Finding Factorial using while-else in python
num = 5
factorial = 1
while num > 1:
factorial *= num
num -= 1
else:
print("Factorial is", factorial)
Output
Factorial is 120
Multiplication Table
Multiplication Table using while-else in python
num = 3
i = 1
while i <= 10:
print(num, "*", i, "=", num * i)
i += 1
else:
print("End of multiplication table for", num)
Output
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
End of multiplication table for 3
Sum of Natural Numbers
Sum of Natural Numbers using while-else example in python
num = 10
if num < 0:
print("Enter a positive number")
else:
sum = 0
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Output
The sum is 55
Checking for Palindrome
Palindrome or not using while-else example in python
word = "radar"
is_palindrome = True
i = 0
while i < len(word) // 2:
if word[i] != word[-i - 1]:
is_palindrome = False
break
i += 1
else:
print(word, "is a palindrome")
Output
radar is a palindrome
Fibonacci Series
Fibonacci Series using while-else in python
n = 10
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
else:
print()
Output
0 1 1 2 3 5 8
These examples should give you a good understanding of how to use while-else in Python