Open main menu

CDOT Wiki β

Changes

Team !YOU - OOP344

447 bytes added, 13:36, 25 January 2010
m
(Discussion: Use of iterating variables on for loops)
Option 1: Declare the variables outside the loop; initialize them inside the loop; keep their exit values for future use.
int i; // counter for (i = 0; i<5; i++) printf("."); // Prints ..... printf("%d",i); // Prints 5
Option 2: Declare and initialize variables inside the loop; lose the variable at the end of the loop scope;
|-
| fmDeOliveira || I definitely prefer option 2. It is much easier to keep recycling the loop variables without having to worry if they already exist or not. In case we need the value of the loop variable after the loop is done, we should just copy it to another variable before the end of the loop.
|-
| ammisko || I like option 1 better (I changed it a bit). I think it looks cleaner and is much easier to read for someone who doesn't know what the code does (especially with comments). Also what if we need to use a loop variable from outside the for loop? Or if we need to use the first part of the for-loop for other code? Option 1 could accommodate that better I think.
|-
|}
1
edit