while statement
#1

Hey people. I just wanna ask a doubt regarding "while" statement.

If we're using " if " statement then we can use "else" but when using "while" else cant be added along with it, pop up an error when using it. What do we supposed to add instead of "else" for "while" ?

Eg code:

pawn Код:
while(rows != 0)
    {
        new
            username[24];

        rows --;
        cache_get_field_content(ID, "user",username, mysql, 24);
        ID ++;
        format(output, sizeof(output), "%s"ccwhite"%d. %s\n", output, ID, username);
        ShowPlayerDialog(playerid, 212, DIALOG_STYLE_MSGBOX, "Level 1 Admins", output, "OK", "Cancel");
    }
    else { //  <--- it gives an error here
    //bla bla
    }
Reply
#2

Nothing.

Just the next instruction after while{ /* */ }...

Because the loop stops when the while's condition ends.

Btw, if something happenned during the loop and you get satisfied, just use break; to stop the loop.
Reply
#3

Why will it give an error in which part are you expecting a error
This is the correct way of doing it
pawn Код:
new username[24], temp[35];
for(new ID = 0; ID < rows; ID++)
{
    cache_get_field_content(ID, "user",username, mysql, 24);
    format(temp, sizeof(temp), "-"ccwhite"%d. %s",  ID, username);
    strcat(output, temp);
    strcat(output, "\n");
}
ShowPlayerDialog(...);//this should be outside loop not inside
Reply
#4

This checks whether it does have any rows and returns the output if any
pawn Код:
while(rows != 0)
But i want to use an else statement there to show a message that if there are no rows exists. When i add the else as the code above it shows an error.
Reply
#5

There is no need for else
It would be like this
pawn Код:
if(!rows)return SendClientMessage(playerid, -1, "{ff0000}>>> {ffffff}No rows!");
new username[24], temp[35];
for(new ID = 0; ID < rows; ID++)
{
    cache_get_field_content(ID, "user",username, mysql, 24);
    format(temp, sizeof(temp), "-"ccwhite"%d. %s",  ID, username);
    strcat(output, temp);
    strcat(output, "\n");
}
ShowPlayerDialog(...);
Reply
#6

The code inside the if-statement is only executed once if the condition is true and you can specify other code to be executed using "else" when the condition is false.

The code inside the while loop is executed as long as the condition it true.
You cannot specify any code to be executed in case the condition is false.

The difference is that the if-statement is a one-time-only condition check, where the while-loop re-checks the condition as long as it's true.
Once the while-loop sees the condition is false (even if it would be the first time it would run), the loop is exited and the code below the loop will be executed.

There is also a difference between the while-loop and the for-loop:

A for-loop is executed as many times as you want it to run (say 2000 times when looping through all vehicles).

A while-loop is usually used when you don't know how many times it will run, like opening a file first, then looping through every line in the file, reading it and printing the line. The while-loop is preferred in such a case, as you don't know how many lines the file actually holds. You can read the size of the file (the amount of bytes), but not the amount of lines in it.
Using a while loop, you can run your code as long as the end of the file hasn't been reached yet, and you never know when that will be.

A while loop can also lead to infinite loops because of this.
pawn Код:
new i = 0;
while (i < 1000)
{
}
A while loop like this would cause your server to crash, as "i" will ALWAYS be lower than 1000, because the value of "i" isn't changed anywhere and will always stay at 0.
pawn Код:
new i = 0;
while (i < 1000)
{
    i = i + 1;
}
This loop will only run 1000 times, as "i" will hold the value 1000 at some point.

You can't have that with for-loops, as most for-loops are used like this:
pawn Код:
for (new i; i < 1000; i++)
{
}
In that line, you already state that i must be increased by 1 every iteration of the loop, so it will only loop 1000 times.

You also have do-while loops.

This kind of loops run at least once, as the condition isn't checked when the loop is entered, but it's checked when it ends.
pawn Код:
do
{
    i = i + 1;
}
while (i < 1000)
This code will run first, before the actual condition is checked.
If the condition is still true after exiting the loop, the loop runs again and again, until the condition is false.
Reply
#7

Okay thanks guys (cant rep unless it passes 24 hours)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)