Programming Example
Infinite while loop Example 3
Infinite loop: var value will keep decreasing because of –- operator, hence it will always be <= 10.
#include <stdio.h>
int main()
{
int var =5;
while (var <=10)
{
printf("%d \n", var);
var--;
}
return 0;
}
-15563
-15564
-15565
-15566
-15567
-15568
-15569
...........
............
............
..........
infinite time
–- operator, hence it will always be <= 10. First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.