while(1)

Pages: 12
Feb 21, 2010 at 9:20am
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)
Last edited on Feb 21, 2010 at 9:21am
Feb 21, 2010 at 9:27am
recursion is when a function calls itself, not when a function is called repeatedly in a loop
Feb 21, 2010 at 11:33am
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 ?

still what is the tinny diffrenet between them?
Feb 21, 2010 at 11:57am
Recursion is when a function calls itself :
1
2
3
4
5
void f(int i) {
    //... 
    f(i+1);
    //...
}

Calling another function repeatedly (or otherwise) is not recursion :
1
2
3
4
5
6
7
void g() {}

void f() {
    while(1) { 
        g();
    }
}

Feb 21, 2010 at 12:12pm
ok so what does it good for ?
when do we use recursion and when we call the function repeatdly for what reason...
Last edited on Feb 21, 2010 at 12:21pm
Feb 21, 2010 at 12:30pm
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).

Last edited on Feb 21, 2010 at 12:31pm
Feb 21, 2010 at 12:58pm
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 ?
Feb 21, 2010 at 1:04pm
Post the function, I don't understand what you're saying.
Feb 21, 2010 at 1:23pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char check_option (const char 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:";
	 }
}

is the commands rights?
Feb 21, 2010 at 1:38pm
I don't know what upcase() does, but this function is surely not a recursive function. (unless upcase() calls check_option (which I doubt))
Feb 21, 2010 at 1:58pm
no upcase is toupper function ... she converts small to capital letter.

what does the (1) refer to ? while (1) what is his condition ? what is
Last edited on Feb 21, 2010 at 2:29pm
Feb 21, 2010 at 4:57pm
what does the (1) refer to ?

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)
Feb 21, 2010 at 5:59pm
yes but this function check the user inputs , somtimes its correct and some times wrong
Feb 21, 2010 at 8:04pm
When you return from it, you exit the function (and leave the infinite loop you were in).
Feb 22, 2010 at 1:27am
while(1)

is equivalent also to
while(true)//always correct

both of them are infinite loop. If you want to exit the loop, 'return' and 'break' keyword can be used.
Feb 22, 2010 at 8:34am
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 (const char 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:";
	 
}


Feb 22, 2010 at 8:51am
Well you misunderstand what cppbeg posted.

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.
Feb 22, 2010 at 9:05am
my mistake.

I still wonder whats loaded in char ch char ch = upcase();
and when cppbeg says
no upcase is toupper function ... she converts small to capital letter.
Feb 22, 2010 at 11:41am
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


can you post the other function or more code?
Last edited on Feb 22, 2010 at 11:44am
Feb 22, 2010 at 11:59am
I think cppbeg means toupper convert the lower case character to upper case character .
Am i right cppbeg ?
Pages: 12