3.2. boolean Data Type and Operations

 
[Page 67]

Chapter 3. Selection Statements

Mayan God Shel, Mexico. Photographer: Philip Coblentz. Courtesy Brand X Pictures.

Objectives

  • To declare boolean type and write Boolean expressions ( §3.2).

  • To distinguish between conditional and unconditional && and operators ( §3.2.1).

  • To use Boolean expressions to control selection statements ( § §3.3 “3.5).

  • To implement selection control using if and nested if statements ( §3.3).

  • To implement selection control using switch statements ( §3.4).

  • To write expressions using the conditional operator ( §3.5).

  • To display formatted output using the System.out.printf method and to format strings using the String.format method ( §3.6).

  • To know the rules governing operand evaluation order, operator precedence, and operator associativity ( § §3.7 “3.8).


[Page 68]

3.1. Introduction

In Chapter 2, "Primitive Data Types and Operations," if you assigned a negative value for radius in Listing 2.1, ComputeArea.java, the program would print an invalid result. If the radius is negative, you don't want the program to compute the area. Like all high-level programming languages, Java provides selection statements that let you choose actions with two or more alternative courses. You can use selection statements in the following pseudocode (i.e., natural language mixed with programming code) to rewrite Listing 2.1:

   if   the radius is negative   the program displays a message indicating a wrong input;   else   the program computes the area and displays the result; 

Selection statements use conditions. Conditions are Boolean expressions . This chapter first introduces Boolean types, values, operators, and expressions.

 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net