Home » Programming Tips & Tricks » C - Tips & Tricks

Infinite for loop without condition in C

By: IncludeHelp, on 21 FEB 2017

Learn: How to run an infinite for loop without using condition? Here is a trick to make for loop as an infinite loop without using any value within the loop statement.

We know that, generally for loop has three parts initialization of loop counter, conditional statement and increment/decrement (or updating the loop counter), we can make a for loop infinite without using these three parts.

Here is the statement

for(;;);

Keep blank all three parts and terminate for loop using semicolon. This loop will run as infinitely.

Here is the program

#include <stdio.h>

int main()
{
    printf("Hello\n");
    for(;;);
    printf("world\n");

    return 0;
}

Output

Hello
[Infinite...] "Hey, this line will not print"





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.