Best post to learn loop in C part 3-infinite loop

Infinite loop in C programming

All loops depends on condition and if the condition is true then loops will execute the block of code, and if the condition is false loops will terminate their self. But what is we don’t want to terminate any loop in c program? We take help of infinite loop.

In simple words infinite loop is the loop which will never terminates and it will run throughout the program, and execute the code. 

We can create infinite loop from all the loop present in C programming and also infinite loop can be created from goto statement.

In loop we just have to set condition which will never be invalid, example we can set 1 in while which is indicates true condition.

Infinite loop will effect memory, to stop it we have to generate interrupt e.g. Keyboard interrupt “Ctrl+c” 

Infinite loop using while loop

    #include<stdio.h>
void main()
{
while(1)
{
printf("ITVoyagersn");
}
}
Output:

  ITVoyagers
  ITVoyagers
  ITVoyagers
    ITVoyagers
.
.

In C programming 1 represents true and 0 represents false. while loop will check if the condition is true, and if it is true it will run block of code. In above example we have set while loop’s condition to be true by setting 1 in its parameter and while loop will run infinite times because 1 represent true condition.

Infinite loop using do-while loop

    #include<stdio.h>
void main()
{
do
{
printf("ITVoyagersn");
}
while(1)
}
Output:

  ITVoyagers
  ITVoyagers
  ITVoyagers
    ITVoyagers
.
.

In do-while if we set 1 as a condition in while then it will convert do-while to infinite loop.

Infinite loop using for loop

    #include<stdio.h>
void main()
{
for(;;)
{
printf("ITVoyagersn");
}
}
Output:

  ITVoyagers
  ITVoyagers
  ITVoyagers
    ITVoyagers
.
.

If we want to create infinite loop using for loop then we don’t need to pass any parameter in for loop.

Infinite loop using goto statement

    #include<stdio.h>
void main()
{
Label_1:
printf("ITVoyagersn");
goto Label_1;
}
Output:

  ITVoyagers
  ITVoyagers
  ITVoyagers
    ITVoyagers
.
.

We can create infinite loop using goto statement and label. In above example goto statement will continuously make jump to Label_1 and all the statements between will get executed.

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment