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.
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.