Docsity

Java Programming: Control Structures and Selection Statements, Study notes of Java Programming

Partial preview of the text

Download Java Programming: Control Structures and Selection Statements and more Study notes Java Programming in PDF only on Docsity!

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-

Chapter 3 Control Statements

Chapter 1 Introduction to Computers, Programs,

and Java

Basic computer skills such as using Windows,Internet Explorer, and Microsoft WordChapter 2 Primitive Data Types and OperationsChapter 3 Control StatementsChapter 4 MethodsChapter 5 Arrays

Prerequisites for Part I

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-

Objectives

)

To understand the flow of control in selection and loop statements(§

)

To use Boolean expressions to control selection statements and loopstatements (

)

To implement selection control using if and nested if statements(§

)

To implement selection control using switch statements (

)

To write expressions using the conditional operator (

)

To use while, do-while, and for loop statements to control therepetition of statements (

)

To write nested loops (

)

To know the similarities and differences of three types of loops(§

)

To implement program control with break and continue (

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

Simple

if

Statements

BooleanExpressiontrue Statement(s)

false

(radius >= 0)true

area = radius * radius * PI;System.out.println("The area for the circle of " +"radius " + radius + " is " + area);

false

(A)

(B)

if (booleanExpression) { statement(s); }

if (radius >= 0) { area = radius * radius * PI;System.out.println("The area" “ for the circle of radius "+ "radius is " + area); }

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

Note

if

(

(i > 0) && (i < 10)

)^

{

System.out.println("i is an " +

  • "integer between 0 and 10"); }

(a)

Equivalent

(b)

if

(

(i > 0) && (i < 10)

)

System.out.println("i is an " +

  • "integer between 0 and 10");

Outer parentheses required

Braces can be omitted if the block contains a singlestatement

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

The

if...else

Statement

if

(booleanExpression)

statement(s)-for-the-true-case; } else

statement(s)-for-the-false-case; }

BooleanExpression

false

true

Statement(s) for the false case

Statement(s) for the true case

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

if...else

Example

if (radius >= 0) {

area = radius * radius * 3.14159;System.out.println("The area for the “

  • “circle of radius " + radius +" is " + area);

} else {

System.out.println("Negative input"); }

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

Note

The else clause matches the most recent if clause in thesame block.^ int i = 1;int j = 2;int k = 3;if (i > j)

if (i > k)

System.out.println("A");

else

System.out.println("B");

(a)

Equivalent

( b )

int i = 1;int j = 2;int k = 3;if (i > j)

if (i > k)

System.out.println("A"); else

System.out.println("B");

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

Note, cont. rights reserved. 0-13-148952-

Nothing is printed from the preceding statement. Toforce the else clause to match the first if clause, you mustadd a pair of braces:

int

i

int

j

int

k

if

(i

j)

if

(i

k)

System.out.println("A");

} else

System.out.println("B");

This statement prints B.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

CAUTION

if (even == true)

System.out.println(

"It is even.");

(a)

Equivalent

if (even)

System.out.println(

"It is even.");

(b)

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

Example 3.1 Computing Taxes

The US federal personal income tax is calculated based onthe filing status and taxable income. There are four filingstatuses: single filers, married filing jointly, married filingseparately, and head of household. The tax rates for 2002are shown in Table 3.1.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

switch

Statements

switch (status) {

case 0: compute taxes for single filers;

break;

case 1: compute taxes for married file jointly;

break;

case 2: compute taxes for married file separately;

break;

case 3: compute taxes for head of household;

break;

default: System.out.println("Errors: invalid status");

System.exit(0);

}

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

switch

Statement Flow Chart

status is 0

Compute tax for single filers

break

Compute tax for married file jointly

break

status is 1

Compute tax for married file separatly

break

status is 2

Compute tax for head of household

break

status is 3

Default actions

default

Next Statement

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

switch

Statement Rules

The keyword break is optional,but it should be used at the end ofeach case in order to terminate theremainder of the switchstatement. If the break statementis not present, the next casestatement will be executed.

switch (switch-expression) {case value1: statement(s)1;

break;

case value2: statement(s)2;

break;

… case valueN: statement(s)N;

break;

default: statement(s)-for-default;}

The default case, which isoptional, can be used to performactions when none of thespecified cases matches theswitch-expression.

The case statements are executed in sequentialorder, but the order of the cases (including thedefault case) does not matter. However, it is goodprogramming style to follow the logical sequenceof the cases and place the default case at the end.

Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All

rights reserved. 0-13-148952-

Conditional Operator

if (x > 0)

y = 1

else

y = -1;

is equivalent toy = (x > 0)? 1 : -1;(booleanExpression)? expression1 : expression2Ternary operatorBinary operatorUnary operator