-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteLoop.java
More file actions
61 lines (51 loc) · 1.7 KB
/
Copy pathInfiniteLoop.java
File metadata and controls
61 lines (51 loc) · 1.7 KB
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
package Phase1_CoreLanguage.Loops;
import java.util.ArrayList;
/**One of the most common mistakes while implementing any sort of looping is that that it may not ever exit,
* that is the loop runs for infinite time. This happens when the condition fails for some reason.**/
public class InfiniteLoop {
public static void main(String[] args)
{
// infinite loop because condition is not apt
// condition should have been i>0.
for (int i = 5; i != 0; i += 2)
{
System.out.println(i);
}
int x = 5;
// infinite loop because update statement
// is not provided.
while (x == 5)
{
System.out.println("In the loop");
}
//OUTPUT
//Only for loop is executing because the for loop is infinite loop and before terminating while loop is not executing.
//5
//7
//9
//11
//13
//15
//17
//19
//21
//23
//25
//27
//Many More n Number
//Ctrl + c or terminate the execution of class then it'll exit from the loop
ArrayList<Integer> ar = new ArrayList<>();
for (int i = 0; i < Integer.MAX_VALUE; i++)
{
ar.add(i);
}
//OUTPUT
//Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
//at java.util.Arrays.copyOf(Unknown Source)
//at java.util.Arrays.copyOf(Unknown Source)
//at java.util.ArrayList.grow(Unknown Source)
//at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
//at java.util.ArrayList.add(Unknown Source)
//at article.Integer1.main(Integer1.java:9)
}
}