Friday, 4 December 2015

Python: while statement

‘while’ statement executes a block of statements until a particular condition is true

Syntax
while_stmt ::=  "while" expression ":" suite
                           ["else" ":" suite]

‘else’ clause is optional. If the expression evaluates to false, then else clause will execute (if else present), and terminates.


test.py
a=2

print("Even numbers are")
while(a < 10) :
 print (a, end=' ')
 a +=2
else:
 print("\nExit from loop")

print("Done")

$ python3 test.py
Even numbers are
2 4 6 8
Exit from loop
Done




Previous                                                 Next                                                 Home

No comments:

Post a Comment