how many times the body of loop will be executed?

Discussion in 'Programming' started by masterwebby, Mar 22, 2010.

  1. #1
    Hi friends,

    I have to answer this question. I think the loop will get executed infinite number of times. Am I right? or is there an trick in this question?

    Q) how many times the body of loop will be executed?

    X= 5;
    y=50;
    while (X<=y)
    {
    X = y /X
    ………….
    }
     
    masterwebby, Mar 22, 2010 IP
  2. fex

    fex Peon

    Messages:
    89
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The loop will be executed an infinite number of times indeed.

    The value of x alters between 5 and 10 each time.
     
    fex, Mar 23, 2010 IP
  3. Amator

    Amator Well-Known Member

    Messages:
    1,424
    Likes Received:
    55
    Best Answers:
    0
    Trophy Points:
    165
    #3
    you are right
     
    Amator, Mar 23, 2010 IP
  4. EgyptGuy

    EgyptGuy Peon

    Messages:
    167
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Infinite loop is right answer
     
    EgyptGuy, Mar 23, 2010 IP
  5. kidsalsa

    kidsalsa Greenhorn

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #5
    That's an interesting loop.
     
    kidsalsa, Mar 25, 2010 IP
  6. NeoCambell

    NeoCambell Peon

    Messages:
    456
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    x is less than y at start and x is event getting smaller with the division.
    So the condition in "while" will be maintained true for any number of x = x/y executions.
    Since there isn't any operation on y, this loop will run indefinitely.

    Just added some ideas to find the answer to such questions.
     
    NeoCambell, Mar 26, 2010 IP