while(1)

Pages: 12
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
recursion is when a function calls itself, not when a function is called repeatedly in a loop
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?
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();
    }
}

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
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
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 ?
Post the function, I don't understand what you're saying.
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?
I don't know what upcase() does, but this function is surely not a recursive function. (unless upcase() calls check_option (which I doubt))
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
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)
yes but this function check the user inputs , somtimes its correct and some times wrong
When you return from it, you exit the function (and leave the infinite loop you were in).
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.
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:";
	 
}


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.
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.
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
I think cppbeg means toupper convert the lower case character to upper case character .
Am i right cppbeg ?
Pages: 12