C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python While loopThe Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop. It can be viewed as a repeating if statement. When we don't know the number of iterations then the while loop is most effective to use. The syntax is given below. while expression: statements Here, the statements can be a single statement or a group of statements. The expression should be any valid Python expression resulting in true or false. The true is any non-zero value and false is 0. While loop FlowchartLoop Control StatementsWe can change the normal sequence of while loop's execution using the loop control statement. When the while loop's execution is completed, all automatic objects defined in that scope are demolished. Python offers the following control statement to use within the while loop. 1. Continue Statement - When the continue statement is encountered, the control transfer to the beginning of the loop. Let's understand the following example. Example: # prints all letters except 'a' and 't' i = 0 str1 = 'TheDeveloperBlog' while i < len(str1): if str1[i] == 'a' or str1[i] == 't': i += 1 continue print('Current Letter :', a[i]) i += 1 Output: Current Letter : j Current Letter : v Current Letter : p Current Letter : o Current Letter : i Current Letter : n 2. Break Statement - When the break statement is encountered, it brings control out of the loop. Example: # The control transfer is transfered # when break statement soon it sees t i = 0 str1 = 'TheDeveloperBlog' while i < len(str1): if str1[i] == 't': i += 1 break print('Current Letter :', str1[i]) i += 1 Output: Current Letter : j Current Letter : a Current Letter : v Current Letter : a 3. Pass Statement - The pass statement is used to declare the empty loop. It is also used to define empty class, function, and control statement. Let's understand the following example. Example - # An empty loop str1 = 'TheDeveloperBlog' i = 0 while i < len(str1): i += 1 pass print('Value of i :', i) Output: Value of i : 10 Example-1: Program to print 1 to 10 using while loopi=1 #The while loop will iterate until condition becomes false. While(i<=10): print(i) i=i+1 Output: 1 2 3 4 5 6 7 8 9 10 Example -2: Program to print table of given numbers.i=1 number=0 b=9 number = int(input("Enter the number:")) while i<=10: print("%d X %d = %d \n"%(number,i,number*i)) i = i+1 Output: Enter the number:10 10 X 1 = 10 10 X 2 = 20 10 X 3 = 30 10 X 4 = 40 10 X 5 = 50 10 X 6 = 60 10 X 7 = 70 10 X 8 = 80 10 X 9 = 90 10 X 10 = 100 Infinite while loopIf the condition is given in the while loop never becomes false, then the while loop will never terminate, and it turns into the infinite while loop. Any non-zero value in the while loop indicates an always-true condition, whereas zero indicates the always-false condition. This type of approach is useful if we want our program to run continuously in the loop without any disturbance. Example 1while (1): print("Hi! we are inside the infinite while loop") Output: Hi! we are inside the infinite while loop Hi! we are inside the infinite while loop Example 2var = 1 while(var != 2): i = int(input("Enter the number:")) print("Entered value is %d"%(i)) Output: Enter the number:10 Entered value is 10 Enter the number:10 Entered value is 10 Enter the number:10 Entered value is 10 Infinite time Using else with while loopPython allows us to use the else statement with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using break statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional to use with the while loop. Consider the following example. Example 1i=1 while(i<=5): print(i) i=i+1 else: print("The while loop exhausted") Example 2i=1 while(i<=5): print(i) i=i+1 if(i==3): break else: print("The while loop exhausted") Output: 1 2 In the above code, when the break statement encountered, then while loop stopped its execution and skipped the else statement. Example-3 Program to print Fibonacci numbers to given limitterms = int(input("Enter the terms ")) # first two intial terms a = 0 b = 1 count = 0 # check if the number of terms is Zero or negative if (terms <= 0): print("Please enter a valid integer") elif (terms == 1): print("Fibonacci sequence upto",limit,":") print(a) else: print("Fibonacci sequence:") while (count < terms) : print(a, end = ' ') c = a + b # updateing values a = b b = c count += 1 Output: Enter the terms 10 Fibonacci sequence: 0 1 1 2 3 5 8 13 21 34
Next TopicPython break statement
|