-
Notifications
You must be signed in to change notification settings - Fork 0
/
LOOPs.txt
68 lines (62 loc) · 1.98 KB
/
LOOPs.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
loops in java with examples
The Java for loop is used to iterate a part of the program several times.
If the number of iteration is fixed, it is recommended to use for loop.
There are three types of for loops in Java.
In Java, loops are used to execute a block of code repeatedly as long as a specified condition is true.
There are several types of loops available:
1. **for loop**: It repeats a block of code a fixed number of times.
```java
for (initialization; condition; update) {
// code to be executed
}
```
Example:
```java
for (int i = 0; i < 5; i++) {
System.out.println("Iteration " + i);
}
```
2. **while loop**: It repeats a block of code as long as a specified condition is true.
```java
while (condition) {
// code to be executed
}
```
Example:
```java
int count = 0;
while (count < 5) {
System.out.println("Count: " + count);
count++;
}
```
3. **do-while loop**: It is similar to the while loop, but it guarantees that the block of code is executed at least once, even if the condition is initially false.
```java
do {
// code to be executed
} while (condition);
```
Example:
```java
int number = 5;
do {
System.out.println("Number: " + number);
number--;
} while (number > 0);
```
4. **for-each loop (Enhanced for loop)**:
It iterates over elements in an array or collection.
```java
for (type variable : array/collection) {
// code to be executed
}
```
Example:
```java
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println("Number: " + num);
}
```
Each type of loop has its own use case, and you can choose the one that best fits your specific scenario.
Loops are essential for iterating over arrays, processing collections, and performing repetitive tasks in Java programs.