Page 141 - Introduction to Programming with Java: A Problem Solving Approach
P. 141
4.1 Introduction
In Chapter 3, we kept things simple and wrote pure sequential programs. In a pure sequential program, statements execute in the sequence/order in which they are written; that is, after executing a statement, the computer executes the statement that immediately follows it. Pure sequential programming works well for trivial problems, but for anything substantial, you’ll need the ability to execute in a nonsequential fashion. For example, if you’re writing a recipe-retrieval program, you probably don’t want to print all of the program’s recipes one after another. You’ll want to execute the chocolate chip cookie print statements if the user indi- cates an affinity for chocolate chip cookies, and you’ll want to execute the crab quiche print statements if the user indicates an affinity for crab quiche. That sort of functionality requires the use of control statements. A control statement controls the order of execution of other statements. In Chapter 2 you used pseudocode if and while statements to control the order of execution within an algorithm. In this chapter, you’ll use Java if and while statements, plus a few additional Java statements, to control the order of execution within a program.
In controlling the order of execution, a control statement uses a condition (a question) to decide which way to go. We start Chapter 4 with an overview of Java conditions. We then describe Java’s control state- ments—the if statement, the switch statement, the while loop, the do loop, and the for loop. Along the way, we describe Java’s logical operators &&, ||, and !, which are needed when dealing with more complicated conditions. We conclude the chapter with several loop-related concepts—nested loops, input validation, and boolean variables. Good stuff!
4.2 Conditions and Boolean Values
Apago PDF Enhancer
In Chapter 2’s flowcharts, we used diamond shapes to represent logical decision points—points where con- trol flow went either one way or another. Into those diamonds we inserted various abbreviated questions like, “ceoSalary greater than $500,000?” and “count less than or equal to 100?” Then we labeled alternate paths away from those diamonds with “yes” or “no” answers to those questions. In Chapter 2’s pseudocode, we used “if” and “while” clauses to describe logical conditions. Examples included: “if shape is a circle,” “if grade is greater than or equal to 60,” and “while score is not equal to 1.” We considered pseudo- code conditions to be either “true” or “false.”
Informal condition expressions like these are fine for flowcharts and pseudocode, but when you start writing real Java code, you must be precise. The computer interprets each “if” condition or loop condi- tion as a two-way choice. What are the two possible values recognized by Java? They are the Java values true and false. These values, true and false, are called Boolean values, after George Boole, a fa- mous 19th century logician. Throughout the rest of this chapter, you’ll see if statements and loop state- ments where conditions appear as little fragments of code within a pair of parentheses, like this:
if (<condition>) {
.. .
}
while (<condition>) {
}
.. .
4.2 Conditions and Boolean Values 107