21.01.2015, 14:43
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.
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.
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:
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.
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.
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)
{
}
pawn Код:
new i = 0;
while (i < 1000)
{
i = i + 1;
}
You can't have that with for-loops, as most for-loops are used like this:
pawn Код:
for (new i; i < 1000; i++)
{
}
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)
If the condition is still true after exiting the loop, the loop runs again and again, until the condition is false.

