lets say you want to loop the text:
"The red brown fox jumps over the lazy dog",
you want to know at what position THE FIRST 'a' is. so let's say it's stored in:
you loop the whole string till you find an 'a' so:
pawn Код:
// syntax for 'for' loop:
// for ( <executed code once, for example declare an variable, more code can be done with comma's ex: new a,b,c,a=b=c=0>; <statement which is true, if false loop exits>; <code executed every loop call, for example: a= a+1>)
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{
}
that will make the loop, now we need to execute something in it each call:
pawn Код:
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{
if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
{
}
}
ok now we check the availability for the character 'a'.
but what happen if we return something in the loop?:
pawn Код:
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{
if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
{
}
return 1;// this will preven the whole loop, whatever you return, the loop will be executed only once or not at all (because the statement matters too)
}
however:
pawn Код:
stock function()
{
for(new counter_variable = 0; counter_variable /*is less*/ < /*than*/ 64 /* our string is 64 chars long */; ++counter_variable)
{
if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
{
return counter_variable; // loop will end, everything below will not be executed, so -1 won't be returned, but the position of the first 'a' will get returned
}
}
return -1//not found
}
just look out your loop will not be unlimited:
pawn Код:
for(new variable = 1; variable > 0; ++variable)
{
//will teoretically never end, however if 2^31 is reached, it will be ended because of an integer overflow
}
while is the same but is is just
pawn Код:
while(statement)//if statement is false (0) while will stop looping
{//do
//this
}
like:
pawn Код:
stock function()
{
new counter_variable = 64;//set to 64, however now it will return the position of the LAST 'a'
while(--counter_variable)
{
if(string[counter_variable] /* get the string at position |counter_variable| */ == 'a' /* matches "a" , CASE SENSETIVE */)
{
return counter_variable; // loop will end, everything below will not be executed, so -1 won't be returned, but the position of the first 'a' will get returned
}
}
return -1//not found
}
and do.. well if u see any code used , then just look back a this and i will understand it.. pure 100% logic.