| Blockland Forums > Modification Help |
| How do for() statements work? |
| (1/1) |
| Wordy:
How do for statements wori? And what can you do with them? |
| CityRPG:
--- Quote from: Wordy on March 01, 2012, 02:43:20 AM ---How do for statements wori? And what can you do with them? --- End quote --- Like this. --- Code: ---for($a = 0; $a < 100; $a++) { echo($a); } --- End code --- And, a lot. The syntax for a for loop is this: --- Code: ---for( On Loop Start; Keep Looping If; In Between Loops) --- End code --- Usually, for loops are utilized in scanning through a group/list/array. --- Code: ---for(%a = 0; %a < ClientGroup.getCount(); %a++) { %client = ClientGroup.getObject(%a); something_to_do_for_this_client(%client); } --- End code --- Loops can go inside loops. This goes through each brick in the game by going through each brick group. --- Code: ---for(%a = 0; %a < MainBrickGroup.getCount(); %a++) { %brickGroup = MainBrickGroup.getObject(%a); for(%b = 0; %b < %brickGroup.getCount(); %b++) { %brick = %brickGroup.getObject(%a); do_something_for_a_brick(%brick); } } --- End code --- What are you trying to accomplish? |
| Chrono:
It's also worth noting that the In Between Loops happens at the end of each loop, then the Keep Looping If is checked before starting the next one. This means that in his first example, after the loop, $a will be 100 but wont echo 100 during the loop. It will echo between 0 and 99. |
| DontCare4Free:
A for loop like this: --- Code: ---for (startStmt; cond; postStmt) { block } --- End code --- Is syntactical sugar for: --- Code: ---startStmt; while (cond) { block postStmt; } --- End code --- |
| Wordy:
--- Quote from: CityRPG on March 01, 2012, 03:07:21 AM ---What are you trying to accomplish? --- End quote --- I am not trying to accomplish anything. Although I asked so I could experiment with the for() statement. Thank you. |
| Navigation |
| Message Index |