Do While Loop In Python
A "do-while" loop is not a built-in loop construct in Python, as Python does not have a do-while loop. However, you can implement similar behavior using a "while" loop with a "break" statement.
Here's an example of how you might use a "while" loop to simulate a "do-while" loop in Python:
pythonwhile True:
# code to be executed
if some_condition:
break
In this example, the code inside the loop will be executed at least once, because the loop condition is always True. The break
statement is used to exit the loop when some condition is met.
It's important to note that the break
statement should be placed inside the loop, otherwise the loop will not terminate and you'll end up with an infinite loop.
"डू-वाइल" लूप पायथन में बिल्ट-इन लूप निर्माण नहीं है, क्योंकि पायथन में डू-जबकि लूप नहीं है। हालाँकि, आप "ब्रेक" स्टेटमेंट के साथ "जबकि" लूप का उपयोग करके समान व्यवहार को लागू कर सकते हैं।
यहां एक उदाहरण दिया गया है कि आप पायथन में "डू-वाइल" लूप का अनुकरण करने के लिए "जबकि" लूप का उपयोग कैसे कर सकते हैं:
पायथन कॉपी कोड
जबकि सही: # कोड निष्पादित किया जाना है अगर some_condition: break
इस उदाहरण में, लूप के अंदर का कोड कम से कम एक बार निष्पादित किया जाएगा, क्योंकि लूप की स्थिति हमेशा True होती है। ब्रेक स्टेटमेंट का उपयोग कुछ शर्तों के पूरा होने पर लूप से बाहर निकलने के लिए किया जाता है।
यह ध्यान रखना महत्वपूर्ण है कि ब्रेक स्टेटमेंट को लूप के अंदर रखा जाना चाहिए, अन्यथा लूप समाप्त नहीं होगा और आप अनंत लूप के साथ समाप्त हो जाएंगे।
Comments
Post a Comment