Loops & its Types in Java
Loops & its Types in Java
In programming, a loop is a control structure that repeatedly executes a block of code as long as a specified condition is met. Loops are fundamental to performing repetitive tasks efficiently.
Types of Loops in Java
1. For Loop
A `for` loop is used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and increment/decrement.
Syntax:
for (initialization; condition; increment/decrement) {
// statements
}
Example:
for (int i = 0 ; i < 5 ; i++ ) {
System.out.println( "Value of i: " + i);
}
2. While Loop
A `while` loop is used when the number of iterations is not known and depends on a condition. The loop will continue as long as the condition is true.
Syntax:
while ( condition ) {
// statements
}
Example:
int i = 0 ;
while ( i < 5 ) {
System.out.println(" Value of i: " + i );
i++ ;
}
3. Do-While Loop
A `do-while` loop is similar to a `while` loop, but it guarantees that the loop's body will be executed at least once since the condition is evaluated after the loop's body.
Syntax:
do {
// statements
} while ( condition );
Example:
int i = 0 ;
do {
System.out.println(" Value of i: " + i );
i++ ;
} while ( i < 5 );
4. Enhanced For Loop (for-each)
The enhanced `for` loop, also known as the `for-each` loop, is used to iterate over elements in an array or a collection.
Syntax:
for ( elementType element : arrayOrCollection ) {
// statements
}
Example:
int[] numbers = { 1, 2, 3, 4, 5 };
for (int number : numbers ) {
System.out.println(" Number: " + number );
}
Summary:
- For Loop: Best when the number of iterations is known.
- While Loop: Used when the number of iterations is unknown and depends on a condition.
- Do-While Loop: Similar to `while` loop but guarantees at least one execution.
- Enhanced For Loop: Convenient for iterating over arrays and collections.
जावा में लूप और लूप के प्रकार
प्रोग्रामिंग में, लूप एक नियंत्रण संरचना है जो निर्दिष्ट शर्त पूरी होने तक कोड के एक ब्लॉक को बार-बार निष्पादित करती है। लूप दोहरावदार कार्यों को कुशलता से करने के लिए मौलिक हैं।
जावा में लूप के प्रकार
1. For Loop
`for` लूप का उपयोग तब किया जाता है जब पुनरावृत्तियों की संख्या पहले से ज्ञात होती है। इसमें तीन भाग होते हैं: प्रारंभिककरण, शर्त, और वृद्धि/कमी।
Syntax:
for (initialization; condition; increment/decrement) {
// statements
}
उदाहरण:
for (int i = 0 ; i < 5 ; i++ ) {
System.out.println( "i का मान: " + i);
}
2. While Loop
`while` लूप का उपयोग तब किया जाता है जब पुनरावृत्तियों की संख्या ज्ञात नहीं होती और यह एक शर्त पर निर्भर करती है। यह लूप तब तक जारी रहेगा जब तक शर्त सही है।
Syntax:
while ( condition ) {
// statements
}
उदाहरण:
int i = 0 ;
while ( i < 5 ) {
System.out.println(" i का मान: " + i );
i++ ;
}
3. Do-While Loop
`do-while` लूप `while` लूप के समान है, लेकिन यह गारंटी देता है कि लूप का शरीर कम से कम एक बार निष्पादित होगा क्योंकि शर्त लूप के शरीर के बाद जांची जाती है।
Syntax:
do {
// statements
} while ( condition );
उदाहरण:
int i = 0 ;
do {
System.out.println(" i का मान: " + i );
i++ ;
} while ( i < 5 );
4. Enhanced For Loop (for-each)
एन्हांस्ड `for` लूप, जिसे `for-each` लूप भी कहा जाता है, का उपयोग किसी array या collection के तत्वों को दोहराने के लिए किया जाता है।
Syntax:
for ( elementType element : arrayOrCollection ) {
// statements
}
उदाहरण:
int[] numbers = { 1, 2, 3, 4, 5 };
for (int number : numbers ) {
System.out.println(" संख्या: " + number );
}
सारांश:
- For Loop: जब पुनरावृत्तियों की संख्या ज्ञात हो तब सबसे अच्छा।
- While Loop: जब पुनरावृत्तियों की संख्या अज्ञात हो और यह एक शर्त पर निर्भर हो।
- Do-While Loop: `while` लूप के समान लेकिन कम से कम एक निष्पादन की गारंटी देता है।
- Enhanced For Loop: arrays और collections के तत्वों को दोहराने के लिए सुविधाजनक।
Post a Comment