Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python - Itertools.takewhile()
Next article icon
Try it on GfG Practice
redirect icon

Python While Loopis used to execute a block of statements repeatedly until a given condition is satisfied. When the condition becomes false, the line immediately after the loop in the program is executed.

In this example, the condition for while will be True as long as the counter variable (count) is less than 3. 

Python
# Python example for while loopcount=0while(count<3):count=count+1print("Hello Geek")

Output
Hello GeekHello GeekHello Geek

Let's take a look at Python While Loop in detail:

while loop Syntax

while expression:
statement(s)

  • condition: This is a boolean expression. If it evaluates to True, the code inside the loop will execute.
  • statement(s): These are the statements that will be executed during each iteration of the loop.

While Loop Flowchart

Python While Loop
While Loop

The while loop will continue running the code block as long as the condition evaluates to True. Each time the loop executes, the condition is checked again. If it is True, the loop continues; if it is False, the loop terminates, and the program moves to the next statement after the loop.

Infinite while Loop in Python

Here, the value of the condition is always True. Therefore, the body of the loop is run infinite times until the memory is full.

Python
age=28# the test condition is always Truewhileage>19:print('Infinite Loop')

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

while loop with continue statement

PythonContinue Statement returns the control to the beginning of the loop.

Python
# Prints all letters except 'e' and 's'i=0a='geeksforgeeks'whilei<len(a):ifa[i]=='e'ora[i]=='s':i+=1continueprint(a[i])i+=1

Output
gkforgk

while loop with break statement

PythonBreak Statement brings control out of the loop.

Python
# break the loop as soon it sees 'e'# or 's'i=0a='geeksforgeeks'whilei<len(a):ifa[i]=='e'ora[i]=='s':i+=1breakprint(a[i])i+=1

Output
g

while loop with pass statement

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

Python
# An empty loopa='geeksforgeeks'i=0whilei<len(a):i+=1passprint('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 false, the statement immediately after the loop is executed. The else clause is only executed when your while condition becomes false. If you break out of the loop, or if an exception is raised, it won’t be executed.

Note: The else block just after for/while is executed only when the loop is NOT terminated by a break statement. 

Python
# Python program to demonstrate# while-else loopi=0whilei<4:i+=1print(i)else:# Executed because no break in forprint("No Break\n")i=0whilei<4:i+=1print(i)breakelse:# Not executed as there is a breakprint("No Break")

Output
1234No Break1

While Loops in Python
Visit Courseexplore course icon

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp