why while(10) excuate body infinity times ???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • waqas qayum
    New Member
    • Apr 2014
    • 1

    why while(10) excuate body infinity times ???

    what does 10 means in while(10) and why it excuate infinity times like while (1) iz 10 also means true same like 1 ?????
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    while(10) is the same as while(1) or any other symbolic constant.

    It means that the loop runs forever.

    You usually see this in a menu driver:

    Code:
    int choice;
    while(1)
    {
    
      choice = getChoice();
      switch (choice)
      {
         case 1:
           ..this is menu choice 1...
           break;
         case 2: 
           .. this is menu choice 2..
           break;
         default:
            ...this is a bad choice..
         }
    }
      }
    The program will stay in this loop for all choices. Hopefully, of choice is to exit the program or you are going to be here a looonnnggg time.

    Comment

    • techboy
      New Member
      • Apr 2014
      • 28

      #3
      yes in C programming loop has two possibilities
      1)true -i.e. any non-zero values are considered as true
      2)false -i.e. only value zero is used to end the loop
      Note:Its nice practice to use while(10)
      but you must take care that you explicitly mention a break statement to avoid infinte loops

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The break statement I used was for the switch inside the loop. The loop is intended to be infinite so process always returns back to the menu.

        There is nothing wrong in using infinite loops.

        Comment

        • techboy
          New Member
          • Apr 2014
          • 28

          #5
          could you please elaborate why it is not wrong?

          Comment

          • yannifan01
            New Member
            • Jul 2008
            • 3

            #6
            It is not wrong if you want to program to run always.

            Comment

            • techboy
              New Member
              • Apr 2014
              • 28

              #7
              how is it sounds run always.No one wants the program to be forever it always has to some day terminate to give some results!

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Did you ever notice that Windows always runs?

                Comment

                • techboy
                  New Member
                  • Apr 2014
                  • 28

                  #9
                  No it would be honour if you can explain me ?

                  Comment

                  Working...