C - Infinite Loop
C - Infinite Loop
C - Infinite Loop
In C language, an infinite loop (or, an endless loop) is a never-ending looping
construct that executes a set of statements forever without terminating the loop. It
has a true condition that enables a program to run continuously.
Example
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 1/10
6/16/24, 12:24 PM C - Infinite Loop
#include <stdio.h>
int main() {
while (1)
{
printf("Hello World");
}
return 0;
}
The while keyword is used to form a counted loop. The loop is scheduled to repeat
till the value of some variable successively increments to a predefined value.
However, if the programmer forgets to put the increment statement within the loop
body, the test condition doesn’t arrive at all, hence it becomes endless or infinite.
Example 1
#include <stdio.h>
int i = 0;
while (i <= 10){
// i++;
printf("i: %d\n", i);
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 2/10
6/16/24, 12:24 PM C - Infinite Loop
}
return 0;
}
Output
Since the increment statement is commented out here, the value of "i" continues to
remain "0", hence the output shows "i: 0" continuously until you forcibly stop the
execution.
i: 0
i: 0
i: 0
...
...
Example 2
The parenthesis of while keyword has a Boolean expression that initially evaluates
to True, and is eventually expected to become False. Note that any non-zero number
is treated as True in C. Hence, the following while loop is an infinite loop:
#include <stdio.h>
while(1){
printf("Hello World \n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 3/10
6/16/24, 12:24 PM C - Infinite Loop
...
...
while (condition){
...
...
}
Note that the there is no semicolon symbol in front of while, indicating that the
following code block (within the curly brackets) is the body of the loop. If we place a
semicolon, the compiler treats this as a loop without body, and hence the while
condition is never met.
Example 3
In the following code, an increment statement is put inside the loop block, but
because of the semicolon in front of while, the loop becomes infinite.
#include <stdio.h>
int i = 0;
while(i < 10);{
i++;
printf("Hello World \n");
}
return 0;
}
Output
When the program is run, it won't print the message "Hello World". There is no output be
The for loop in C is used for performing iteration of the code block for each value of a
variable from its initial value to the final value, incrementing it on each iteration.
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 4/10
6/16/24, 12:24 PM C - Infinite Loop
Example 1
Note that all the three clauses of the for statement are optional. Hence, if the middle
clause that specifies the final value to be tested is omitted, the loop turns infinite.
#include <stdio.h>
int i;
for(i=1; ; i++){
i++;
printf("Hello World \n");
}
return 0;
}
Output
The program keeps printing Hello World endlessly until you stop it forcibly, because it
has no effect of incrementing "i" on each turn.
Hello World
Hello World
Hello World
...
...
You can also construct a for loop for decrementing the values of a looping variable.
In that case, the initial value should be greater than the final test value, and the
third clause in for must be a decrement statement (using the "--" operator).
If the initial value is less than the final value and the third statement is decrement,
the loop becomes infinite. The loop still becomes infinite if the initial value is larger
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 5/10
6/16/24, 12:24 PM C - Infinite Loop
Example 2
#include <stdio.h>
int main(){
Output
Hello World
Hello World
Hello World
...
...
Example 3
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 6/10
6/16/24, 12:24 PM C - Infinite Loop
}
}
Output
Hello World
Hello World
Hello World
...
...
Example 4
If all the three statements in the parenthesis are blank, the loop obviously is an
infinite loop, as there is no condition to test.
#include <stdio.h>
int main(){
int i;
Output
Hello World
Hello World
Hello World
...
...
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 7/10
6/16/24, 12:24 PM C - Infinite Loop
An infinite loop can also be implemented using the do-while loop construct. You have
to use 1 as the test condition with the while.
Example
#include <stdio.h>
int main() {
do
{
printf("Hello World\n");
} while (1);
return 0;
}
Hello World
Hello World
Hello World
Hello World
...
...
Example
In the following program, there is no test statement in for, but we used a break
statement to make it a finite loop.
#include <stdio.h>
int main(){
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 8/10
6/16/24, 12:24 PM C - Infinite Loop
if(i == 5)
break;
}
return 0;
}
Output
The program prints "Hello World" till the counter variable reaches 5.
Hello World
Hello World
Hello World
Hello World
Example
#include <stdio.h>
int main(){
// do loop execution
LOOP:
a++;
printf("a: %d\n", a);
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 9/10
6/16/24, 12:24 PM C - Infinite Loop
goto LOOP;
return 0;
}
Output
When executed, the above program prints incrementing values of "a" from 1
onwards, but it doesn’t stop. It will have to be forcibly stopped by pressing "Ctrl +
Break" keys.
a: 1
a: 2
...
...
a: 10
a: 11
...
...
https://www.tutorialspoint.com/cprogramming/c_infinite_loop.htm 10/10