469,282 Members | 2,217 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

while(false) { }

sokoun
30
Hi all,

can anyone tell me what this statement means,

while(false){
do_something();
}


what it means, and where the false condition come from. moreover, how this statement execute in the program.

thanks in advance.

sokoun.
Sep 6 '07 #1
14 13621
Savage
1,764 Expert 1GB
Hi all,

can anyone tell me what this statement means,

while(false){
do_something();
}


what it means, and where the false condition come from. moreover, how this statement execute in the program.

thanks in advance.

sokoun.
Hi sokoun,tell me what do you think that this piece of code do?

Savage
Sep 6 '07 #2
vinvik
16
Hi all,

can anyone tell me what this statement means,

while(false){
do_something();
}


what it means, and where the false condition come from. moreover, how this statement execute in the program.

thanks in advance.

sokoun.



hey its just syntax dude..
u have to write condition true/fasle
and the statements to be executed after condn check..

anyway what do u want to do..?
Sep 6 '07 #3
false is a false condition , in C language it means 0 while 1 is true, because c does not include booleans.
suppose you have this code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. main()
  5. {
  6.       int i = 0 ;
  7.  
  8.       while ( 0 )
  9.       { 
  10.             if (i ==2 ) break;
  11.        i ++;
  12.       }
  13.       printf("\n%d\n", i);
  14.       system ("PAUSE");
  15.  
  16. }


It will print 0 ... but if you use ' while ( 1 ) ' it will print 2


Hi all,

can anyone tell me what this statement means,

while(false){
do_something();
}


what it means, and where the false condition come from. moreover, how this statement execute in the program.

thanks in advance.

sokoun.
Sep 6 '07 #4
sokoun
30
Hi sokoun,tell me what do you think that this piece of code do?

Savage
I just wonder about this problem because when i test while(1), i know how it works, but i don't know if while(0) could you tell me?

sokoun.
Sep 6 '07 #5
sokoun
30
false is a false condition , in C language it means 0 while 1 is true, because c does not include booleans.
suppose you have this code:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. main()
  5. {
  6.       int i = 0 ;
  7.  
  8.       while ( 0 )
  9.       { 
  10.             if (i ==2 ) break;
  11.        i ++;
  12.       }
  13.       printf("\n%d\n", i);
  14.       system ("PAUSE");
  15.  
  16. }


It will print 0 ... but if you use ' while ( 1 ) ' it will print 2
thanks for your help
but could you explain me more about this code, because i don't know whether how it works. if while(0) did not affect the program why we use it?

thanks.
Sep 6 '07 #6
Savage
1,764 Expert 1GB
I just wonder about this problem because when i test while(1), i know how it works, but i don't know if while(0) could you tell me?

sokoun.

Let's,for e.g:

i=1

while(i)
{
//do something
//if something change i to 0
}

this loop will run until i becomes 0 or with oder words logical false,same with bool,if it is true it will execute a loop else it will not

Savage
Sep 6 '07 #7
sokoun
30
Let's,for e.g:

i=1

while(i)
{
//do something
//if something change i to 0
}

this loop will run until i becomes 0 or with oder words logical false,same with bool,if it is true it will execute a loop else it will not

Savage
could you tell me when we use the while(0) for, and when this condition execute.

thanks.
Sep 6 '07 #8
Savage
1,764 Expert 1GB
could you tell me when we use the while(0) for, and when this condition execute.

thanks.
while(0) does nothing,it just skips over the loop.It's another way to break,but this time without break keyword.You could do it like:

Expand|Select|Wrap|Line Numbers
  1. while(1)
  2. {
  3.   //do something
  4.   //if something,then break
  5. }
the effect would be the same,if you exclude those experts speaking that break keyword is not a good way to get out of a loop and start jumping on your head until you decide to change just while condition and break from loop non-violantly.



Savage
Sep 6 '07 #9
while(0) does nothing,it just skip over the loop.It's another way to break,but this time without break keyword.You could do it like:

Expand|Select|Wrap|Line Numbers
  1. while(1)
  2. {
  3.   //do something
  4.   //if something,then break
  5. }
the effect would be the same,if you exclude those experts speaking that break keyword is not a good way to get out of a loop and start jumping on your head until you decide to change just while condition and break from loop non-violantly.

Savage
Maybe break it's not a nice way to exit a loop , but if you use while (1) without break you enter an infinite loop. I've used it just to show a concrete example.
Sep 6 '07 #10
Banfa
9,064 Expert Mod 8TB
could you tell me when we use the while(0) for, and when this condition execute.
If you are asking when you would use while(0) with the 0 hardcoded, as you suggest in each of your posts, rather than using a variable as all the experts are suggesting then the answer is never.

while(0) is a completely pointless statement all it does is prevent the code in the while loop from executing and there are better ways of doing that (deleting it, using preprocessor statements etc). If you have a good optomising compiler then the code in the while loop will not even make it into the binary program, if you don't then you will be wasting space in code memory.
Sep 6 '07 #11
Savage
1,764 Expert 1GB
Maybe break it's not a nice way to exit a loop , but if you use while (1) without break you enter an infinite loop. I've used it just to show a concrete example.
That's why you use some variable for while condition.Inside the loop you change the variable to 0 and you are of the loop

Savage
Sep 6 '07 #12
Tha one was just an easy example to show how C language interpretates conditions inside a loop. Some languages use Boolean data type , here it is not necessary. So

0 -> False
1 -> True

Will be the result of your condition.

could you tell me when we use the while(0) for, and when this condition execute.

thanks.
Sep 6 '07 #13
Really ????

Please follow these guidelines when responding to questions.
Provide relevant answers and solutions


That's why you use some variable for while condition.Inside the loop you change the variable to 0 and you are of the loop

Savage
Sep 6 '07 #14
Savage
1,764 Expert 1GB
Really ????

Please follow these guidelines when responding to questions.
Provide relevant answers and solutions
??

I already posted such example and double posting is, to say so, a CRIME.

Please refer to post #7.

Savage
Sep 6 '07 #15

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

33 posts views Thread by Diez B. Roggisch | last post: by
11 posts views Thread by Angus Graham | last post: by
75 posts views Thread by Greg McIntyre | last post: by
52 posts views Thread by Rick | last post: by
40 posts views Thread by nufuhsus | last post: by
12 posts views Thread by desktop | last post: by
5 posts views Thread by maestro | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.