- Quick Links - |
Visitor No.72282 |
- Random Picture - | ||||||
|
Explains the use of "For" and "While"
The category of these 2 are the same as "if" in cog. Just does a little different from "if". The form looks like this. for(i = X; i <= Y; i = Z) { Do some stuff in here... } It looks a bit complicated but here's what it is all about. The first X is where a new variable "i" will start as a value. The second Y tells up/down to what value the "i" can go to. The last Z will tell by how much does the value "i" changes each time. It can be "i + 1" to increase 1 by 1. The "for" will stop after it goes over the limit. Example : for(i = 1; i <= 3; i = i + 1) //(X; Y; Z) for explanation { PrintInt(i); } The "i" will start from 1 and reads inside the bracket, then "i" will change by the defined value in Z. So it will become 2 and it will read inside again. And moves onto another i + 1, which will be 3 and reads inside with "i" value. This will go on until it goes over the definition, which this time is less or equal to 3. So what it totally does is it prints out "1" "2" "3" in a second. And it stops. This is useful for repetitive commands to make it simpler. For example, if you want to disable all weapons, do this. for(i = 1; i <= 10; i = i + 1) //(start value, the range of the value, how much change every time) { SetInvAvailable(player, i, 0); } Conclusion : "for" will read inside again and again until it reaches to its limit.
Here, I'll start explaining about "while". What it does is basically similar to "for". That this time it can last for undefined time. Format - while(X); If it has "1" as a parenthesis, it will repeat this real fast forever. This will print "arg" until you close the level. (Or it may freeze) while(1) { Print("arg"); } But if "0" is in there instead, it never does anything inside the brackets. Also there is another way of usage with this. Value = rand() * 10; //Creates value 0 to 10 randomly while(Value > 0) { Value = Value - 1; Print("cool"); } With this way the unknown value "Value" will be created, and it will print "cool" while the value is positive, subtracting 1 every time it reads inside. So, it is unsure in this case how many times it reads inside but it does until the statement gets false which means when "Value" gets to negative numbers in this case. Conclusion : "while" will read inside its bracket again and again while the statement in its parenthesis is true. |
This site is developed using PHP under Apache server Certain parts of this site require Cookie support. This site is best viewed with
Internet Explorer 5.5, Netscape 6.0, Opera 6.0 or Mozilla 1.0 and above |