You are here:   HomeC Programming (Theory)Control Structures in CEndless for loop in C
by Dinesh Thakur Category: Control Structures

In the expression for for loop the inclusion of the expressions are optional. However, two semicolons must be included. An endless for loop may be written as shown below.

for(;;)

In this form there are no expressions, only two semicolons are present. The loop may be stopped by using the statement break; . Program provides an illustration of application of an endless/or loop.

Illustrates an endless for loop and uses break statement to stop it.

#include<stdio.h>

#include<math.h>
void main()
{
    int Angle =0;
    double PI = 3.14159, RAngle;
    clrscr();
    for(;;) //endless for loop
    {
       RAngle = Angle *PI/180;
       printf("Angle= %d deg.\t sin(%d) = %.4lf\n",Angle,Angle, sin(RAngle) );
       Angle = Angle + 45;
       if (Angle >180)
          break;
    } // break included to exit from the loop.
}

The expected output is given below.

     Endless for loop in C

In the above program, an endless for loop is used to calculate sine of an angle, where the value of the angle varies from 0 degrees to 180 degrees. The statement break; is used for getting out of loop when the value increases beyond 180. The header file <math.h> is included in order to use its function sin().For using the sin()function, the angle is converted into radians, because the argument of sin() is the value of angle in radians. The program then calculates the value of sin (angle) and displays it.



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article