Study guide: Non-object oriented language basics, Part 3
Brush up on Java terms, learn tips and cautions, take the first Java 101 quiz, and read Jeff's answers to student questions
By Jeff Friesen, JavaWorld.com, 02/02/01
Glossary of terms
- block
- A compound statement.
- break statement
- A statement that terminates the current loop iteration and causes execution to break out of a loop.
- chained if-else statement
- A statement that consists of several if-else statements chained together. Each if-else statement evaluates its Boolean expression
until either one of those expressions evaluates to true or the final
else is reached.
- continue statement
- A statement that terminates the current loop iteration and persists the loop with the next iteration.
- dangling else problem
- The common mistake of not properly matching
ifs with elses.
- do-while loop statement
- A statement that executes a statement and then evaluates a Boolean expression. If that expression is true, the statement re-executes.
Those execute statement/evaluate expression activities continue until the expression is false.
- empty statement
- A statement consisting of only a semicolon.
- for loop statement
- A statement that repeatedly executes another statement under the control of one or more loop control variables.
- if-else statement
- A statement that evaluates a Boolean expression and allows one statement to execute if the expression evaluates to true and
another statement to execute if the expression evaluates to false.
- if statement
- A statement that evaluates a Boolean expression and executes another statement if that expression evaluates to true.
- loop control variable
- A variable that tracks the current loop iteration.
- return statement
- A statement that returns values from methods, just as it returns values from C/C++ functions.
- scope
- Visibility.
- statements
- Source code constructs that declare variables, assign expression results to variables, repeatedly execute other statements
in loops, control loops, make decisions, and so on.
- switch statement
- A more compact but less flexible version of the chained if-else statement.
- while loop statement
- A statement that evaluates a Boolean expression and, if this expression is true, executes another statement. Those evaluate
expression/execute statement activities continue until the expression is false.
Tips and cautions
These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error
messages.
Tips
- Use care when formatting
ifs and elses. Proper formatting helps you see which else matches which if. The result: you will most likely avoid the dangling-else problem.
- Use an empty statement -- a single semicolon -- with a looping statement (such as a while loop statement) when you are more
concerned with the execution of the loop statement's expression. For example,
while (System.in.read () != '\n'); is more concerned with emptying the buffer that associates with the standard input device than the continual execution of
another statement.
Cautions
- Because a local variable's scope restricts the block that declares that variable and all nested blocks, code outside those
blocks cannot access the variable. Any attempt to perform such access results in a compiler error.
- An attempt to declare a local variable with the same name and in the same block as another local variable or parameter results
in a compiler error.
- Forgetting that an
else matches up with the most recent if often leads to the dangling else problem. That problem often results in unexpected execution behavior.
Homework
Welcome to the first of several Java 101 quizzes. I've designed each quiz to help you think about concepts introduced in the Java 101 course and to help prepare you for certification. This first quiz covers material from the previous four Java 101 articles (including the article that accompanies this study guide). I will post answers in next month's study guide. However,
you can answer the questions presented in this quiz by reading previous articles. I suggest you sit back and relax, as you
have one month to complete the quiz. You may begin!