sponsor

sponsor

Slider

Recent Tube

Business

Technology

Life & style

Games

Sports

Fashion

» » while Loop

Loop एक ही statement को बार-बार execute करता है |
Looping में indentation का काफी महत्व होता है |
while Loop का इस्तेमाल जब तक condition true होती है तब तक statement execute होता रहता है और जब condition false हो जाती है तब loop का iteration; stop हो जाता है |

Syntax for while Loop in Python

while (expression):
 while_statement(s)

Example for while Loop

Source Code :
a = 0

while (a < 10) :
    print("Value a is ", a)
    a = a + 1
Output :
Value a is  0
Value a is  1
Value a is  2
Value a is  3
Value a is  4
Value a is  5
Value a is  6
Value a is  7
Value a is  8
Value a is  9


while Loop with else Statement in Python

while Loop के else का सम्बन्ध बनाया गया है | जब तक condition true होती है तब तक while loop का statement iterate होता रहता है और condition false होती है तब control pass होके else पर जाकर else का statement execute होता है |
Source Code :
a = 0

while(a < 5):
    print("Value of a is ", a);
    a = a + 1
else:
    print("Out of Loop");
Output :
Value of a is  0
Value of a is  1
Value of a is  2
Value of a is  3
Value of a is  4
Out of Loop


infinite Loop in Python

जब तक while का expression 'True' होता है तब तक वो अपना statement execute करता रहता है | Example पर condition false ही नहीं हो रही है इसीलिए infinite times; statement को execute किया जा रहा है |
Source Code :
while(True) :  #expression is always true
   print("Hello")
Output :
Hello
Hello

«
Next
Newer Post
»
Previous
Older Post

No comments:

Leave a Reply