Understanding Infinite Loops in C Programming: Concept and Flowchart
Learn about infinite loops in C programming, where a set of statements is executed indefinitely without termination. Discover how true conditions lead to endless loops and explore a flowchart illustrating the concept. Master the structure of infinite loops and avoid common pitfalls in your C programs.
C - Infinite Loop
In C language, an infinite loop (or an endless loop) is a loop that executes a set of statements forever without terminating. It has a true condition that keeps the program running continuously.
Flowchart of an Infinite Loop
If the flow of the program is unconditionally directed to any previous step, an infinite loop is created.
An infinite loop is very rarely created intentionally. However, in embedded headless systems and server applications, the application runs in an infinite loop to listen to client requests. In other cases, infinite loops are mostly due to programming errors.
How to Create an Infinite Loop in C?
To create an infinite loop, use a loop construct (while, do while, or for) with a non-zero value as the test condition. Generally, 1 is used as the test condition.
Example
Example
#include
int main() {
while (1) {
printf("Hello World");
}
return 0;
}
Output
Hello WorldHello WorldHello World...
Types of Infinite Loops in C
There are three types of infinite loops in C: while, do while, and for loops. These loops execute the code statement continuously.
Infinite While Loop
The while
keyword forms a loop that continues until the condition becomes false. If the increment statement is omitted, the loop can become infinite.
Example 1
#include
// infinite while loop
int main() {
int i = 0;
while (i <= 10) {
// i++;
printf("i: %d\n", i);
}
return 0;
}
Output
i: 0
i: 0
i: 0
...
Example 2
#include
// infinite while loop
int main() {
while (1) {
printf("Hello World\n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
...
The syntax of while loop is:
while (condition) {
// code
}
Note: If a semicolon is placed after while
, the loop becomes an infinite loop with no body.
Example 3
#include
// infinite while loop
int main() {
int i = 0;
while (i < 10); {
i++;
printf("Hello World\n");
}
return 0;
}
Output
(no output)
Infinite For Loop
The for
loop in C is used for iterating a code block. If the middle clause (condition) is omitted, the loop becomes infinite.
Example 1
#include
// infinite for loop
int main() {
int i;
for (i = 1; ; i++) {
printf("Hello World\n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
...
Example 2
#include
// infinite for loop
int main() {
for (int i = 10; i >= 1; i++) {
i++;
printf("Hello World\n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
...
Example 3
#include
// infinite for loop
int main() {
for (int i = 1; i <= 10; i--) {
printf("Hello World\n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
...
Example 4
#include
// infinite for loop
int main() {
int i;
for ( ; ; ) {
printf("Hello World\n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
...
Infinite Do While Loop
The do while
loop can also create an infinite loop if the condition is always true.
Example
#include
int main() {
do {
printf("Hello World\n");
} while (1);
return 0;
}
Output
Hello World
Hello World
Hello World
...
How to Break an Infinite Loop in C?
To break an infinite loop, use a conditional break
statement within the loop.
Example
#include
int main() {
for (int i = 1; ; ) {
printf("Hello World\n");
if (i == 5) break;
i++;
}
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
How to Stop an Infinite Loop Forcefully in C?
When a program enters an infinite loop, it must be forcibly stopped using "Ctrl + C" or "Ctrl + Break" depending on the operating system.
Example
#include
int main() {
int a = 0;
LOOP:
a++;
printf("a: %d\n", a);
goto LOOP;
return 0;
}
Output
a: 1
a: 2
...
a: 10
a: 11
...