10.10.2011, 16:59
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:
that will make the loop, now we need to execute something in it each call:
ok now we check the availability for the character 'a'.
but what happen if we return something in the loop?:
however:
just look out your loop will not be unlimited:
while is the same but is is just
like:
and do.. well if u see any code used , then just look back a this and i will understand it.. pure 100% logic.
"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:
pawn Код:
new string[64];
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)
{
}
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 */)
{
}
}
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)
}
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
}
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
}
pawn Код:
while(statement)//if statement is false (0) while will stop looping
{//do
//this
}
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
}