जावा जबकि और करते हैं ... जबकि लूप

इस ट्यूटोरियल में, हम उदाहरणों की मदद से जावा में लूप करते समय उपयोग करना और करना सीखेंगे।

कंप्यूटर प्रोग्रामिंग में, कोड के एक ब्लॉक को दोहराने के लिए लूप का उपयोग किया जाता है। उदाहरण के लिए, यदि आप 100 बार संदेश दिखाना चाहते हैं, तो आप लूप का उपयोग कर सकते हैं। यह सिर्फ एक सरल उदाहरण है; आप छोरों के साथ बहुत अधिक प्राप्त कर सकते हैं।

पिछले ट्यूटोरियल में, आपने लूप के लिए जावा के बारे में सीखा। यहाँ, आप के बारे में whileऔर do… whileछोरों के बारे में जानने जा रहे हैं ।

लूप करते समय जावा

जावा whileलूप का उपयोग विशिष्ट कोड चलाने के लिए किया जाता है जब तक कि एक निश्चित शर्त पूरी नहीं हो जाती। whileलूप का सिंटैक्स है:

 while (testExpression) ( // body of loop )

यहाँ,

  1. एक whileलूप कोष्ठक के अंदर textExpression का मूल्यांकन करता है ()
  2. यदि textExpression का मूल्यांकन करता है true, तो whileलूप के अंदर कोड निष्पादित होता है।
  3. TextExpression फिर से मूल्यांकन किया जाता है।
  4. यह प्रक्रिया तब तक जारी रहती है जब तक textExpression है false
  5. जब TextExpression मूल्यांकन करता है false, तो लूप बंद हो जाता है।

शर्तों के बारे में अधिक जानने के लिए, जावा रिलेशनल और लॉजिकल ऑपरेटरों पर जाएं।

लूप के फ्लोचार्ट

लूप करते समय जावा का फ्लोचार्ट

उदाहरण 1: 1 से 5 तक संख्याएं प्रदर्शित करें

 // Program to display numbers from 1 to 5 class Main ( public static void main(String() args) ( // declare variables int i = 1, n = 5; // while loop from 1 to 5 while(i <= n) ( System.out.println(i); i++; ) ) )

आउटपुट

 1 2 3 4 5

यहां बताया गया है कि यह कार्यक्रम कैसे काम करता है।

Iteration चर दशा: i <= n क्रिया
पहली बार i = 1
n = 5
true 1 छपा है।
मुझे 2 तक बढ़ाया गया है ।
2 रा i = 2
n = 5
true 2 छपा है।
मुझे बढ़ाकर 3 कर दिया गया है
i = 3
n = 5
true 3 छपा है।
मुझे बढ़ाकर 4 कर दिया गया है
४ था i = 4
n = 5
true 4 छपा है।
मैं 5 तक बढ़ा दी गई हूं ।
5 वाँ i = 5
n = 5
true 5 छपा है।
मुझे बढ़ाकर 6 कर दिया गया है
6 ठ i = 6
n = 5
false लूप को समाप्त कर दिया जाता है

उदाहरण 2: केवल सकारात्मक संख्याओं का योग

 // Java program to find the sum of positive numbers import java.util.Scanner; class Main ( public static void main(String() args) ( int sum = 0; // create an object of Scanner class Scanner input = new Scanner(System.in); // take integer input from the user System.out.println("Enter a number"); int number = input.nextInt(); // while loop continues // until entered number is positive while (number>= 0) ( // add only positive numbers sum += number; System.out.println("Enter a number"); number = input.nextInt(); ) System.out.println("Sum = " + sum); input.close(); ) )

आउटपुट

 एक नंबर दर्ज करें 25 एक नंबर दर्ज करें एक नंबर दर्ज करें 5 एक नंबर दर्ज करें -3 ​​Sum = 39

उपरोक्त कार्यक्रम में, हमने उपयोगकर्ता से इनपुट लेने के लिए स्कैनर वर्ग का उपयोग किया है। यहां, nextInt()उपयोगकर्ता से पूर्णांक इनपुट लेता है।

whileपाश जारी है जब तक उपयोगकर्ता एक ऋणात्मक संख्या में प्रवेश करती है। प्रत्येक पुनरावृत्ति के दौरान, उपयोगकर्ता द्वारा दर्ज की गई संख्या को sumचर में जोड़ा जाता है ।

जब उपयोगकर्ता एक नकारात्मक संख्या में प्रवेश करता है, तो लूप समाप्त हो जाता है। अंत में, कुल योग प्रदर्शित होता है।

जावा करते हैं … जबकि लूप

do… whileपाश जबकि पाश के समान है। हालाँकि, do… whileपरीक्षण अभिव्यक्ति की जाँच करने से पहले लूप के शरीर को एक बार निष्पादित किया जाता है। उदाहरण के लिए,

 do ( // body of loop ) while(textExpression)

यहाँ,

  1. लूप के शरीर को पहले निष्पादित किया जाता है। तब textExpression का मूल्यांकन किया जाता है।
  2. यदि textExpression का मूल्यांकन करता है true, तो doकथन के अंदर लूप का शरीर फिर से निष्पादित होता है।
  3. TextExpression एक बार फिर से मूल्यांकन किया जाता है।
  4. यदि textExpression का मूल्यांकन करता है true, तो doकथन के अंदर लूप का शरीर फिर से निष्पादित होता है।
  5. यह प्रक्रिया तब तक जारी रहती है जब तक textExpression मूल्यांकन नहीं करता है false। फिर लूप बंद हो जाता है।

लूप करते समय फ्लोचार्ट करें …

जावा के फ़्लोचार्ट लूप करते समय करते हैं

Let's see the working of do… while loop.

Example 3: Display Numbers from 1 to 5

 // Java Program to display numbers from 1 to 5 import java.util.Scanner; // Program to find the sum of natural numbers from 1 to 100. class Main ( public static void main(String() args) ( int i = 1, n = 5; // do… while loop from 1 to 5 do ( System.out.println(i); i++; ) while(i <= n); ) )

Output

 1 2 3 4 5

Here is how this program works.

Iteration Variable Condition: i <= n Action
i = 1
n = 5
not checked 1 is printed.
i is increased to 2.
1st i = 2
n = 5
true 2 is printed.
i is increased to 3.
2nd i = 3
n = 5
true 3 is printed.
i is increased to 4.
3rd i = 4
n = 5
true 4 is printed.
i is increased to 5.
4th i = 5
n = 5
true 6 is printed.
i is increased to 6.
5th i = 6
n = 5
false The loop is terminated

Example 4: Sum of Positive Numbers

 // Java program to find the sum of positive numbers import java.util.Scanner; class Main ( public static void main(String() args) ( int sum = 0; int number = 0; // create an object of Scanner class Scanner input = new Scanner(System.in); // do… while loop continues // until entered number is positive do ( // add only positive numbers sum += number; System.out.println("Enter a number"); number = input.nextInt(); ) while(number>= 0); System.out.println("Sum = " + sum); input.close(); ) )

Output 1

 Enter a number 25 Enter a number 9 Enter a number 5 Enter a number -3 Sum = 39

Here, the user enters a positive number, that number is added to the sum variable. And this process continues until the number is negative. When the number is negative, the loop terminates and displays the sum without adding the negative number.

Output 2

 Enter a number -8 Sum is 0

Here, the user enters a negative number. The test condition will be false but the code inside of the loop executes once.

Infinite while loop

If the condition of a loop is always true, the loop runs for infinite times (until the memory is full). For example,

 // infinite while loop while(true)( // body of loop )

Here is an example of an infinite do… while loop.

 // infinite do… while loop int count = 1; do ( // body of loop ) while(count == 1)

In the above programs, the textExpression is always true. Hence, the loop body will run for infinite times.

for and while loops

forपाश जब पुनरावृत्तियों की संख्या में जाना जाता है प्रयोग किया जाता है। उदाहरण के लिए,

 for (let i = 1; i <=5; ++i) ( // body of loop )

और whileऔर do… whileछोरों आम तौर पर उपयोग किया जाता है जब पुनरावृत्तियों की संख्या अज्ञात है। उदाहरण के लिए,

 while (condition) ( // body of loop )

दिलचस्प लेख...