Table of Contents
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
Quizzes on Operator |
Easy Quiz on Operators in C part 1 |
Easy quiz on operators in C part 2 |
Easy quiz on operators in C part 3 |
Quizzes on Loops |
Easy quiz on loops in C part 1 |
Easy quiz on loops in C part 2 |
Easy quiz on loops in C part 3 |
Quizzes on Decision Making Statement |
Easy Quiz On Decision Making Statements Part 1 |
Easy Quiz On Decision Making Statements Part 2 |
Quizzes on String |
Easy quiz on String in C programming part 1 |
Quizzes on Array |
Easy quiz on Array in C programming – part 1 |
5 – Best Java program to convert decimal to binary |
New Quizzes Coming soon.. |
PRACTICALS IN C PROGRAM
Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs to understand the basic data types and I/O.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
