Infinite Loop
Infinite loop is a repetition structure that loops forever, either due to it contains no condition that can cause the loop to be terminated or having condition that can never be satisfied. Let us see how while, do while and for loop statement can be set up to get an infinite loop.
Having an expression within the parenthesis of while and do while (second expression of for loop) that always returns true (non-zero value) can result an infinite loop. The following programs are example of infinite loop of while, do while and for repetition structure.
Sometimes infinite loop can be useful, for example when we want to write a program that computes the average of an arbitrary number of integers. A sample program is given below. The program should be able to allow the user to input any number of integers until he is satisfied. In this case, we have to define an infinite loop so as the program can perform the specific task. But what can we do to have the means of exiting the infinite loop? The answer is the statements in the loop's body must contains the means of exiting from the loop. Remember keyword break from Tutorial 5.4. We have seen how break can be used to jump out of switch selection bypassing the following statements. Similarly break can be used to exit a loop from any point of its body.
This program computes the average of any number of integers.
Enter integers (-1 to stop input):
10
5
7
-1
The average is 7.33
Next: Functions
Infinite loop is a repetition structure that loops forever, either due to it contains no condition that can cause the loop to be terminated or having condition that can never be satisfied. Let us see how while, do while and for loop statement can be set up to get an infinite loop.
Having an expression within the parenthesis of while and do while (second expression of for loop) that always returns true (non-zero value) can result an infinite loop. The following programs are example of infinite loop of while, do while and for repetition structure.
#include <stdio.h> int main() { int i = 0; while(1) { // loop condition is always true printf("%d\n", i); i++; } return 0; }
#include <stdio.h> int main() { int i = 0; do { printf("%d\n", i); i++; }while(1); // loop condition is always true return 0; }
#include <stdio.h> int main() { int i = 0; for(i = 0; 1 ; i++) { // loop condition is always true printf("%d\n", i); } return 0; }The loop condition (expression) is set to 1, resulting an infinite loop.
Sometimes infinite loop can be useful, for example when we want to write a program that computes the average of an arbitrary number of integers. A sample program is given below. The program should be able to allow the user to input any number of integers until he is satisfied. In this case, we have to define an infinite loop so as the program can perform the specific task. But what can we do to have the means of exiting the infinite loop? The answer is the statements in the loop's body must contains the means of exiting from the loop. Remember keyword break from Tutorial 5.4. We have seen how break can be used to jump out of switch selection bypassing the following statements. Similarly break can be used to exit a loop from any point of its body.
#include <stdio.h> int main() { int i = 0; float num = 0; float sum = 0; float average = 0; printf("This program computes the"); printf(" average of any number of integers.\n"); printf("Enter integers (-1 to stop input): \n"); while(1) { // loop condition is always true scanf("%f", &num); if (num == -1) break; else { i++; sum += num; } } average = sum / i; printf("The average is %.2f\n", average); return 0; }Sample output:
This program computes the average of any number of integers.
Enter integers (-1 to stop input):
10
5
7
-1
The average is 7.33
int i = 0; float num = 0; float sum = 0; float average = 0;Here we declare four (4) variables namely i, num, sum and average. Variable i is used to count the number of integers entered by the user. The entered integer is stored in variable num. The summation of all integers is stored in variable sum and lastly variable average is used to store the average.
printf("This program computes the"); printf(" average of any number of integers.\n"); printf("Enter integers (-1 to stop input): \n");Then we display the user instruction using printf. We inform the user to input -1 if he wants to end input process.
while(1) { // loop condition is always true scanf("%f", &num); if (num == -1) break; else { i++; sum += num; } }We set up an the loop using while statement. Since loop condition has the value of 1, the block of statements will be repeated forever. In the body of the loop, we have read input from user statement. The input integer is stored in variable num. Then we compare the value of num with -1. If the comparison is true indicating the user wants to end the input process, break statement will be executed. Else the i is incremented and the value entered is added to variable sum.
average = sum / i; printf("The average is %.2f\n", average);Finally we calculate the average and display the result.
Next: Functions
4 comments:
for(;i=1;);
is this infinite loop in c?
nupp, it will show a syntax error
infinite loop
for( ; ; )
while(6){
printf("hello\n");
}
what is o\p?
why i put 1 into while looping while(1), if i put 6 what happens?
Post a Comment