Usage of loops
#4

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:
pawn Код:
new string[64];
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.
Reply


Messages In This Thread
Usage of loops - by suhrab_mujeeb - 10.10.2011, 16:37
Re: Usage of loops - by Ash. - 10.10.2011, 16:42
Re: Usage of loops - by hittt - 10.10.2011, 16:56
Re: Usage of loops - by Gamer_Z - 10.10.2011, 16:59
Re: Usage of loops - by suhrab_mujeeb - 10.10.2011, 17:24
Re: Usage of loops - by hittt - 10.10.2011, 18:06
Re: Usage of loops - by suhrab_mujeeb - 11.10.2011, 13:22

Forum Jump:


Users browsing this thread: 1 Guest(s)