Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python syntax for an empty while loop

I have written this:

    while file.readline().startswith("#"):         continue 

But I suspect the continue is unnecessary? What is the correct syntax for what i'm trying to achieve?

like image 808
jsj Avatar asked Feb 10 '13 11:02

jsj


People also ask

How do you write an empty while loop in Python?

In Python, to write empty functions, we use pass statement. pass is a special statement in Python that does nothing. It only works as a dummy statement. We can use pass in empty while statement also.

What is the syntax of while loop in Python?

Syntax of while Loop in Python In the while loop, test expression is checked first. The body of the loop is entered only if the test_expression evaluates to True . After one iteration, the test expression is checked again. This process continues until the test_expression evaluates to False .

Can you have an empty while loop?

Empty while loopsAn empty while loop is a bad idea. This isn't the best approach but it is an acceptable one (in the right scenario). This ensures that you only perform the check at a reasonable interval. Note that the precise length of that interval is up to you to decide.

What is the use of empty loop in Python?

It can be used when a statement is required syntactically but the program requires no action.


1 Answers

while file.readline().startswith("#"):     pass 

This uses the pass statement :

The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action.

http://www.network-theory.co.uk/docs/pytut/passStatements.html

like image 81
ogzd Avatar answered Oct 06 '22 06:10

ogzd