Python Run Each Loop for Certain Time Before Trying Again
Python While Loop is used to execute a block of statements repeatedly until a given condition is satisfied. And when the status becomes false, the line immediately afterwards the loop in the program is executed. While loop falls nether the category of indefinite iteration. Indefinite iteration ways that the number of times the loop is executed isn't specified explicitly in advance.
Syntax:
while expression: statement(southward)
Statements represent all the statements indented by the same number of character spaces after a programming construct are considered to exist office of a single block of code. Python uses indentation every bit its method of grouping statements. When a while loop is executed, expr is offset evaluated in a Boolean context and if it is truthful, the loop body is executed. Then the expr is checked over again, if it is still truthful then the trunk is executed again and this continues until the expression becomes false.
Flowchart of While Loop :
Example one: Python While Loop
Python3
count = 0
while (count < 3 ):
count = count + 1
print ( "Hello Geek" )
Output
Howdy Geek How-do-you-do Geek How-do-you-do Geek
In the above example, the condition for while will be Truthful every bit long as the counter variable (count) is less than 3.
Instance two: Python while loop with list
Python3
a = [ ane , 2 , 3 , iv ]
while a:
print (a.pop())
In the in a higher place instance, nosotros have run a while loop over a list that will run until there is an element nowadays in the list.
Single statement while cake
But like the if block, if the while cake consists of a single argument we tin declare the entire loop in a single line. If in that location are multiple statements in the block that makes upward the loop body, they can exist separated by semicolons (;).
Python3
count = 0
while (count < v ): count + = 1 ; print ( "Hello Geek" )
Output:
Hello Geek Hello Geek Hello Geek Hullo Geek Hello Geek
Loop Command Statements
Loop control statements alter execution from its normal sequence. When execution leaves a scope, all automated objects that were created in that scope are destroyed. Python supports the post-obit command statements.
Continue Argument
Python Continue Statement returns the control to the first of the loop.
Example: Python while loop with continue argument
Python3
i = 0
a = 'geeksforgeeks'
while i < len (a):
if a[i] = = 'eastward' or a[i] = = 's' :
i + = 1
go along
impress ( 'Current Letter of the alphabet :' , a[i])
i + = 1
Output:
Electric current Letter : thousand Electric current Letter : thousand Current Letter : f Current Alphabetic character : o Electric current Alphabetic character : r Current Letter : k Current Letter of the alphabet : 1000
Break Statement
Python Interruption Argument brings control out of the loop.
Instance: Python while loop with pause argument
Python3
i = 0
a = 'geeksforgeeks'
while i < len (a):
if a[i] = = 'east' or a[i] = = 'due south' :
i + = one
break
impress ( 'Electric current Letter :' , a[i])
i + = 1
Output:
Current Letter : g
Pass Statement
The Python laissez passer argument to write empty loops. Pass is as well used for empty control statements, functions, and classes.
Example: Python while loop with pass statement
Python3
a = 'geeksforgeeks'
i = 0
while i < len (a):
i + = 1
laissez passer
print ( 'Value of i :' , i)
Output:
Value of i : thirteen
While loop with else
Equally discussed above, while loop executes the block until a condition is satisfied. When the condition becomes false, the statement immediately after the loop is executed. The else clause is only executed when your while condition becomes fake. If you pause out of the loop, or if an exception is raised, it won't be executed.
Note: The else block only after for/while is executed merely when the loop is Not terminated by a intermission statement.
Python3
i = 0
while i < 4 :
i + = one
impress (i)
else :
impress ( "No Pause\n" )
i = 0
while i < 4 :
i + = 1
print (i)
break
else :
print ( "No Break" )
Output:
i two 3 4 No Break i
Watch Controlled Statement
In this, we don't use any counter variable because we don't know that how many times the loop will execute. Here user decides that how many times he wants to execute the loop. For this, we use a sentinel value. A sentinel value is a value that is used to terminate a loop whenever a user enters information technology, generally, the sentinel value is -1.
Example: Python while loop with user input
Python3
a = int ( input ( 'Enter a number (-ane to quit): ' ))
while a ! = - 1 :
a = int ( input ( 'Enter a number (-i to quit): ' ))
Output:
Caption:
- Beginning, it asks the user to input a number. if the user enters -1 then the loop will not execute
- User enter 6 and the body of the loop executes and again ask for input
- Here user can input many times until he enters -1 to terminate the loop
- User tin decide how many times he wants to enter input
Source: https://www.geeksforgeeks.org/python-while-loop/
0 Response to "Python Run Each Loop for Certain Time Before Trying Again"
Post a Comment