Help understanding continue and for loops

Pfap

Member
Hi, when you use the continue key word doe it automatically add to the value of the iterator?


Code:
repeat(4){
 
 show_debug_message(i);
 if is_undefined(id_sprite[i]){ show_debug_message(i); show_debug_message("continuing"); continue; }
 show_debug_message(i);
if id_sprite[i].sprite_index == sprite_index{
 
 //run the code to add to score if the instance hasn't been tagged yet
 event_user(4);
 
 i += 1;
 
 }
 else{
 
  i += 1;
 
 }
 
}
My issue is that I'm pulling instance id's from a grid and sometimes the value coming out is undefined.

The manual section for the keyword continue showed it being used with this style:

Code:
//my i's are auto capitalized so using j's
for(j = 0; j < 10; j++){


//your code here

}

Which leads to the second part of my question. What is the difference between using the above versus the below:

Code:
for(j = 0; j < 10;){

//your code here
j += 1;

}
Is it just style? I personally prefer the second usage, it seems easier for me to think about when j is changing even though I know it's at the end of the loop. Also, if using the second style do I need to add to j before calling continue?


Ok, sometimes just writing a forum posting can do wonders. I figured out the question about the operation of continue, still curious about the difference in for loops though.


I had to add to j before using continue
Code:
id_sprite[0] = undefined;
id_sprite[1] = undefined;
id_sprite[2] = undefined;
id_sprite[3] = undefined;
i = 0;

repeat(4){
 show_debug_message("i at start of loop");
 show_debug_message(i);
 if is_undefined(id_sprite[i]){ show_debug_message(i); show_debug_message("i at continue of loop"); i += 1; show_debug_message("continuing"); continue; }
 show_debug_message("i after continue");
 show_debug_message(i);
if id_sprite[i].sprite_index == sprite_index{
 
 //run the code to add to score if the instance hasn't been tagged yet
 event_user(4);
 
 i += 1;
 
 }
 else{
 
  i += 1;
 
 }
 
}
 

Simon Gust

Member
In a for loop, the iterator already changes at the end of the current iteration. So you don't have to do it yourself unless you want to change
the iterator by random values or do something crazy.
Continue makes the loop jump immedeatly to the next iteration. That means everything below a continue will not be executed if continue is reached.
Continue does not mean "continue to run", it means "stop here and go the the next iteration".

If you do need iterators, I highly advise you to use for loops instead of repeat loops.

So your code should simply be
Code:
id_sprite[0] = undefined;
id_sprite[1] = undefined;
id_sprite[2] = undefined;
id_sprite[3] = undefined;

for (var i = 0; i < 4; i++)
{
    if (is_undefined(id_sprite[i])) {
        continue; // this id sprite is undefined, skip iteration, code below won't run.
    }
   
    if (id_sprite[i].sprite_index == sprite_index)
    {
        //run the code to add to score if the instance hasn't been tagged yet
        event_user(4);
    }
}
Guessing that only for testing, they are all set to undefined.
 

Pfap

Member
So your code should simply be
Code:
id_sprite[0] = undefined;
id_sprite[1] = undefined;
id_sprite[2] = undefined;
id_sprite[3] = undefined;

for (var i = 0; i < 4; i++)
{
    if (is_undefined(id_sprite[i])) {
        continue; // this id sprite is undefined, skip iteration, code below won't run.
    }
   
    if (id_sprite[i].sprite_index == sprite_index)
    {
        //run the code to add to score if the instance hasn't been tagged yet
        event_user(4);
    }
}
Guessing that only for testing, they are all set to undefined.

Ok yours looks a lot cleaner and it gives me a little insight as to why you can use both

for(j = 0; j < 10;) and for(j = 0; j < 10; j++;)

I'm guessing it's in cases where you want to iterator more than once like j += 2 or something.
 

TheouAegis

Member
for(j=0; j<10; ) j++;

is simply

j=0; while j<10 j++;

A normal for loop can be thought of as

j=0;
if j<10 {
code_block();
j++; }

The code block is a subroutine run separately from the for loop, whereas they are mutually inclusive in a while loop.

Your method is more likely to hang than a normal for loop.


FYI in a for loop as defined by for(A; B; C; ), A and C are technically code blocks as well and B is any boolean expression.
Code:
for(var inst = instance_create(irandom(room_width),irandom(room_height),obj_cat); instance_number(obj_cat)<instance_number(obj_dog); { var n = instance_find(obj_bird, irandom(instance_number(obj_bird)-1); instance_create(n.x + choose(-64,0,64), n.y + choose(-48, 0, 48), obj_cat); })

for(var xx=0, yy=0; yy<room_height-32 || xx<32; {xx+=32; if xx>=room_width {yy+=32; xx=0;}})

for({var i; i=0;} i<10; i++)
 
Last edited:
Top