-6

I want to know that why this is an infinite loop.

The script is as follows:

   x=1;
   while x<5;
     x=2;
     x=x+2;
     if x==4;
       x=x-1;
     end 
   end
Deendayal Garg
  • 5,030
  • 2
  • 19
  • 33

1 Answers1

0

Step through this to see why x will always be less than 5:

x=1;
while x<5;
  x=2; // You set x to 2 at the beginning of every single iteration
  x=x+2; // At this point, x == 4
  if x==4; // The "if" is unnecessary - of COURSE x == 4 - how could it possibly be anything else?
    x=x-1; // x now contains 3
  end
end

At the beginning of every iteration, then, x == 2. At the end of the iteration, x == 3.