Python | Loop | While Loop By. Shivansh Sir

PYTHON PROGRAMMING

PYTHON | LOOP | WHILE LOOP

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

Syntax

while (expression):

while_statement(s)

Example

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

PYTHON | LOOP | WHILE LOOP | WITH ELSE STATEMENT

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

PYTHON | LOOP | WHILE LOOP | INFINITE LOOP

जब तक while का expression 'True' होता है तब तक वो अपना statement execute करता रहता है | Example पर condition false ही नहीं हो रही है इसीलिए infinite times; statement को execute किया जा रहा है |

Source Code

while(True) :  #expression is always true

   print("WELCOME TO SUNRISE")

Output

WELCOME TO SUNRISE

WELCOME TO SUNRISE

Note : "WELCOME TO SUNRISE" String print infinite times because while expression is always true

Comments

Popular posts from this blog

Python | All in One Program By. Shivansh Sir

Python | Data Types | Number & it's Conversion By. Shivansh Sir

Python | Loop | For Loop By. Shivansh Sir