An infinite loop is a sequence of instructions or statements in a computer program executing endlessly either due to the loop having no terminating condition, a specified condition never be met or one that causes the loop to start over.
Infinite for loop
A for loop can be an infinite either due to condition statement or increment statement. It can be also possible that the condition parameter or increment statement again gets modified in the loop body. etc..
1
2 for(i = 0; i < -1; i++) {
3
4 }
5
6
7 for(i = 0; i < 5; ) {
8
9 }
10
11
12 for(i = 0; i < 5; i++) {
13
14 i--;
15 }
Infinite while loop
A while loop can be an infinite either due to incorrect condition which never met or conditional statements variable modified in the body of while loop. etc..
1
2 int i = 0;
3 while(i < -1) {
4 i++;
5 }
6
7
8 while(1) {
9
10 }
11
12
13 int i = 0;
14 while(i < 5) {
15 i++;
16
17 i = 1;
18 }
19
20
21 int i = 0;
22 while(i < 5) {
23
24 continue;
25 i++;
26 }
Infinite do.. while loop
Similar to while loop, a do.. while loop can be infinite due to incorrect condition which never met or conditional statements variable modified in the body of while loop. etc..
1
2 int i = 0;
3 do{
4 i++;
5 } while(i < -1) ;
6
7
8 do {
9
10 } while(1);
11
12
13 int i = 0;
14 do {
15 i++;
16
17 i = 1;
18 } while(i < 5);
19
20
21 int i = 0;
22 do {
23
24 continue;
25 i++;
26 } while(i < 5);
Note : There are so many root cause where looping statement can be infinite which depends on condition and condition variable or incorrect order of statements.
Related options for your search