473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 15323
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,065 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

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

Similar topics

33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
11
by: Angus Graham | last post by:
Hi: I've recently come across a number of programmers who use the following type of do/while(false) structure quite often: do { if (! CatchFish(pFish)) { break; } if (! CleanFish(pFish)) {
75
by: Greg McIntyre | last post by:
I have a Python snippet: f = open("blah.txt", "r") while True: c = f.read(1) if c == '': break # EOF # ... work on c Is some way to make this code more compact and simple? It's a bit...
52
by: Rick | last post by:
Hi, For portability, can an issue arise if we use while(1) for an infinite loop in C? If so, should we then use for(;;)? Thanks, Rick
0
by: tjonsek | last post by:
I am working with directories in PHP for the first time. I have code that I've changed multiple times to try different things. I would think this is pretty standard fare so I'm not sure why I can't...
40
by: nufuhsus | last post by:
Hello all, First let me appologise if this has been answered but I could not find an acurate answer to this interesting problem. If the following is true: C:\Python25\rg.py>python Python...
3
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
12
by: desktop | last post by:
Are there any performance issues between: bool run = true; while(run) { if (bob1 &&){ //dothing break; }
5
by: maestro | last post by:
why does this work? "while p" = "while p != 0" ? 1 is True and 0 is false in python but other numbers have no boolean value so why doesnt it abort. 16 print p p -= 1
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.