Java Reference
In-Depth Information
Chapter 2
Primitive Data and
Definite Loops
Introduction
2.1 Basic Data Concepts
Primitive Types
Now that you know something about the basic structure of Java pro-
grams, you are ready to learn how to solve more complex problems. For
the time being we will still concentrate on programs that produce output,
but we will begin to explore some of the aspects of programming that
require problem-solving skills.
Expressions
Literals
Arithmetic Operators
Precedence
Mixing Types and Casting
2.2 Variables
Assignment/Declaration
Variations
The first half of this chapter fills in two important areas. First, it examines
expressions, which are used to perform simple computations in Java, par-
ticularly those involving numeric data. Second, it discusses program ele-
ments called variables that can change in value as the program executes.
String Concatenation
Increment/Decrement Operators
Variables and Mixing Types
2.3 The for Loop
The second half of the chapter introduces your first control structure: the
for loop.You use this structure to repeat actions in a program. This is
useful whenever you find a pattern in a task such as the creation of a
complex figure, because you can use a for loop to repeat the action to
create that particular pattern.The challenge is finding each pattern and fig-
uring out what repeated actions will reproduce it.
Tracing for Loops
for Loop Patterns
Nested for Loops
2.4 Managing Complexity
Scope
Pseudocode
Class Constants
The for loop is a flexible control structure that can be used for many
tasks. In this chapter we use it for definite loops, where you know exactly
how many times you want to perform a particular task. In Chapter 5 we
will discuss how to write indefinite loops, where you don't know in
advance how many times to perform a task.
2.5 Case Study: Hourglass
Figure
Problem Decomposition
and Pseudocode
Initial Structured Version
Adding a Class Constant
Further Variations
61
 
Search WWH ::




Custom Search