i understand that while (1) is an infinite loop .
i have a function that check corrrct input . it's happend when i define a local array with possiblities the function check if there is a match .
also i have
return ch ;
in the while (1)
is this a recursion or the return canceld it (i mean the recursion)
ok then . so while(1) is not a recursion .
so im calling to the function over and over untiil a condition is happen?
this is the same with recursion no ?
when do we use recursion and when we call the function repeatdly for what reason...
It depends on what you are trying to do.
Usually recursion is not recommended since it can eat stack space quickly, but recursive algorithms usually are easier to read. You can rewrite any recursive algorithms to a non-recursive one (usually by simulating the stack).
umm ok. is it right to say that while (1) is a infinity loop ?
for examplt i have a local array that conatin a number of possible choices. i am sending to a function that calld herself untill there is match .
when he found match he output a fit msg.
basiclly , if i hadn't have "return" i could say this is recursion ?
char check_option (constchar options[], int size)
{ //check if the option that pressed is correct
while (1) //infinity loop, stops when correct char will enter
{
char ch = upcase();
for (int i = 0; i < size; ++i)
{
if (ch == options[i])
{
return ch;
}
}
cout << "\nWrong key! try again:";
}
}
Any non-zero value represents a true logical value, and zero represents a false logical value.
Therefore while(1) has an always true condition (e.g infinite loop)
I could not understand why are we using while(1) ?!
simplified code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include<ctype.h>
char check_option (constchar options[], int size)
{ //check if the option that pressed is correct
for (int i = 0; i < size; i++)
{
if (ch == toupper(options[i]))
{
return ch;
}
}
cout << "\nWrong key! try again:";
}
He want only some specific letter to be pressed.
If that key is pressed then it will exit the loop
else it will input try again then get the input key again.
So basically he need an infinite loop for this.
doesn't seem to be any input from where I'm sitting. But we can safely assume there would be as the question was not something like "help this doesn't work!"
yes but this function check the user inputs , somtimes its correct and some times wrong