Q&A

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 coding 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 Hour 11, "Describing What Your Object Is Like."

Q2:

The term initialization has been used in several places. What does it mean?

A2:

It means to give something an initial value and set it up. When you create a variable and assign a starting value to it, you are initializing the variable.

Q3:

If a loop never ends, how does the program stop running?

A3:

Usually in a program where a loop does not end, something else in the program is set up to stop execution in some way. For example, a loop could continue indefinitely while the program waits for the user to click a button labeled Quit.

A3:

One bug that crops up often as you work on programs is an infinite loop, a loop that never stops because of a coding mistake. If one of the Java programs you run from the command line is stuck in an infinite loop, press Ctrl+C.

Q4:

Why do airplane windows have tiny little scratches on them?

A4:

The small hairline scratches and spider-web marks are caused by cleaning or chemicals in the atmosphere such as volcanic ash. The windows, made of plastic rather than glass, get scratched often. There's nothing unsafe about the scratches on the windows, which are periodically removed and polished.

      
Comments