Infinite While Loop Queation (var != var)

Discussion in 'Programming' started by techbongo, Jan 16, 2010.

  1. #1
    I've got a typical question. I need your assistance.

    while(var != var)
    {
    System.out.println("I'm inside the loop");
    }
    Code (markup):

    How to declare the variable var (of any data type), so the the while loop runs infinitely? This is possible, I know. But don't know how. Please help.

    I've used Java here, you can assist me in any other language.
     
    techbongo, Jan 16, 2010 IP
  2. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here's your solution in PHP...
    while ($var==$var) {
      echo 'I\'m inside the loop';
    }
    PHP:
    You were using != which is "not equal to" so if you're checking the same variable against itself the content of echo will never display.
     
    Yesideez, Jan 16, 2010 IP
  3. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can run inf loop like this
    
    while(1)
    {
    ...
    }
    
    Code (markup):
    
    for(;;)
    {
    ...
    }
    
    Code (markup):
     
    Kaimi, Jan 16, 2010 IP
  4. sweethear409

    sweethear409 Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    int i=0;
    while(i>0)
    {
    system.out.println("hello");
    i++;
    }
     
    sweethear409, Jan 17, 2010 IP
  5. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    These are the two correct methods to run infinite loops.
     
    NeoCambell, Jan 17, 2010 IP
  6. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #6
    Thanks you all for replying. Fortunately, I searched a lot yesterday and found the answer.
    My question was correct. Just while(var != var) can create an infinite loop. I've got the solution and I'll like to share it with you.

    It's basically a floating point value, in Java it's NaN (Not a Number).

    float var;
    var= (0f / 0); //zero-f by zero
    while(var != var)
    System.out.println("Inside loop");


    This code creates an infinite loop. Because a variable with value of NaN is not equal to itself, according to convention and practice.
     
    techbongo, Jan 18, 2010 IP