Java Reference
In-Depth Information
4.1 Introduction
4.2 Algorithms
4.3 Pseudocode
4.4 Control Structures
4.5 if Single-Selection Statement
4.6 if else Double-Selection
Statement
4.7 Student Class: Nested if else
Statements
4.8 while Repetition Statement
4.9 Formulating Algorithms: Counter-
Controlled Repetition
4.10 Formulating Algorithms: Sentinel-
Controlled Repetition
4.11 Formulating Algorithms: Nested
Control Statements
4.12 Compound Assignment Operators
4.13 Increment and Decrement Operators
4.14 Primitive Types
4.15 (Optional) GUI and Graphics Case
Study: Creating Simple Drawings
4.16 Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference
4.1 Introduction
Before writing a program to solve a problem, you should have a thorough understanding
of the problem and a carefully planned approach to solving it. When writing a program,
you also should understand the available building blocks and employ proven program-
construction techniques. In this chapter and the next, we discuss these issues in presenting
the theory and principles of structured programming. The concepts presented here are
crucial in building classes and manipulating objects. We discuss Java's if statement in ad-
ditional detail, and introduce the if else and while statements—all of these building
blocks allow you to specify the logic required for methods to perform their tasks. We also
introduce the compound assignment operator and the increment and decrement opera-
tors. Finally, we consider the portability of Java's primitive types.
4.2 Algorithms
Any computing problem can be solved by executing a series of actions in a specific order.
A procedure for solving a problem in terms of
1. the actions to execute and
2. the order in which these actions execute
is called an algorithm . The following example demonstrates that correctly specifying the
order in which the actions execute is important.
Consider the “rise-and-shine algorithm” followed by one executive for getting out of
bed and going to work: (1) Get out of bed; (2) take off pajamas; (3) take a shower; (4) get
dressed; (5) eat breakfast; (6) carpool to work. This routine gets the executive to work well
prepared to make critical decisions. Suppose that the same steps are performed in a slightly
different order: (1) Get out of bed; (2) take off pajamas; (3) get dressed; (4) take a shower;
(5) eat breakfast; (6) carpool to work. In this case, our executive shows up for work soaking
wet. Specifying the order in which statements (actions) execute in a program is called pro-
gram control . This chapter investigates program control using Java's control statements .
 
 
 
Search WWH ::




Custom Search