Preventing infinite loops

Go down

Preventing infinite loops

Post  Quicksilver on Mon Sep 22, 2008 11:28 am

Name the most common run time problem with using while/ do-while loops? - Infinite loop is the unquestionable winner. They result in unexpected outputs; They prevent further execution; They result in program crashes and what not. What I'll teach you today is "How to write your do-while/while loop conditions to prevent infinite loopings in your programs." But before that you'll need to know about this nifity function: kbhit ()

kbhit ();
 Defined in: <conio.h>
 Description: The kbhit function is an acronym for Keyboard Hit. The function returns an integer value when called. The return values are more like true and false - If 0 then false and if non-zero then true. The working of the function goes something like this: When the function is executed it checks the buffer for any inputs from the keyboard. If any are present the function returns a non-zero value (true) and if not then 0 is returned (false). Note that the keyboard buffer is not cleared i.e. the input from the keyboard can still be retrived using the getch () or getche () function.

Now, we move to our implementation -
 Well thats pretty easy... when you write the condition in your while/do-while loop add this to it:
while ( <your conditional statement> && !kbhit () )
{
  ........
  ..... 
  ...
  .
};
and you are done!

How did this work??
 All you need to know is the boolean algebra involved. 
Suppose, inspite of your condition the while/do-while statement enters an infinite loop i.e. the value for <your conditional statement> = 1. 
Now, when the while condition can be said to be equated to:
 while ( <your conditional statement> && !kbhit () )
 = while ( 1 && !kbhit () ) 
 = while ( !kbhit () ) [By boolean algebra 1 AND <condition> = <condtion>]

So when your condtional statement becomes useless the while loop becomes completely dependent on the kbhit function. Remember that the kbhit returns a 0 till you hit a key. Therefore when the loop is checked...
  whlie (!kbhit ())
= while (!0)
= whlie (1)
i.e. the loop will re-execute. But when you press a key...
  whlie (!kbhit ())
= while (!<non zero val>)
= whlie (0)
asta-la-vista infinte loops!

Now a small assignment for the curious....
Copy the following code into your TC compiler;
____________________
#include <stdio.h>
#include <conio.h>

void main ()
{
 clrscr ();

  while ( !kbhit () )
  {
  getch ();
  };

}
___________________
Try exiting out of this loop. Those who do, can send me a reply about how they managed it, did they learn something new in the process, what was it that they learnt and why they couldn't manage exiting the loop in the way that I explained earlier in the article.

Keep learning!
Tridiv Sardesai
avatar
Quicksilver

Number of posts : 36
Age : 31
Location : Pune, Maharashtra, India
Branch : Computer Engineering
Registration date : 2008-08-08

View user profile

Back to top Go down

Re: Preventing infinite loops

Post  Admin on Wed Sep 24, 2008 9:43 pm

nice post ....keep them coming Smile
avatar
Admin
Admin

Number of posts : 37
Registration date : 2008-08-08

View user profile http://buzztalk.1talk.net

Back to top Go down

Re: Preventing infinite loops

Post  aditya on Sun Oct 12, 2008 12:39 pm

there is always the option of ctrl-break..

aditya

Number of posts : 4
Age : 29
Location : Pune
Registration date : 2008-08-25

View user profile

Back to top Go down

Re: Preventing infinite loops

Post  Quicksilver on Mon Oct 20, 2008 1:39 pm

aditya wrote:there is always the option of ctrl-break..
I exiting a currupt piece of code there is always some method of coming out. While one can (in a case of extremity) argue that he/she can always reboot the machine - the method will be termed as 'inelegant' and not fit for a programmer. the ctrl+brk option does work but not always some times it makes not only your program but also TC++ crash in which case you lose your unsaved code - which can some times be just a line or can be a hundred more. In any case a prorammer's code should be able to handle all possible exceptions (errors) that occur by itself, prevetig the infinite loop is one of the many methods...
avatar
Quicksilver

Number of posts : 36
Age : 31
Location : Pune, Maharashtra, India
Branch : Computer Engineering
Registration date : 2008-08-08

View user profile

Back to top Go down

Re: Preventing infinite loops

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum