Loop Question
#1

If i have this Loop:

pawn Код:
for (new a = 0; a < 3; a++)
{
    if (a == 1) continue;
    printf("a = %d", a);
}
a is equal to 1 and it will be skiped.

That code can be equal to:

pawn Код:
for (new a = 0; a < 3; a++)
{
    if (a == 1) return 0;
    printf("a = %d", a);
}
Or i'm a bit confused?
Reply
#2

No, continue just skips 1 iteration of the loop. Return stops the loop and aborts the function that it is in completely. Then you also have break, which breaks out of the loop prematurely and then goes on with the first code after the loop.
Reply
#3

Well, if you use return, it's the same as using break, it will break the loop.

pawn Код:
for (new a = 0; a < 3; a++)
{
    if (a == 1) return 0; // Will be called first
    printf("a = %d", a); // Won't be called
}
Reply
#4

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
Well, if you use return, it's the same as using break, it will break the loop.

pawn Код:
for (new a = 0; a < 3; a++)
{
    if (a == 1) return 0; // Will be called first
    printf("a = %d", a); // Won't be called
}
Vince said break is same as return 0 but also goes to first line of the loop.
Reply
#5

Quote:
Originally Posted by Anthony_prince
Посмотреть сообщение
Vince said break is same as return 0 but also goes to first line of the loop.
Nah, he said that it goes to the first line after the loop
pawn Код:
for (new a = 0; a < 3; a++)
{
    if (a == 1) break; //called
    printf("a = %d", a);
}
//Will be called next

for (new a = 0; a < 3; a++)
{
    if (a == 1) return 0; //called
    printf("a = %d", a);
}
//Won't be called at all as the callback / function has been given a return value.
Reply
#6

Oh thanks for helping.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)