While Loop in Python
While Loop in Python
a) While: - The
"for" loop is completely dependent on the range while the
"while" loop does not. With the while loop we can execute a
set of statements as long as a condition is true. The way of writing it will be
slightly different. There will be a start, condition, and there will be
increment and decrement. "for" लूप पूरी तरह रेंज पर डिपेंड होता है जबकि "while" लूप में ऐसा नहीं है While
लूप के साथ हम Statement के
एक सेट को तब तक execute कर
सकते हैं जब तक कोई शर्त true है इसके लिखने का तरीका थोड़ा अलग होगा
इसमें स्टार्ट होगा, कंडीशन
होगी और इन्क्रीमेंट और डेक्रेमेंट होंगे
Example
Print
i as long as i is less than 6:
i = 1
while i < 6:
print(i)
i += 1
Output: 1
2
3
4
5
Note: remember to increment i, or
else the loop will continue forever. i को बढ़ाना याद रखें, वरना
लूप हमेशा के लिए जारी रहेगा।
The while loop requires relevant variables to be
ready, in this example we need to define an indexing variable, i, which we
set to 1.
Print string with I as long as I is less than
6
Example
i = 1
while i < 6:
print(i, “Hello”)
i += 1
Output: 1
Hello
2 Hello
3 Hello
4 Hello
5 Hello
Print
i as long as i is less than 6 in reverse:
i = 6
while i > 1:
print(i, “Hello”)
i = i - 1
Output: 5
Hello
4 Hello
3 Hello
2 Hello
1 Hello
Post a Comment