तो आज हम लोग जानने वाले है Javascript While Loop के बारे में। तो अगर मैं आपसे बोलू की आप 1 से 100 तक की गिनती print कराओ तो कैसे कराओगे। अब अगर आप ये सोच रहे हो की आप document.write() function का use करके print करा सकते हो तो ये तरीका बिलकुल ही गलत है और ऐसा करने में आपका काफी time जायेगा। तो इस problem के लिए javascript में loop का use किया जाता है। Loop कई प्रकार के होते है उनके बारे में हम लोग एक एक करे देखेंगे आज हम लोग while loop के बारे में जानेंगे।
While Loop
ये loop बहुत ही basic होता है। इस loop में आप एक condition देते हो तो जब तक condition true होगी तब तक loop execute होगा जैसे ही condition false हुई loop terminate हो जायेगा। Loop 3 चीज़ो से मिलकर बना होता है
loop initialization :ये loop starting point होता है आप किस value से अपना loop start कर रहे है ये आपको बताना होता है।
condition: loop start होने बाद आप condition check करते है अगर codition true होती है तो loop के अंदर का code run होगा वरना loop से बाहर चला जायेगा।
increment: last जो चीज़ है वो है counter को increment या decrement करना।
Syntax
while (expression) {
Statement(s) to be executed if expression is true
}
ऊपर वाले program को ध्यान से देखिये इसमें मैंने एक let variable लिया है जिसकी value 0 है। अगर आपको नहीं पता की let क्या होता तो इसके बारे में मैंने पिछली पोस्ट में बताया है तो पहले आप वो पढ़ लीजिये जो आपको अच्छे से समझ में आ जयेगा इसके बारे में। तो चलिए program पे आते है तो उसके बाद while में condition लिखी हुई है की num छोटा है 5 से तो उसके बाद while के अंदर tech code now print कराया गया है उसके बाद num variable को increment किया गया है ठीक है इतना है प्रोग्राम में तो जब तक num की value 5 से छोटी रहेगी तब तक loop चलता रहेगा और जैसे ही num की value 5 या 5 से ज्यादा हुई वैसे ही loop terminate हो जयेगा। अपने ध्यान दिया होगा तो num की value को increment किया गया है।
अगर ऐसा न करे तो loop infinte तक चलता जायेगा क्युकी num की value हमेशा 5 से छोटी यानि की 0 ही रहेगी और condition हमेशा true होगी। चलिए एक और program बनाते है।
एक ऐसा प्रोग्राम बनाओ जिसे आप किसी भी number का table print कर सकते है।
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
let table = 2;
let num = 1;
while(num <= 10)
{
document.write(table+"x"+num+" = "+table*num+"<br>");
num++;
}
</script>
</head>
<body>
</body>
</html>
तो इस program में ज्यादा कुछ किआ नहीं गया है इसमें 2 variable लेके उन्हें आपस में multiply किया गया है और उसी से ही 2 का table print हो रहा है। अगर आपको किसी और number का table चाहिए तो आप table वाले variable में उस value को रख दीजिये तो आपको उस value का table print होकर मिल जायेगा। तो इस तरह हम while loop का use कर सकते है।
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy
Privacy Overview
This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.