September 2005
Beginner
576 pages
13h 6m
English
| Q1: | Should the counter variable used in a for loop be created inside the for statement or before the loop begins? |
| A1: | The only time the counter should be created outside of the for loop, or any other loop for that matter, is when it needs to be used in another part of the program. A variable that is created in a loop or other kind of block statement only exists inside that block. You can't use the value in any other part of the program. This is good programming practice because it makes it harder to misuse variables—you can't set their value in one part of the program and use them somewhere else incorrectly. The concept of a variable existing in one part of a program and not existing anywhere else is called scope, and it's covered fully during ... |
Read now
Unlock full access