Important Notice:

The while Statement

The while Statement

18 views 2 min read

The while Statement :-

while statement Python का एक Iterative Control Statement (दोहराव नियंत्रण कथन) है। इसका उपयोग किसी statement या statements के समूह को बार-बार execute करने के लिए किया जाता है, जब तक दी गई condition True (सही) रहती है।

यदि condition True होती है, तो while के अंदर लिखा code execute होता है और condition को फिर से check किया जाता है। जब condition False (गलत) हो जाती है, तो while loop समाप्त हो जाता है।

Python में while statement के अंदर लिखे code को Indentation (रिक्त स्थान) देना आवश्यक होता है। Indentation यह बताता है कि कौन-सा statement while block के अंदर है। सामान्यतः Python में 4 spaces का indentation प्रयोग किया जाता है।

English

The while statement is an iterative control statement in Python. It is used to execute a statement or group of statements repeatedly as long as the given condition remains True.

If the condition is True, the statements inside the while block are executed, and the condition is checked again. When the condition becomes False, the while loop terminates.

In Python, the code written inside a while statement must be given Indentation (spaces). Indentation indicates which statements are part of the while block. Generally, 4 spaces are used for indentation.

Syntax:-

while condition:
    statement

Example:

i = 1

while i <= 5:
    print(i)
    i += 1

Output:

1
2
3
4
5

Because:

i = 1
1 <= 5 → True
2 <= 5 → True
3 <= 5 → True
4 <= 5 → True
5 <= 5 → True
6 <= 5 → False

इसलिए print(i) 5 times execute होता है और जब i = 6 होता है, तो condition False हो जाती है तथा loop समाप्त हो जाता है।

English

The print(i) statement is executed 5 times. When i becomes 6, the condition 6 <= 5 becomes False, so the loop terminates.

Important Point :-

while loop में loop control variable को update करना बहुत important है। यदि condition कभी False नहीं होती, तो loop लगातार चलता रहेगा, जिसे Infinite Loop कहा जाता है।

English

In a while loop, it is important to update the loop control variable. If the condition never becomes False, the loop continues indefinitely. This is called an Infinite Loop.

Example:

i = 1

while i <= 5:
    print(i)
    i += 1  # agar ye line na likhe to infinite loop chelega

यहाँ i += 1 प्रत्येक iteration में i की value को बढ़ाता है।

Here, i += 1 increases the value of i in each iteration.

Related Notes