SOLVED While vs Do?

Gudiwa

Member
Cant tell when to use "while" and "do-until". Also why should i use one of these two when i could just use "if"
 

FrostyCat

Member
Like your other question, this is another one that a quick glance at the Manual would have answered. Learn to use its Index tab to look up unfamiliar terms.
  • while is for repeating a block of code if the condition is true, and do so until the condition becomes false. No other outside code runs as long as the loop is running.
  • do-until is for repeating a block of code at least once, and do so until the condition becomes true. No other outside code runs as long as the loop is running.
  • if is for running a block of code just once if the condition is true, and skip past it if the condition is false.
 

woods

Member
while.. this will repeatedly check for a non-collision until one is found, and move there.. and then run the rest of the code

GML:
{
while (!place_free(x, y))
   {
   x = random(room_width);
   y = random(room_height);
   }
}

if.. this will check for a non-collision once and move there if its a free space, ..if not it wont move there... and then run the rest of the code

Code:
{
if (!place_free(x, y))
   {
   x = random(room_width);
   y = random(room_height);
   }
}

right?
 
Top