Do While Loop Ask for Answer Again if Not Correct C

Python While Loop is used to execute a block of statements repeatedly until a given status is satisfied. And when the status becomes false, the line immediately after the loop in the program is executed. While loop falls under the category of indefinite iteration. Indefinite iteration means that the number of times the loop is executed isn't specified explicitly in advance.

Syntax:

while expression:     statement(south)

Statements represent all the statements indented by the aforementioned number of character spaces after a programming construct are considered to be part of a single block of code. Python uses indentation as its method of grouping statements. When a while loop is executed, expr is first evaluated in a Boolean context and if it is truthful, the loop body is executed. So the expr is checked again, if it is yet true then the body is executed again and this continues until the expression becomes fake.

Flowchart of While Loop :

python while loop

Instance one: Python While Loop

Python3

count = 0

while (count < iii ):

count = count + 1

print ( "Hello Geek" )

Output

Hi Geek Hello Geek Hello Geek

In the above instance, the condition for while will exist True as long equally the counter variable (count) is less than three.

Case ii: Python while loop with listing

Python3

a = [ one , 2 , 3 , 4 ]

while a:

print (a.pop())

In the above example, we have run a while loop over a list that will run until there is an chemical element nowadays in the list.

Unmarried argument while cake

Just like the if cake, if the while cake consists of a unmarried statement we tin can declare the entire loop in a unmarried line. If there are multiple statements in the block that makes up the loop trunk, they can be separated by semicolons (;).

Python3

count = 0

while (count < five ): count + = 1 ; print ( "Howdy Geek" )

Output:

How-do-you-do Geek Hello Geek Hello Geek Hello Geek Hello Geek

Loop Control Statements

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.

Proceed Statement

Python Keep Statement returns the command to the kickoff of the loop.

Case: Python while loop with go on statement

Python3

i = 0

a = 'geeksforgeeks'

while i < len (a):

if a[i] = = 'e' or a[i] = = 'due south' :

i + = 1

go along

print ( 'Current Letter :' , a[i])

i + = 1

Output:

Current Letter : k Current Letter : k Current Letter of the alphabet : f Current Alphabetic character : o Current Letter : r Current Letter : g Electric current Letter of the alphabet : k

Break Argument

Python Pause Statement brings control out of the loop.

Example: Python while loop with break statement

Python3

i = 0

a = 'geeksforgeeks'

while i < len (a):

if a[i] = = 'e' or a[i] = = 'due south' :

i + = ane

interruption

print ( 'Current Letter of the alphabet :' , a[i])

i + = ane

Output:

Current Letter of the alphabet : one thousand

Pass Argument

The Python pass statement to write empty loops. Pass is also used for empty control statements, functions, and classes.

Instance: Python while loop with pass statement

Python3

a = 'geeksforgeeks'

i = 0

while i < len (a):

i + = 1

pass

print ( 'Value of i :' , i)

Output:

Value of i : 13

While loop with else

As discussed above, while loop executes the block until a condition is satisfied. When the condition becomes faux, the argument immediately afterward the loop is executed. The else clause is only executed when your while condition becomes false. If you lot suspension out of the loop, or if an exception is raised, it won't be executed.

Notation: The else cake only after for/while is executed simply when the loop is Non terminated by a interruption statement.

Python3

i = 0

while i < 4 :

i + = 1

print (i)

else :

impress ( "No Break\n" )

i = 0

while i < 4 :

i + = 1

print (i)

break

else :

print ( "No Pause" )

Output:

1 two 3 4 No Pause  1

Sentinel 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 utilise a sentinel value. A sentinel value is a value that is used to stop a loop whenever a user enters it, more often than not, the sentinel value is -one.

Example: Python while loop with user input

Python3

a = int ( input ( 'Enter a number (-1 to quit): ' ))

while a ! = - 1 :

a = int ( input ( 'Enter a number (-1 to quit): ' ))

Output:

Explanation:

  • Get-go, information technology asks the user to input a number. if the user enters -one then the loop will not execute
  • User enter half-dozen and the body of the loop executes and again ask for input
  • Here user tin input many times until he enters -1 to stop the loop
  • User can determine how many times he wants to enter input

callihanstor1972.blogspot.com

Source: https://www.geeksforgeeks.org/python-while-loop/

0 Response to "Do While Loop Ask for Answer Again if Not Correct C"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel