while(false) { }

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sokoun
    New Member
    • Sep 2007
    • 30

    #1

    while(false) { }

    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.
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by sokoun
    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

    Comment

    • vinvik
      New Member
      • Aug 2007
      • 16

      #3
      Originally posted by sokoun
      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..?

      Comment

      • diSangro
        New Member
        • Jan 2007
        • 69

        #4
        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:

        Code:
        #include <stdio.h>
        #include <stdlib.h>
        
        main()
        {
              int i = 0 ;
              
              while ( 0 )
              { 
                    if (i ==2 ) break;
               i ++;
              }
              printf("\n%d\n", i);
              system ("PAUSE");
        
        }


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


        Originally posted by sokoun
        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.
        Last edited by diSangro; Sep 6 '07, 08:45 AM. Reason: wrong format

        Comment

        • sokoun
          New Member
          • Sep 2007
          • 30

          #5
          Originally posted by Savage
          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.

          Comment

          • sokoun
            New Member
            • Sep 2007
            • 30

            #6
            Originally posted by diSangro
            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:

            Code:
            #include <stdio.h>
            #include <stdlib.h>
            
            main()
            {
                  int i = 0 ;
                  
                  while ( 0 )
                  { 
                        if (i ==2 ) break;
                   i ++;
                  }
                  printf("\n%d\n", i);
                  system ("PAUSE");
            
            }


            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.

            Comment

            • Savage
              Recognized Expert Top Contributor
              • Feb 2007
              • 1759

              #7
              Originally posted by sokoun
              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

              Comment

              • sokoun
                New Member
                • Sep 2007
                • 30

                #8
                Originally posted by Savage
                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.

                Comment

                • Savage
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1759

                  #9
                  Originally posted by sokoun
                  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:

                  Code:
                  while(1)
                  {
                    //do something
                    //if something,then break
                  }
                  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

                  Comment

                  • diSangro
                    New Member
                    • Jan 2007
                    • 69

                    #10
                    Originally posted by Savage
                    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:

                    Code:
                    while(1)
                    {
                      //do something
                      //if something,then break
                    }
                    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.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by sokoun
                      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.

                      Comment

                      • Savage
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1759

                        #12
                        Originally posted by diSangro
                        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.Insid e the loop you change the variable to 0 and you are of the loop

                        Savage

                        Comment

                        • diSangro
                          New Member
                          • Jan 2007
                          • 69

                          #13
                          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.

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

                          thanks.

                          Comment

                          • diSangro
                            New Member
                            • Jan 2007
                            • 69

                            #14
                            Really ????

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


                            Originally posted by Savage
                            That's why you use some variable for while condition.Insid e the loop you change the variable to 0 and you are of the loop

                            Savage

                            Comment

                            • Savage
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1759

                              #15
                              Originally posted by diSangro
                              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

                              Comment

                              Working...