SA-MP Forums Archive
Explaination of Continue; and Break; - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Explaination of Continue; and Break; (/showthread.php?tid=132026)



Explaination of Continue; and Break; - pagie1111 - 06.03.2010

I need an in depth explanation of continue and break.

like in this:

[/pawn]
stock LoadStaticVehiclesFromFile(const filename[])
{
new File:file_ptr;
new line[256];
new var_from_line[64];
new vehicletype;
new Float:SpawnX;
new Float:SpawnY;
new Float:SpawnZ;
new Float:SpawnRot;
new Color1, Color2;
new index;
new vehicles_loaded;

file_ptr = fopen(filename,filemode:io_read);
if(!file_ptr) return 0;

vehicles_loaded = 0;

while(fread(file_ptr,line,256) > 0)
{
index = 0;

// Read type
index = token_by_delim(line,var_from_line,',',index);
if(index == (-1)) continue;
vehicletype = strval(var_from_line);
if(vehicletype < 400 || vehicletype > 611) continue;

// Read X, Y, Z, Rotation
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
SpawnX = floatstr(var_from_line);

index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
SpawnY = floatstr(var_from_line);

index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
SpawnZ = floatstr(var_from_line);

index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
SpawnRot = floatstr(var_from_line);

// Read Color1, Color2
index = token_by_delim(line,var_from_line,',',index+1);
if(index == (-1)) continue;
Color1 = strval(var_from_line);

index = token_by_delim(line,var_from_line,';',index+1);
Color2 = strval(var_from_line);

//printf("%d,%d,%f,%f,%f,%f,%d,%d",total_vehicles_fr om_files+vehicles_loaded+1,vehicletype,SpawnX,Spaw nY,SpawnZ,SpawnRot,Color1,Color2);

AddStaticVehicleEx(vehicletype,SpawnX,SpawnY,Spawn Z,SpawnRot,Color1,Color2,(30*60)); // respawn 30 minutes
vehicles_loaded++;
}

fclose(file_ptr);
printf("Loaded %d vehicles from: %s",vehicles_loaded,filename);
return vehicles_loaded;
}[/pawn]
(Source = SA:MP include/gl_common)

Thank you.


Re: Explaination of Continue; and Break; - dice7 - 06.03.2010

In loops, when your code hits continue;, your code will stop and start again at the start of the loop, skipping everything else after the continue;.

break is used to stop a loop. After your code hits it, the code below it wont be executed and it will leave the loop immediately

pawn Код:
new var = -1;
while(1) //unlimited loop
{
    var++;
    if(var == 5) //if var is 5, then the printf and the "if var =10" check will be skipped
    {
        continue;
    }
    printf("%d", var);
   
    if(var == 10) //if var is 10, the loop will break
    {
        break;
    }
}
The above example will print out 0, 1, 2, 3, 4, 6, 7, 8 and 9
If there would be no break, the loop wouldn't stop and it would print out numbers till eternity


Re: Explaination of Continue; and Break; - pagie1111 - 07.03.2010

Okay thank you very much. You've helped alot.