Control Loop programs
This section contains example programs demonstrating the loop construction.
Program using char data type and simple for loop: #include /* library header */
main() { char ch;
for (ch = 'A' ; ch <= 'z' ; ch++) printf("%c\n" , ch); }
This program demonstrates the while loop. #include
main() { int lower , upper , step; float fahr , celsius;
lower = 0 ; upper = 300; step = 20 ;
fahr = lower;
while ( fahr <= upper ) { celsius = (5.0 / 9.0) * (fahr - 32.0); printf("%4.0f %6.1f\n" , fahr , celsius); fahr = fahr + step; } }
This program uses do-while and for loop constructions and standard library mathematical functions. #include #include
main() { int i , j , k; double a=10.0 , b=.0;
do { printf("%f\n" , pow(a,b)); /* standard function */ b++; } while (b<100);
i = 2; for (j=2 ; j<100 ; j=j+2) { i=i*j ; printf("%d\n" , i); } }
|
| Author: Tejwant Singh Randhawa 15 Jun 2008 | Member Level: Silver Points : 2 |
nice programs collection in which he explained for, while & do...while loop. we can also create a infinite loop for while, for & do...while loop like this.
For Infinite loop for(;;) printf("hello");
this will print hello infinite times using for loop.
while Infinite loop int a=1; while(a<10) { printf("hello"); } this will print hello infinite times using while loop.
do...while Infinite loop int a=1 do { printf("hello"); } while(a<10)
this will print hello infinite times using do...while loop
|