How to Create Endless Loops in C Programming

By Dan Gookin

Beware the endless loop! When a C program enters an endless loop, it either spews output over and over without end or it sits there tight and does nothing. Well, it’s doing what you ordered it to do, which is to sit and spin forever.

Sometimes, this setup is done on purpose, but mostly it happens because of programmer error. And with the way loops are set up in C, it’s easy to unintentionally loop ad infinitum.

A Common Way to Make an Endless Loop illustrates a common endless loop, which is a programming error, not a syntax error.

A COMMON WAY TO MAKE AN ENDLESS LOOP

#include <stdio.h>
int main()
{
 int x;
 for(x=0;x=10;x=x+1)
 {
 puts("What are you lookin' at?");
 }
 return(0);
}

The problem with the code in A Common Way to Make an Endless Loop is that the for statement’s exit condition is always true: x=10. Read it again if you didn’t catch it the first time, or just do Exercise 9-18.

Exercise 1: Type the source code for A Common Way to Make an Endless Loop. Save, build, and run.

The compiler may warn you about the constant TRUE condition in the for statement. Code::Blocks should do that, and any other compiler would, if you ratcheted up its error-checking. Otherwise, the program compiles and runs — infinitely.

  • To break out of an endless loop, press Ctrl+C on the keyboard. This trick works only for console programs, and it may not always work. If it doesn’t, you need to kill the process run amok.

  • Endless loops are also referred to as infinite loops.

How to loop endlessly but on purpose

Occasionally, a program needs an endless loop. For example, a microcontroller may load a program that runs as long as the device is on. When you set up such a loop on purpose in C, one of two statements is used:

for(;;)

Read this statement as “for ever.” With no items in the parentheses, but still with the required two semicolons, the for loop repeats eternally — even after the cows come home. Here’s the while loop equivalent:

while(1)

The value in the parentheses doesn’t necessarily need to be 1; any True or non-zero value works. When the loop is endless on purpose, however, most programmers set the value to 1 simply to self-document that they know what’s up.

How to break out of a loop

Any loop can be terminated instantly — including endless loops — by using a break statement within the loop’s repeating group of statements. When break is encountered, looping stops and program execution picks up with the next statement after the loop’s final curly bracket. Get Me Outta Here! demonstrates the process.

GET ME OUTTA HERE!

#include <stdio.h>
int main()
{
 int count;
 count = 0;
 while(1)
 {
 printf("%d, ",count);
 count = count+1;
 if( count > 50)
 break;
 }
 putchar('n');
 return(0);
}

The while loop at Line 8 is configured to go on forever, but the if test at Line 12 can stop it: When the value of count is greater than 50, the break statement (refer to Line 13) is executed and the loop halts.

Exercise 2: Build and run a new project using the source code from Get Me Outta Here!

Exercise 3: Rewrite the source code from Get Me Outta Here! so that an endless for loop is used instead of an endless while loop.

You don’t need to construct an endless loop to use the break statement. You can break out of any loop. When you do, execution continues with the first statement after the loop’s final curly bracket.

How you can screw up a loop with C

There are two common ways to mess up a loop. These trouble spots crop up for beginners and pros alike. The only way to avoid these spots is to keep a keen eye so that you can spot ’em quick.

The first goof-up is specifying a condition that can never be met; for example:

for(x=1;x==10;x=x+1)

In the preceding line, the exit condition is false before the loop spins once, so the loop is never executed. This error is almost as insidious as using an assignment operator (a single equal sign) instead of the “is equal to” operator (as just shown).

Another common mistake is misplacing the semicolon, as in

for(x=1;x<14;x=x+1);
{
 puts(“Sore shoulder surgery”);
}

Because the first line, the for statement, ends in a semicolon, the compiler believes that the line is the entire loop. The empty code repeats 13 times, which is what the for statement dictates. The puts() statement is then executed once.

Those rogue semicolons can be frustrating!

The problem is worse with while loops because the do-while structure requires a semicolon after the final while statement. In fact, forgetting that particular semicolon is also a source of woe. For a traditional while loop, you don’t do this:

while(x<14);
{
 puts(“Sore shoulder surgery”);
}

The severely stupid thing about these semicolon errors is that the compiler doesn’t catch them. It believes that your intent is to have a loop without statements. Such a thing is possible.

A FOR LOOP WITH NO BODY

#include <stdio.h>
int main()
{
 int x;
 for(x=0;x<10;x=x+1,printf("%dn",x))
 ;
 return(0);
}

In the example shown in A for Loop with No Body, the semicolon is placed on the line after the for statement (refer to Line 8 in A for Loop with No Body). That shows deliberate intent.

You can see that two items are placed in the for statement’s parentheses, both separated by a comma. That’s perfectly legal, and it works, though it’s not quite readable.

Exercise 4: Type the source code from A for Loop with No Body into your editor. Build and run.

Though you can load up items in a for statement’s parentheses, it’s rare and definitely not recommended, for readability’s sake.