Thread: Infinite While Loops Question

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    53

    Infinite While Loops Question

    I am learning C++ and I was under the impression if you were to do an infinite while loop to do

    Code:
    int counter=0
    
    while(1)
    {
    counter++;
    if(counter>10)
    {
    break;
    }
    }
    However, my book says to use while(true)...I did learn the while(1) method in C programming, is "true" a new feature since it is apart of c++? Which should I use to fit among the norm?

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    true is the same as 1, and false is 0,

    but for infinnite loops id jsut do

    Code:
    for( ; ; )
    {
         //do stuff here
         if(/*something*/)
         {
                break;
         }
    }

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> true is the same as 1, and false is 0,

    You could see it like that, but I'd advise you not to.

    Don't think of true and false as being 1 and 0. Try to think of true and false as absolute values.

    In C, a negative number would also equate to true in a loop condition, eg.
    Code:
    while (-3)
    {
        //...
    }
    So true could be a negative or a positive, but not zero. Java is strict about this, in that a loop condition must equate to either true or false.

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    yea i was thinking in terms of just 1 and 0's
    but 0 is always false, so anything other then 0 would be true.
    besides i didnt say true was only 1, i just said that true is the
    same as 1. just to help him understand, if id of went on
    with a rant about all the things it could be he could of gotten
    lost, and it strayed from his question.
    Last edited by ILoveVectors; 08-13-2005 at 07:23 PM.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    No biggy, I was just trying to explain the reason for having the value true and false in a language.

    A strongly typed language would enforce a boolean value as a loop condition rather than allowing any value.

    I know you didn't mean that true is only 1, I just did want 'hern' to think of it like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on GCD and for loops
    By dals2002 in forum C Programming
    Replies: 9
    Last Post: 03-15-2008, 01:59 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. Quick question on nested for loops
    By GCNDoug in forum C++ Programming
    Replies: 12
    Last Post: 10-17-2007, 08:54 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM