लूपिंग स्टेटमेंट्स (Looping Statements)
स्टेटमेंट्स के किसी ब्लाॅक को बार बार एग्ज़िक्यूट करने की प्रक्रिया को लूपिंग कहते हैं। एक स्टेटमेंट ब्लाॅक को लूप की सहायता से कई बार एग्ज़िक्यूट कराया जा सकता है। लूपिंग में स्टेटमेंट्स का ब्लाॅक तब तक एग्ज़िक्यूट होता है जब तक लूप में दी गई कण्डीषन true रहती है। जावा में लूप के मुख्यतः दो भाग होते हैं -
- लूप की बाॅडी (body) या ब्लाॅक (block)
- कन्ट्रोल स्टेटमेंट (Control Statement)
- entry controlled loop
- exit controlled loop
जावा में लूपिंग के लिए तीन कन्ट्रोल स्टेटमेंट होते हैं -
- while स्टेटमेंट
- do स्टेटमेंट
- for स्टेटमेंट
while स्टेटमेंट
while लूप entry controlled loop की श्रेणी में आता है क्योंकि इसमें लूप में प्रवेश करने से पहले कण्डीषन की जाँच (check) होती है। इस लूप में जब तक कण्डीषन true होती है तब तक इसकी बाॅडी में दी हुई स्टेटमेंट्स एग्ज़िक्यूट होती हैं। यह प्रक्रिया तब तक चलती है जब तक कि कण्डीषन false न हो जाए। while स्टेटमेंट का निम्न प्रारुप होता है -
initialization section; while(test condition) { Statement 1; Statement 2; . . . . . . . . . . . . Statement n; }
निम्न उदाहरण में एक संदेश को 5 बार प्रिंट कराया गया है।
public class Demo
{
public static void main(String arr[])
{
int x = 1;
while(x <= 5)
{
System.out.println("Hello JAVA");
x++;
}
}
}
Output:
Hello JAVA Hello JAVA Hello JAVA Hello JAVA Hello JAVA
निम्न उदाहरण में 1 से 10 तक संख्याएं प्रिंट की गई हैं।
public class Demo
{
public static void main(String arr[])
{
int a = 1;
while(a <= 10)
{
System.out.print(a + " ");
a++;
}
}
}
Output:
1 2 3 4 5 6 7 8 9 10
do स्टेटमेंट
do लूप exit controlled loop की श्रेणी में आता है क्योंकि इसमें लूप से बाहर निकलते समय कण्डीषन की जाँच (check) होती है। while लूप की भांति यह लूप भी तब तक एग्ज़िक्यूट होता है जब तक कण्डीषन true होती है। do स्टेटमेंट का निम्न प्रारुप होता है -
initialization section; do { Statement 1; Statement 2; . . . . . . . . . . . . Statement n; } while (test condition);
do .. while लूप की बाॅडी एक बार अवश्य एग्ज़िक्यूट होती है चाहे कण्डीषन true हो या false, जबकि while लूप में यदि कण्डीषन false है तो लूप की बाॅडी एक बार भी एग्ज़िक्यूट नहीं होगी।
निम्न उदाहरण में एक संदेश को 5 बार प्रिंट कराया गया है।
public class Demo
{
public static void main(String arr[])
{
int x = 1;
do
{
System.out.println("Hello JAVA");
x++;
}while(x <= 5);
}
}
Output:
Hello JAVA Hello JAVA Hello JAVA Hello JAVA Hello JAVA
निम्न उदाहरण में 1 से 10 तक संख्याएं प्रिंट की गई हैं।
public class Demo
{
public static void main(String arr[])
{
int a = 1;
do
{
System.out.print(a + " ");
a++;
}while(a <= 10);
}
}
Output:
1 2 3 4 5 6 7 8 9 10
for स्टेटमेंट
यह भी एक entry controlled लूप होता है। इसका प्रयोग सामान्यतः तब किया जाता है जब लूप को निश्चित बार रन करना हो। इसका निम्न प्रारुप होता है -
for(intialization; test condition; increment/decrement) { Statement 1; Statement 2; . . . . . . . . . . . . Statement n; }
निम्न उदाहरण में एक संदेश को 5 बार प्रिंट कराया गया है।
public class Demo
{
public static void main(String arr[])
{
int x;
for(x=1; x<=5; x++)
{
System.out.println("Hello JAVA");
}
}
}
Output:
Hello JAVA Hello JAVA Hello JAVA Hello JAVA Hello JAVA
निम्न उदाहरण में 1 से 10 तक संख्याएं प्रिंट की गई हैं।
public class Demo
{
public static void main(String arr[])
{
int a;
for(a=1; a<=10; a++)
{
System.out.print(a + " ");
}
}
}
Output:
1 2 3 4 5 6 7 8 9 10
निम्न उदाहरण initialization का प्रयोग वित में किए बिना 1 से 5 तक संख्याएं प्रिंट की गई हैं।
public class Demo
{
public static void main(String arr[])
{
int a = 1;
for(; a<=5; a++)
{
System.out.print(a + " ");
}
}
}
Output:
1 2 3 4 5
निम्न उदाहरण initialization तथा increment/decrement का प्रयोग for में किए बिना 1 से 5 तक संख्याएं प्रिंट की गई हैं।
public class Demo
{
public static void main(String arr[])
{
int a = 1;
for(; a<=5; )
{
System.out.print(a + " ");
a++;
}
}
}
Output:
1 2 3 4 5
निम्न उदाहरण में एक से अधिक वेरिएबलों को initialize तथा increase/decrease करते हुए 1 से 5 एवं 5 से 1 तक संख्याएं साथ साथ प्रिंट की गई हैं।
public class Demo
{
public static void main(String arr[])
{
int a,b;
for(a=1, b=5; a<=5; a++, b--)
{
System.out.print("a=" + a);
System.out.println("\tb=" + b);
}
}
}
Output:
a=1 b=5 a=2 b=4 a=3 b=3 a=4 b=2 a=5 b=1
लूप की नेस्टिंग (Nesting of Loops)
जावा में एक लूप के अन्दर दूसरा लूप हो सकता है इसे लूप की नेस्टिंग कहते हैं, इसे नीचे दर्षाया गया है -
for(initialization ; test condition ; increment/decrement) { . . . . . . . . . . . . . . for(initialization ; test condition ; increment/decrement) { . . . . . . . . . . . . . . } . . . . . . . . . . . . . . }
नीचे दिए उदाहरण में पिरामिड आउटपुट दर्षाया गया है।
public class Demo
{
public static void main(String arr[])
{
int a,b;
for(a=1; a<=5; a++)
{
for(b=1; b<=a; b++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
* * * * * * * * * * * * * * *
अन्य उदाहरण
उदाहरणः पाँच संख्याओं का जोड़ ज्ञात करना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, c = 1, sum = 0;
Scanner ob = new Scanner(System.in);
while(c <= 5)
{
System.out.print("Enter no. " + c + ": ");
a = ob.nextInt();
sum += a;
c++;
}
System.out.println("Total of 5 nos.: " + sum);
}
}
Output:
Enter no. 1: 3 Enter no. 2: 2 Enter no. 3: 5 Enter no. 4: 1 Enter no. 5: 2 Total of 5 nos.: 13
उदाहरणः पाँच संख्याओं में से बड़ा नम्बर प्रिन्ट करना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, c = 1, max = 0;
Scanner ob = new Scanner(System.in);
while(c <= 5)
{
System.out.print("Enter no. " + c + ": ");
a = ob.nextInt();
if(c == 1)
max = a;
if(max < a)
max = a;
c++;
}
System.out.println("Largest: " + max);
}
}
Output:
Enter no. 1: 3 Enter no. 2: 2 Enter no. 3: 5 Enter no. 4: 1 Enter no. 5: 2 Largest: 5
उदाहरणः पाँच संख्याओं में से कितने शून्य से बड़े, छोटे या शून्य हैं।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, c = 1, n = 0, p = 0, z = 0;
Scanner ob = new Scanner(System.in);
while(c <= 5)
{
System.out.print("Enter no. " + c + ": ");
a = ob.nextInt();
if(a < 0)
n++;
else if(a > 0)
p++;
else
z++;
c++;
}
System.out.println("No. of -ve: " + n);
System.out.println("No. of +ve: " + p);
System.out.println("No. of Zero: " + z);
}
}
Output:
Enter no. 1: 3 Enter no. 2: -2 Enter no. 3: 5 Enter no. 4: 1 Enter no. 5: 0 No. of –ve: 1 No. of +ve: 3 No. of Zero: 1
उदाहरणः गई संख्या के अंकों को विपरित क्रम में प्रिंट कराना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, c = 1;
Scanner ob = new Scanner(System.in);
System.out.print("Enter a no.: ");
a = ob.nextInt();
while((a/10) > 0)
{
System.out.print(a % 10);
a = a / 10;
c++;
}
System.out.print(a);
}
}
Output:
Enter a no.: 342 243
उदाहरणः प्राइम (अभाज्य) नम्बर ज्ञात करना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, b, c, p = 0;
Scanner ob = new Scanner(System.in);
System.out.print("Enter a number: ");
a = ob.nextInt();
for(c=2; c < a; c++)
{
b = a % c;
if(b == 0)
{
p = 1;
break;
}
}
if(p == 1)
System.out.println("No. is NOT PRIME");
else
System.out.println("No. is PRIME");
}
}
Output:
Enter a number: 11 No. is PRIME
उदाहरणः दी गई संख्या का फेक्टोरियल ज्ञात करना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, f = 1, c;
Scanner ob = new Scanner(System.in);
System.out.print("Enter a number: ");
a = ob.nextInt();
for(c=1; c<= a; c++)
{
f = f * c;
}
System.out.println("Result: " + f);
}
}
Output:
Enter a number: 4 Result: 24
उदाहरणः दी गई संख्या की टेबल प्रिन्ट करना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a, c;
Scanner ob = new Scanner(System.in);
System.out.print("Enter a no.: ");
a = ob.nextInt();
for(c=1; c<=10; c++)
{
System.out.println(a + " x " + c + " = " + (a*c));
}
}
}
Output:
Enter a no.: 3 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30
उदाहरणः पिरामिड 1
public class Demo
{
public static void main(String arr[])
{
int c, r;
for(r=1; r<=5; r++)
{
for(c=1; c<=r; c++)
{
System.out.print(c);
}
System.out.println();
}
}
}
Output:
1 12 123 1234 12345
उदाहरणः पिरामिड 2
public class Demo
{
public static void main(String arr[])
{
int a = 1, c, t;
for(c=1; c<=5; c++)
{
for(t=1; t<=c; t++)
{
System.out.print(a);
if(a == 1)
a = 0;
else
a = 1;
}
System.out.println();
if((c%2) == 0)
a = 1;
else
a = 0;
}
}
}
Output:
1 01 101 0101 10101
उदाहरणः फिबोनाकी सिरीज़ प्रिन्ट करना।
import java.util.*;
public class Demo
{
public static void main(String arr[])
{
int a = 1, b = 1, c, t, l;
Scanner ob = new Scanner(System.in);
System.out.print("Enter limit: ");
l = ob.nextInt();
System.out.print(a + " ");
for(t=1; t<=l; t++)
{
System.out.print(b + " ");
c = a + b;
a = b;
b = c;
}
}
}
Output:
Enter limit: 7 1 1 2 3 5 8 13
उदाहरणः पिरामिड 3
public class Demo
{
public static void main(String arr[])
{
int a,b,c,r;
for(r=1; r<=5; r++)
{
for(c=5; c>r; c--)
System.out.print(" ");
for(c=1; c<=r; c++)
System.out.print(c);
System.out.println();
}
}
}
Output:
1 12 123 1234 12345
