control statements: part 2kraemere/cse491/notes/javahtp7e_05_6pp.pdf · Ó 1992-2007 pearson...

16
1 1 1992-2007 Pearson Education, Inc. All rights reserved. 5 5 Control Statements: Part 2 2 1992-2007 Pearson Education, Inc. All rights reserved. Not everything that can be counted counts, and not every thing that counts can be counted. Albert Einstein Who can control his fate? William Shakespeare The used key is always bright. Benjamin Franklin Intelligence … is the faculty of making artificial objects, especially tools to make tools. Henri Bergson Every advantage in the past is judged in the light of the final issue. Demosthenes 3 1992-2007 Pearson Education, Inc. All rights reserved. OBJECTIVES In this chapter you will learn: The essentials of counter-controlled repetition. To use the and repetition statements to execute statements in a program repeatedly. To understand multiple selection using the selection statement. To use the and program control statements to alter the flow of control. To use the logical operators to form complex conditional expressions in control statements. 4 1992-2007 Pearson Education, Inc. All rights reserved. 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 Repetition Statement 5.4 Examples Using the Statement 5.5 Repetition Statement 5.6 Multiple-Selection Statement 5.7 and Statements 5.8 Logical Operators 5.9 Structured Programming Summary 5.10 (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals 5.11 (Optional) Software Engineering Case Study: Identifying ObjectsStates and Activities 5.12 Wrap-Up 5 1992-2007 Pearson Education, Inc. All rights reserved. 5.1 Introduction Continue structured-programming discussion Introduce Java’s remaining control structures , , 6 1992-2007 Pearson Education, Inc. All rights reserved. 5.2 Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable Increment/decrement of control variable through each loop Loop-continuation condition that tests for the final value of the control variable

Upload: others

Post on 18-Jan-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

1

1

1992-2007 Pearson Education, Inc. All rights reserved.

55Control Statements:

Part 2

2

1992-2007 Pearson Education, Inc. All rights reserved.

Not everything that can be counted counts, and not every thing that counts can be counted.

— Albert Einstein

Who can control his fate?— William Shakespeare

The used key is always bright.— Benjamin Franklin

Intelligence … is the faculty of making artificial objects, especially tools to make tools.

— Henri Bergson

Every advantage in the past is judged in the light of the final issue.

— Demosthenes

3

1992-2007 Pearson Education, Inc. All rights reserved.

OBJECTIVESIn this chapter you will learn:� The essentials of counter-controlled repetition.� To use the ��� and ������ repetition

statements to execute statements in a program repeatedly.

� To understand multiple selection using the ���� � selection statement.

� To use the ���� and ������ program control statements to alter the flow of control.

� To use the logical operators to form complex conditional expressions in control statements.

4

1992-2007 Pearson Education, Inc. All rights reserved.

5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 ������������ Repetition Statement 5.4 Examples Using the ������������ Statement 5.5 ������������������������ Repetition Statement 5.6 ���� ����� ����� ����� � Multiple-Selection Statement 5.7 ���������������� and ������ ������ ������ ������ Statements 5.8 Logical Operators 5.9 Structured Programming Summary 5.10 (Optional) GUI and Graphics Case Study: Drawing

Rectangles and Ovals 5.11 (Optional) Software Engineering Case Study:

Identifying Objects’ States and Activities

5.12 Wrap-Up

5

1992-2007 Pearson Education, Inc. All rights reserved.

5.1 Introduction

• Continue structured-programming discussion– Introduce Java’s remaining control structures

� ������������, ��������…������������, ���� ����� ����� ����� �

6

1992-2007 Pearson Education, Inc. All rights reserved.

5.2 Essentials of Counter-Controlled Repetition

• Counter-controlled repetition requires:– Control variable (loop counter)– Initial value of the control variable– Increment/decrement of control variable through each

loop– Loop-continuation condition that tests for the final value of

the control variable

Page 2: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

2

7

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������������������

1 ������������������������������������������������������������������������������������������������������������

2 ������������������������������������ ��������!��������������������!�����������"��� ��������!��������������������!�����������"��� ��������!��������������������!�����������"��� ��������!��������������������!�����������"���

3 4 !��� � ���!��� � ���!��� � ���!��� � ���������������������������������������������������

5 ####����

6 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(��"���$�%����������&'�(��"���$�%����������&'�(��"���$�%����������&'�(�����

7 ���#���������#���������#���������#����������

8 ������������������������������������� ������)�� ������)�� ������)�� ������)�����*�*�*�*����� ����������� ����������� ����������� ��������������+� �������������������+� �������������������+� �������������������+� ������������

9 10 �������������������������������������$��$��$��$� ������,)� ������,)� ������,)� ������,)��-�-�-�-�(��(��(��(������!�����!�����!�����! ������������ �������� ������������ �������� ������������ �������� ������������ ��������

11 ������#������#������#������#����

12 ���������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$�/0���//0���//0���//0���/1� ������(*1� ������(*1� ������(*1� ������(*����

13 ������������������������������������22 �����*�22 �����*�22 �����*�22 �����*������ �"��� ��������������.������� �"��� ��������������.������� �"��� ��������������.������� �"��� ��������������.��

14 ������3�������3�������3�������3�������������������������������������

15 16 ������%.��"�����!�����$(*������%.��"�����!�����$(*������%.��"�����!�����$(*������%.��"�����!�����$(*�������!����������������!����������������!����������������!���������

17 ���3����3����3����3�������"���������"���������"���������"���

18 3�3�3�3������� ������������������� ������������������� ������������������� ������������� ���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-����

Control-variable name is �����

Control-variable initial value is �

Condition tests for �����’s final value

Increment for �����

8

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.1

Because floating-point values may be approximate, controlling loops with floating-point variables may result in imprecise counter values and inaccurate termination tests.

9

1992-2007 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip 5.1

Control counting loops with integers.

10

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.1

Place blank lines above and below repetition and selection control statements, and indent the statement bodies to enhance readability.

11

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 5.1

“Keep it simple” remains good advice for most of the code you will write.

12

1992-2007 Pearson Education, Inc. All rights reserved.

5.3 ������������ Repetition Statement

• Handles counter-controlled-repetition details

Page 3: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

3

13

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

��������������������������������������������������������

;����-��� ������)��*

;����- ������,)��-*

;����-

�����22*

1 ����������4��������������������������4��������������������������4��������������������������4����������������

2 ������������������������������������ ��������!��������������������!�����������"��� ��������!��������������������!�����������"��� ��������!��������������������!�����������"��� ��������!��������������������!�����������"���

3 4 !��� � ���!��� � ���!��� � ���!��� � ���������������������������������������������������

5 ####����

6 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(��"���$�%����������&'�(��"���$�%����������&'�(��"���$�%����������&'�(�����

7 ���#���#���#���#����

8 �����������������������������������"���������� �����������+�������������"���������� �����������+�������������"���������� �����������+�������������"���������� �����������+�����1���1���1���1����

9 �����������������������������!�����!�����!�����! ������������ ��������������� �"�� ������������ ��������������� �"�� ������������ ��������������� �"�� ������������ ��������������� �"����������������������

10 �������������������������������������$��$��$��$�������������� ������)�� ������)�� ������)�� ������)�����*� ������,)�*� ������,)�*� ������,)�*� ������,)��-�-�-�-*� �����22�(*� �����22�(*� �����22�(*� �����22�(����

11 ������������������������������������%.��"�����!�����$%.��"�����!�����$%.��"�����!�����$%.��"�����!�����$�/0���/�/0���/�/0���/�/0���/1� ������(*1� ������(*1� ������(*1� ������(*����������������������������������������

12 13 ������%.��"�����!�����$(*�������%.��"�����!�����$(*�������%.��"�����!�����$(*�������%.��"�����!�����$(*�������!���������������!���������������!���������������!���������

14 ���3���3���3���3����������"���������"���������"���������"���

15 3�3�3�3������� ������������������� ������������������� ������������������� ������������� ���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-����

Control-variable name is �����

Control-variable initial value is �

Condition tests for �����’s final value

Increment for �����

14

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.2

Using an incorrect relational operator or an incorrect final value of a loop counter in the loop-continuation condition of a repetition statement can cause an off-by-one error.

15

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.2

Using the final value in the condition of a ������������ or for statement and using the <= relational operator helps avoid off-by-one errors. For a loop that prints the values 1 to 10, the loop-continuation condition should be ������,)��- ������,)��- ������,)��- ������,)��- rather than ������,� ������,� ������,� ������,�

�-�-�-�- (which causes an off-by-one error) or ������,� ������,� ������,� ������,�

�������� (which is correct). Many programmers prefer so-called zero-based counting, in which to count 10 times, ����� ����� ����� ����� would be initialized to zero and the loop-continuation test would be ������,��- ������,��- ������,��- ������,��-.

16

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.3 | ������������ statement header components.

17

1992-2007 Pearson Education, Inc. All rights reserved.

5.3 ������������ Repetition Statement (Cont.)

��� ( initialization; loopContinuationCondition; increment ) statement;

can usually be rewritten as:

initialization;��� ( loopContinuationCondition ) {

statement;increment;

}

18

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.3

Using commas instead of the two required semicolons in a ������������ header is a syntax error.

Page 4: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

4

19

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.4

When a ������������ statement’s control variable is declared in the initialization section of the ������������’s header, using the control variable after the ������������’s body is a compilation error.

20

1992-2007 Pearson Education, Inc. All rights reserved.

Performance Tip 5.1

There is a slight performance advantage to preincrementing, but if you choose to postincrement because it seems more natural (as in a ������������ header), optimizing compilers will generate Java bytecode that uses the more efficient form anyway.

21

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.3

In the most cases, preincrementing and postincrementing are both used to add 1 to a variable in a statement by itself. In these cases, the effect is exactly the same, except that preincrementing has a slight performance advantage. Given that the compiler typically optimizes your code to help you get the best performance, use the idiom with which you feel most comfortable in these situations.

22

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.5

Placing a semicolon immediately to the right of the right parenthesis of a ������������ header makes that ������������’s body an empty statement. This is normally a logic error.

23

1992-2007 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip 5.2

Infinite loops occur when the loop-continuation condition in a repetition statement never becomes ������������. To prevent this situation in a counter-controlled loop, ensure that the control variable is incremented (or decremented) during each iteration of the loop. In a sentinel-controlled loop, ensure that the sentinel value is eventually input.

24

1992-2007 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip 5.3

Although the value of the control variable can be changed in the body of a ������������ loop, avoid doing so, because this practice can lead to subtle errors.

Page 5: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

5

25

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.4 | UML activity diagram for the ������������ statement in Fig. 5.2.

26

1992-2007 Pearson Education, Inc. All rights reserved.

5.4 Examples Using the ������������ Statement

• Varying control variable in ������������ statement– Vary control variable from ���� to �--�--�--�-- in increments of ����

� ������������ $�$�$�$������������� ��)���)���)���)�����*���,)�*���,)�*���,)�*���,)��--�--�--�--*��22�(*��22�(*��22�(*��22�(

– Vary control variable from �--�--�--�-- to ���� in increments of <<<<����� ������������ $�$�$�$������������� ��)���)���)���)��--�--�--�--*���=)�*���=)�*���=)�*���=)�����*��*��*��*�� ((((

– Vary control variable from 8888 to 88888888 in increments of 8888� ������������ $�$�$�$������������� ��)���)���)���)�8888*���,)�*���,)�*���,)�*���,)�88888888*���2)�*���2)�*���2)�*���2)�8888 ((((

– Vary control variable from 4-4-4-4- to 4444 in decrements of 4444� ������������ $�$�$�$������������� ��)���)���)���)�4-4-4-4-*���=)�*���=)�*���=)�*���=)�4444*���*���*���*��� )�)�)�)�4444 ((((

– Vary control variable over the sequence: 4444, ����, 9999, ��������, �6�6�6�6, �8�8�8�8, 4-4-4-4-� ������������ $�$�$�$������������� ��)���)���)���)�4444*���,)�*���,)�*���,)�*���,)�4-4-4-4-*���2)�*���2)�*���2)�*���2)�5555 ((((

– Vary control variable over the sequence: ::::::::, 99999999, 88888888, 77777777, ��������, 66666666, 55555555, 44444444, ��������, ----� ������������ $�$�$�$������������� ��)���)���)���)�::::::::*���=)�*���=)�*���=)�*���=)�----*���*���*���*��� )�)�)�)��������� ((((

27

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.6

Not using the proper relational operator in the loop-continuation condition of a loop that counts downward (e.g., using ��,)����,)����,)����,)�� instead of ��=)����=)����=)����=)�� in a loop counting down to 1) is usually a logic error.

28

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

%�"�����%�"�����%�"�����%�"�����

;�����;�����;�����;�����

1 �������������%�"������������������%�"������������������%�"������������������%�"�����

2 ���%�""���������������������������"������%�""���������������������������"������%�""���������������������������"������%�""���������������������������"���

3 4 !��� � ���!��� � ���!��� � ���!��� � ����%�"��%�"��%�"��%�"�����

5 ####����

6 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

7 ���#���#���#���#����

8 ������������������������������������������)�������)�������)�������)�----****�����������+����������������+����������������+����������������+�����

9 10 ���������������������������"�4������������������������������"�4������������������������������"�4������������������������������"�4���������4-������4-������4-������4-

11 �������������������������������������$��$��$��$����������������"���)����"���)����"���)����"���)�4444*���"���,)�*���"���,)�*���"���,)�*���"���,)�4-4-4-4-*���"���2)�*���"���2)�*���"���2)�*���"���2)�4444�(�(�(�(����

12 �����������������������������������������2)���"��*�����2)���"��*�����2)���"��*�����2)���"��*����������������������������������������������������������������������������������������������������������������������������

13 14 ������%.��"�����!�����$������%.��"�����!�����$������%.��"�����!�����$������%.��"�����!�����$�/%�"����0��/%�"����0��/%�"����0��/%�"����0�>>>>�/�/�/�/1������(*�1������(*�1������(*�1������(*�������!�.������������!�.������������!�.������������!�.������

15 ���3���3���3���3�������"����������"����������"����������"���

16 3333������� ����%�"������� ����%�"������� ����%�"������� ����%�" %�"������-%�"������-%�"������-%�"������-����

increment ��"�� by 4 each iteration

29

1992-2007 Pearson Education, Inc. All rights reserved.

5.4 Examples Using the ������������ Statement (Cont.)

• Initialization and increment expression can be comma-separated lists of expressions

– E.g., lines 11-12 of Fig. 5.5 can be rewritten asfor ( int number = 2; number <= 20; total += number, number += 2 )

; // empty statement

30

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.4

Limit the size of control statement headers to a single line if possible.

Page 6: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

6

31

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.5

Place only expressions involving the control variables in the initialization and increment sections of a ������������ statement. Manipulations of other variables should appear either before the loop (if they execute only once, like initialization statements) or in the body of the loop (if they execute once per iteration of the loop, like increment or decrement statements).

32

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

?����������?����������?����������?����������

(1 of 2)

Line 8

Line 13

1 ����������7��?��������������������7��?��������������������7��?��������������������7��?����������

2 �����"!���������"!���������"!���������"!���� ������� � ������������������������ � ������������������������ � ������������������������ � �����������������

3

4 !��� � ���!��� � ���!��� � ���!��� � ����?�������?�������?�������?����������

5 ####����

6 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

7 ���#���#���#���#����

8 ������������������������������������������"����*���"����*���"����*���"����*�����"���������!��������������� ��.������"���������!��������������� ��.������"���������!��������������� ��.������"���������!��������������� ��.��

9 ��������������������������������������������!��� �!��)�!��� �!��)�!��� �!��)�!��� �!��)��---�-�---�-�---�-�---�-*�*�*�*������������"���������������������������"���������������������������"���������������������������"����������������

10 ���������������������������������������������)������)������)������)�-�-�-�-�-�-�-�-�*�*�*�*�����������������������������������������������������

11 12 ������������!�.������������������!�.������������������!�.������������������!�.������

13 ������%.��"�����!�����$������%.��"�����!�����$������%.��"�����!�����$������%.��"�����!�����$�/0��/0��/0��/0�04-�04-�04-�04-�>>>>�/�/�/�/1111�/@��/�/@��/�/@��/�/@��/1�1�1�1�/A"���������!����//A"���������!����//A"���������!����//A"���������!����/�(*�(*�(*�(*����

14

Java treats literal values with decimal points as type ����

Second string is right justified and displayed with a field width of 4-

33

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

?����������?����������?����������?����������

(2 of 2)

Lines 16-23

Line 22

Program output

15 ��������������������������� � �����"���������!���������� ��������.������ � �����"���������!���������� ��������.������ � �����"���������!���������� ��������.������ � �����"���������!���������� ��������.�������

16 �������������������������������������$��$��$��$��������������.���)��.���)��.���)��.���)�����*�.���,)�*�.���,)�*�.���,)�*�.���,)��-�-�-�-*�.��22�(*�.��22�(*�.��22�(*�.��22�(��������������������������������������������������������

17 ������������������������####��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

18 ��������������������������������������� � ��������"����������! �����.����� � ��������"����������! �����.����� � ��������"����������! �����.����� � ��������"����������! �����.����������������������������������

19 �������������������������������������"�����)�!��� �!��B�C����!��$�"�����)�!��� �!��B�C����!��$�"�����)�!��� �!��B�C����!��$�"�����)�!��� �!��B�C����!��$���-���-���-���-�2����1�.���(*�2����1�.���(*�2����1�.���(*�2����1�.���(*����

20 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

21 ������������������������������������������!�.����.�����������"����������!�.����.�����������"����������!�.����.�����������"����������!�.����.�����������"��������������������������������������������������������������������

22 ������������������������������������%.��"�����!�����$�%.��"�����!�����$�%.��"�����!�����$�%.��"�����!�����$�/06�014-�4�/06�014-�4�/06�014-�4�/06�014-�4�>>>>�/�/�/�/1�.��1��"�1�.��1��"�1�.��1��"�1�.��1��"�����(*����(*����(*����(*����

23 ������������������������3�3�3�3���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

24 ���3����3����3����3�������"���������"���������"���������"���

25 3�3�3�3������� ����?����������� ����?����������� ����?����������� ����?����� @�����A"���������!����@�����A"���������!����@�����A"���������!����@�����A"���������!�������������������������1-�-�--�����������������1-�-�--�����������������1-�-�--�����������������1-�-�--�������4�������������1�-4��-���4�������������1�-4��-���4�������������1�-4��-���4�������������1�-4��-�������5�������������1��8�75���5�������������1��8�75���5�������������1��8�75���5�������������1��8�75�������6�������������14��������6�������������14��������6�������������14��������6�������������14��������������������������������������������1487�49�����������1487�49�����������1487�49�����������1487�49�������7�������������156-��-���7�������������156-��-���7�������������156-��-���7�������������156-��-�������8�������������16-8��-���8�������������16-8��-���8�������������16-8��-���8�������������16-8��-�������9�������������1688�67���9�������������1688�67���9�������������1688�67���9�������������1688�67�������:�������������1����55���:�������������1����55���:�������������1����55���:�������������1����55�������-�������������1749�9:���-�������������1749�9:���-�������������1749�9:���-�������������1749�9:����

Calculate �"���� with ���statement

Use the comma (1) formatting flag to display the amount with a thousands separator

34

1992-2007 Pearson Education, Inc. All rights reserved.

5.4 Examples Using the ������������ Statement (Cont.)

• Formatting output– Field width– Minus sign ( ) formatting flag for left justification– Comma (1111) formatting flag to output numbers with

grouping separators

������ ����� ����� ����� method– ClassName.methodName( arguments)

35

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.6Do not use variables of type ���������������� (or ����������������) to perform precise monetary calculations. The imprecision of floating-point numbers can cause errors that will result in incorrect monetary values. In the exercises, we explore the use of integers to perform monetary calculations. [Note: Some third-party vendors provide for-sale class libraries that perform precise monetary calculations. In addition, the Java API provides class �����"���������"���������"���������"����D��E �"�D��E �"�D��E �"�D��E �"�

for performing calculations with arbitrary precision floating-point values.]

36

1992-2007 Pearson Education, Inc. All rights reserved.

Performance Tip 5.2

In loops, avoid calculations for which the result never changes— such calculations should typically be placed before the loop. [Note: Many of today’s sophisticated optimizing compilers will place such calculations outside loops in the compiled code.]

Page 7: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

7

37

1992-2007 Pearson Education, Inc. All rights reserved.

5.5 ��������…������������ Repetition Statement

������������������������� statement– Similar to ������������ statement– Tests loop-continuation after performing body of loop

• i.e., loop body always executes at least once

38

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

E����F��E����F��E����F��E����F����������������������

;���9;���9;���9;���9

;�����-;�����-;�����-;�����- �6�6�6�6

G�����"����!��G�����"����!��G�����"����!��G�����"����!��

1 ����������8��E����F�����������������8��E����F�����������������8��E����F�����������������8��E����F�������

2 �������������!�����������"����������������!�����������"����������������!�����������"����������������!�����������"���

3 4 !��� � ����!��� � ����!��� � ����!��� � ����E����F���E����F���E����F���E����F�������

5 #��#��#��#������

6 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

7 ���#���#���#���#����

8 ������������������������������������� ������)�� ������)�� ������)�� ������)�����*�*�*�*�����������+� ���������������+� ���������������+� ���������������+� �����

9 10 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

11 ������������������������####��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

12 ������������������������������������%.��"�����!�����$�%.��"�����!�����$�%.��"�����!�����$�%.��"�����!�����$�/0���//0���//0���//0���/1� ������(*1� ������(*1� ������(*1� ������(*��������������������

13 ������������������������������������22 �����*22 �����*22 �����*22 �����*��������������������������������������������������������������������������������������������������������������������������������

14 ������������������������3�3�3�3��������������$� ������,)��$� ������,)��$� ������,)��$� ������,)��-�-�-�-�(*��(*��(*��(*���������������������������������������������������������

15 16 ������%.��"�����!�����$(*�������%.��"�����!�����$(*�������%.��"�����!�����$(*�������%.��"�����!�����$(*�������!����������������!����������������!����������������!����������

17 ���3���3���3���3�������"����������"����������"����������"���

18 3333������� ����E����F��������� ����E����F��������� ����E����F��������� ����E����F�� ���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-���4��5��6�����7��8��9��:���-����

Declares and initializes control variable �����

Variable �����’s value is displayed before testing �����’s final value

39

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.8 | �������������������������������� repetition statement UML activity diagram.

40

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.7

Always include braces in a ��������������������������������

statement, even if they are not necessary. This helps eliminate ambiguity between the ������������

statement and a �������������������������������� statement containing only one statement.

41

1992-2007 Pearson Education, Inc. All rights reserved.

5.6 ���� ����� ����� ����� � Multiple-Selection Statement

����� ����� ����� ����� � statement– Used for multiple selections

42

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���H���D���H���D���H���D�����������������������

(1 of 5)

Lines 8-14

1 ����������:��H���D������������������:��H���D������������������:��H���D������������������:��H���D�������� 2 ���H���D���� ������������ ������"������ �����A1�D1��1�E����������������H���D���� ������������ ������"������ �����A1�D1��1�E����������������H���D���� ������������ ������"������ �����A1�D1��1�E����������������H���D���� ������������ ������"������ �����A1�D1��1�E������������� 3 �"!����"!����"!����"!�������������% ����*�����������% ����*�����������% ����*�����������% ����*����!�����"����� ����% �������!�����"����� ����% �������!�����"����� ����% �������!�����"����� ����% ����

4 5 !��� !��� !��� !��� ���� ��� ��� ��� ����H���D�����H���D�����H���D�����H���D��������

6 ####����

7 ������������!�����!�����!�����!������%������ ����I�"*��%������ ����I�"*��%������ ����I�"*��%������ ����I�"*������"���� ������"���� ������"���� ������"���� ����������H���D�����!��������������H���D�����!��������������H���D�����!��������������H���D�����!����� 8 ������������!�����!�����!�����!��������������������������*������*������*������*������"��������������"��������������"��������������"���������������������������������������������������������������������������������

9 ������������!�����!�����!�����!��������������������������������*������������*������������*������������*������"���������������������"���������������������"���������������������"���������������� 10 ������������!�����!�����!�����!����������������������������*��������*��������*��������*���� ��������A��������� ��������A��������� ��������A��������� ��������A���������������������������������������������������������� 11 ������������!�����!�����!�����!����������������������������*��������*��������*��������*���� ��������D��� ��������D��� ��������D��� ��������D���������������������������������������������������������������������������� 12 ������������!�����!�����!�����!���������������������� �����*�� �����*�� �����*�� �����*���� ������������������ ������������������ ������������������ ������������������������������������������������������������������� 13 ������������!�����!�����!�����!����������������������������*��������*��������*��������*���� ��������E��������� ��������E��������� ��������E��������� ��������E���������������������������������������������������������� 14 ������������!�����!�����!�����!����������������������������*��������*��������*��������*���� ������������������ ������������������ ������������������ ������������������������������������������������������������������� 15 ����������������

16 ��������������� ������ �����������+�� ������� ������ �����������+�� ������� ������ �����������+�� ������� ������ �����������+�� ����I�"*�I�"*�I�"*�I�"*� 17 ������������������������� �������������������+�����-��.������������������� �������������������+�����-��.������������������� �������������������+�����-��.������������������� �������������������+�����-��.������ 18 ������������!��� !��� !��� !��� �H���D���$�%��������"�(�H���D���$�%��������"�(�H���D���$�%��������"�(�H���D���$�%��������"�(����

19 ���#���#���#���#����

20 ������ ����I�"�)���"*������� ����I�"�)���"*������� ����I�"�)���"*������� ����I�"�)���"*�����������+�� ����I�"����������+�� ����I�"����������+�� ����I�"����������+�� ����I�" 21 ���3����3����3����3������� ������ ��������� ������ ��������� ������ ��������� ������ ��� 22 23 ���������������"�������������� �������"���"�������������� �������"���"�������������� �������"���"�������������� �������" 24 ������������!��� !��� !��� !��� ����������������������������I�"$�%��������"�(��������I�"$�%��������"�(��������I�"$�%��������"�(��������I�"$�%��������"�(����

25 ���#���#���#���#����

26 ������ ����I�"�)���"*������� ����I�"�)���"*������� ����I�"�)���"*������� ����I�"�)���"*������������ �������"����������� �������"����������� �������"����������� �������" 27 ���3����3����3����3�������"������������I�"������"������������I�"������"������������I�"������"������������I�" 28

Page 8: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

8

43

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���H���D���H���D���H���D�����������������������

(2 of 5)

Lines 50-54

29 ���������������"����������������� �������"���"����������������� �������"���"����������������� �������"���"����������������� �������"

30 ������������!��� !��� !��� !��� �%�������������I�"$(�%�������������I�"$(�%�������������I�"$(�%�������������I�"$(����

31 ���#���#���#���#����

32 ��������������������������������������������� ����I�"*� ����I�"*� ����I�"*� ����I�"*����

33 ���3����3����3����3�������"������������I�"������"������������I�"������"������������I�"������"������������I�"

34 35 ������������������!�.���� �"�"�����������H���D�������������!�.���� �"�"�����������H���D�������������!�.���� �"�"�����������H���D�������������!�.���� �"�"�����������H���D�������

36 ������������!��� !��� !��� !��� ������������������������!�.C�����!�.C�����!�.C�����!�.C����$(���$(���$(���$(����

37 ���#���#���#���#����

38 �������������������������������I�"����������"������� ���������������I�"����������"������� ���������������I�"����������"������� ���������������I�"����������"������� ����

39 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�/� �"��������������������/� �"��������������������/� �"��������������������/� �"��������������������>>>>�0�J�0�J�0�J�0�J>>>>����>>>>�/�/�/�/1�1�1�1�����

40 ����������������I�"$(�(*����������������I�"$(�(*����������������I�"$(�(*����������������I�"$(�(*����

41 ���3����3����3����3�������"��������!�.C����������"��������!�.C����������"��������!�.C����������"��������!�.C����

42 43 �����������������!�����������.���"������!�����������.���"������!�����������.���"������!�����������.���"���������������"������������������"������������������"������������������"����

44 ������������!��� !��� !��� !��� �����������������������!��H����$(���!��H����$(���!��H����$(���!��H����$(����

45 ���#���#���#���#����

46 ������% �������!���)�������% �������!���)�������% �������!���)�������% �������!���)����������% ����$�%.��"����(*�% ����$�%.��"����(*�% ����$�%.��"����(*�% ����$�%.��"����(*����

47 48 �����������������������������������������*������*������*������*���������������.������������������.������������������.������������������.����

49 50 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�/0�/0�/0�/0�>>>>�0��0��0��0�>>>>����0�����0�����0�����0�>>>>����0�����0�����0�����0�>>>>�/�/�/�/1�1�1�1�����

51 ������������������������������������/K����/K����/K����/K������������������������������-��������������������������-��������������������������-��������������������������- �--�/�--�/�--�/�--�/1�1�1�1�����

52 ������������������������������������/F.!������/F.!������/F.!������/F.!������ �������� ������� ����������"�������!���/������� ����������"�������!���/������� ����������"�������!���/������� ����������"�������!���/1�1�1�1�����

53 ������������������������������������/L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K���//L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K���//L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K���//L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K���/1111����

54 ������������������������������������/L�����������.!�, ��=�+�����!����K���//L�����������.!�, ��=�+�����!����K���//L�����������.!�, ��=�+�����!����K���//L�����������.!�, ��=�+�����!����K���/�(*�(*�(*�(*����

55

Display prompt

44

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���H���D���H���D���H���D�����������������������

(3 of 5)

Line 57

Line 72 controlling expression

Lines 72-94

56 �����������������������������!�������������������������!�������������������������!�������������������������!�������������������� �������� ������� ����������� ����������� ����������� ���� 57 �������������������������������������$��$��$��$���!������IO�$(��!������IO�$(��!������IO�$(��!������IO�$(�(��(��(��(�����

58 ������#������#������#������#����

59 ��������������)���!����O�?��$(*���������������)���!����O�?��$(*���������������)���!����O�?��$(*���������������)���!����O�?��$(*��������������������������������������������� 60 ��������������2)�����*���������������2)�����*���������������2)�����*���������������2)�����*����������������������������������������������������������������������������� 61 ���������22����������*����������22����������*����������22����������*����������22����������*������ ������ ������ ������ �"�����"�����������"�����"�����������"�����"�����������"�����"����������� 62 ����������������������������������������

63 ��������������������������������������� ��"���������� �"����!!��!����� �������� ��"���������� �"����!!��!����� �������� ��"���������� �"����!!��!����� �������� ��"���������� �"����!!��!����� ����� 64 ����������� �"��;���H���������$������(*����������� �"��;���H���������$������(*����������� �"��;���H���������$������(*����������� �"��;���H���������$������(*����

65 ������3�������3�������3�������3���������������������������������������������

66 ���3����3����3����3�������"�������!��H����������"�������!��H����������"�������!��H����������"�������!��H���� 67 68 �������������������������!!��!����� ����������������!!��!����� ����������������!!��!����� ����������������!!��!����� �����������! �����������������! �����������������! �����������������! ��������� 69 ������������!��� !��� !��� !��� ����������������������� �"��;���H���������$���� �"��;���H���������$���� �"��;���H���������$���� �"��;���H���������$����������������"�� H����(���"�� H����(���"�� H����(���"�� H����(����

70 ���#���#���#���#����

71 ������������������������������"������ ���������������������"������ ���������������������"������ ���������������������"������ �����������������������������������������������������������������������

72 ���������������������������� ����� ����� ����� ��$���������$���������$���������$���������-�-�-�-�(�(�(�(��������������������������������������������������������������������������������������������������������������������

73 ������������������������####����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

74 ������������������������������������ �� �� �� ������::::�����������������������������:-�����������������:-�����������������:-�����������������:-������������������������������������������������������������

75 ������������������������������������ �� �� �� �������-�-�-�-����������������--��������--��������--��������--����������������������������������������������������������������������������������������������������������������

76 ������������������������������������������������22������*�22������*�22������*�22������*������ �"�������������� �"�������������� �"�������������� �"���������������������������������������������������������������������

77 ����������������������������������������������������������������*�*�*�*����� ����.����O������ ����.����O������ ����.����O������ ����.����O������� ������ ������ ������ �����������������������������������������

78 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

79 ������������������������������������ �� �� �� ������9999�������������������������9-�����9:�����������������9-�����9:�����������������9-�����9:�����������������9-�����9:������������������������������������

80 ������������������������������������������������22������*�22������*�22������*�22������*������ �"�������������� �"�������������� �"�������������� �"���������������������������������������������������������������������

81 ����������������������������������������������������������������*�*�*�*����O������� ����O������� ����O������� ����O������� ���������������������������������������������������������������������������������������������

82 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

Loop condition uses method ���IO� to determine whether there is more data to input

���� � statement determines which �� label to execute,

depending on controlling expression

(���� � �- ) is controlling expression

45

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���H���D���H���D���H���D�����������������������

(4 of 5)

Line 91 default case

83 ������������������������������������ �� �� �� ������8888�������������������������8-�����8:�����������������8-�����8:�����������������8-�����8:�����������������8-�����8:������������������������������������

84 ������������������������������������������������22 �����*�22 �����*�22 �����*�22 �����*������ �"��� ���������� �"��� ���������� �"��� ���������� �"��� �����������������������������������������������������������������

85 ����������������������������������������������������������������*�*�*�*����O������� ����O������� ����O������� ����O������� ���������������������������������������������������������������������������������������������

86 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

87 ������������������������������������ �� �� �� ������7777�������������������������7-�����7:�����������������7-�����7:�����������������7-�����7:�����������������7-�����7:������������������������������������

88 ������������������������������������������������22������*�22������*�22������*�22������*������ �"�������������� �"�������������� �"�������������� �"���������������������������������������������������������������������

89 ����������������������������������������������������������������*�*�*�*����O������� ����O������� ����O������� ����O������� ���������������������������������������������������������������������������������������������

90 ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

91 ������������������������������������������������������������������������������������������7-������������������7-������������������7-������������������7-����������������������������������������������������

92 ������������������������������������������������22������*�22������*�22������*�22������*������ �"�������������� �"�������������� �"�������������� �"���������������������������������������������������������������������

93 ����������������������������������������������������������������*�*�*�*�����!�����*����O������� ����.��.����!�����*����O������� ����.��.����!�����*����O������� ����.��.����!�����*����O������� ����.��.

94 ������������������������3�3�3�3����������� ����������� ����������� ����������� ���������������������������������������������������������������������������������������������������������������������������������������������

95 ���3����3����3����3�������"������� �"��;��������"������� �"��;��������"������� �"��;��������"������� �"��;���H����������H����������H����������H���������

96 97 ������������������!�.����!���������������������������.�����������!�.����!���������������������������.�����������!�.����!���������������������������.�����������!�.����!���������������������������.�����

98 ������������!��� !��� !��� !��� ������������������������!�.H���P!���$(����!�.H���P!���$(����!�.H���P!���$(����!�.H���P!���$(����

99 ���#���#���#���#����

100 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�////>>>>�H����P!����/�H����P!����/�H����P!����/�H����P!����/�(*�(*�(*�(*����

101 102 ��������������������������������������������������������������������������������������������������������������������������������������������������������

103 ���������������������������������$������������J)��$������������J)��$������������J)��$������������J)�----�(��(��(��(�����

104 ������#������#������#������#����

105 ��������������������������������������� � ���������������������������� � ���������������������������� � ���������������������������� � �������������������������

106 �����������������������������������������������������������)�$�������)�$�������)�$�������)�$����������������(������������������*��(������������������*��(������������������*��(������������������*������

107

����� �� for grade less than 7-

46

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���H���D���H���D���H���D�����������������������

(5 of 5)

108 ������������������������������������������!�����""��.���������������!�����""��.���������������!�����""��.���������������!�����""��.��������� 109 ���������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$�/F����������0����������������0�/F����������0����������������0�/F����������0����������������0�/F����������0����������������0�>>>>�/�/�/�/1�1�1�1�����

110 ����������������������1������(*����������������������1������(*����������������������1������(*����������������������1������(*����

111 ���������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$�/��������������0�4�/��������������0�4�/��������������0�4�/��������������0�4�>>>>�/�/�/�/1�������(*1�������(*1�������(*1�������(*����

112 ���������%.��"�����������%.��"�����������%.��"�����������%.��"�����!�����$����!�����$����!�����$����!�����$�/0�/0�/0�/0�>>>>�0�0��0�0��0�0��0�0�>>>>�0�0��0�0��0�0��0�0�>>>>�0�0��0�0��0�0��0�0�>>>>�0�0��0�0��0�0��0�0�>>>>�0�0��0�0��0�0��0�0�>>>>�/�/�/�/1�1�1�1�����

113 ������������������������������������������������/I�"������������������� ����� �������//I�"������������������� ����� �������//I�"������������������� ����� �������//I�"������������������� ����� �������/1�1�1�1�����

114 ������������������������������������������������/A��//A��//A��//A��/1�������1���1�������1���1�������1���1�������1���������!�.���"������A������������!�.���"������A������������!�.���"������A������������!�.���"������A������ 115 ������������������������������������������������/D��/1/D��/1/D��/1/D��/1�������1����������1����������1����������1���������!�.���"������D������������!�.���"������D������������!�.���"������D������������!�.���"������D������ 116 ������������������������������������������������/���//���//���//���/1� �����1���1� �����1���1� �����1���1� �����1���������!�.���"��������������������!�.���"��������������������!�.���"��������������������!�.���"�������������� 117 ������������������������������������������������/E��//E��//E��//E��/1�������1���1�������1���1�������1���1�������1���������!�.���"������E������������!�.���"������E������������!�.���"������E������������!�.���"������E������ 118 ������������������������������������������������/���//���//���//���/1��������(*�1��������(*�1��������(*�1��������(*�������!�.���"�������������������!�.���"�������������������!�.���"�������������������!�.���"������������� 119 ������3�������3�������3�������3��������������������������������� 120 ������������������������������������������������1�����������������������1�����������������������1�����������������������1�������!����!!��!�����"��������!����!!��!�����"��������!����!!��!�����"��������!����!!��!�����"���� 121 ���������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$����������%.��"�����!�����$�/I���������������//I���������������//I���������������//I���������������/�(*�(*�(*�(*����

122 ���3����3����3����3�������"��������!�.H���P!���������"��������!�.H���P!���������"��������!�.H���P!���������"��������!�.H���P!��� 123 3�3�3�3������� ����H���D��������� ����H���D��������� ����H���D��������� ����H���D���

47

1992-2007 Pearson Education, Inc. All rights reserved.

Portability Tip 5.1

The keystroke combinations for entering end-of-file are system dependent.

48

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.7

Forgetting a ���������������� statement when one is needed in a ���� ����� ����� ����� � is a logic error.

Page 9: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

9

49

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���F��H���D���F��H���D���F��H���D���F����������������������

(1 of 2)

Lines 13-15

1 �����������-��H���D���F������������������-��H���D���F������������������-��H���D���F������������������-��H���D���F�������

2 ��������H���D������� �1���!����������������!�.�������!������������H���D������� �1���!����������������!�.�������!������������H���D������� �1���!����������������!�.�������!������������H���D������� �1���!����������������!�.�������!����

3 4 !��� !��� !��� !��� ���� ��� ��� ��� ����H���D���F���H���D���F���H���D���F���H���D���F������

5 ####����

6 ������������!��� !��� !��� !��� ��������� ����� ����� ����� ���������������������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

7 ���#���#���#���#����

8 ��������������������������� ����H���D������� ��".H���D�������� ����H���D������� ��".H���D�������� ����H���D������� ��".H���D�������� ����H���D������� ��".H���D�����������������

9 ���������������������������!���� �������"���� ������ ������!���� �������"���� ������ ������!���� �������"���� ������ ������!���� �������"���� ������ ���

10 ������H���D����".H���D����)�������H���D����".H���D����)�������H���D����".H���D����)�������H���D����".H���D����)����������H���D���$��H���D���$��H���D���$��H���D���$�����

11 ������������������������������������/�%�-��?������ ��������Q����G�����""���//�%�-��?������ ��������Q����G�����""���//�%�-��?������ ��������Q����G�����""���//�%�-��?������ ��������Q����G�����""���/�(*�(*�(*�(*����

12 13 ������".H���D�������!�.C����$(*�������".H���D�������!�.C����$(*�������".H���D�������!�.C����$(*�������".H���D�������!�.C����$(*�������!�.�� �"�"����������!�.�� �"�"����������!�.�� �"�"����������!�.�� �"�"����

14 ������������������������".H���D�����".H���D�����".H���D�����".H���D������!��H����$(*��!��H����$(*��!��H����$(*��!��H����$(*�����������������"��������������������"��������������������"��������������������"����

15 ������������������������".H���D�������!�.H���P!���$(*�".H���D�������!�.H���P!���$(*�".H���D�������!�.H���P!���$(*�".H���D�������!�.H���P!���$(*�������!�.��!�����������������������!�.��!�����������������������!�.��!�����������������������!�.��!�����������������

16 ���3����3����3����3�������"���������"���������"���������"���

17 3�3�3�3������� ����H���D���F�������� ����H���D���F�������� ����H���D���F�������� ����H���D���F��

Call H���D��� !��� methods to count grades

50

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

H���D���F��H���D���F��H���D���F��H���D���F����������������������

(2 of 2)

Program output

� �"��������������������� �"��������������������� �"��������������������� �"�������������������������%�-��?������ ��������Q����G�����""���J�%�-��?������ ��������Q����G�����""���J�%�-��?������ ��������Q����G�����""���J�%�-��?������ ��������Q����G�����""���J���� K������������������������������-K������������������������������-K������������������������������-K������������������������������- �--��--��--��--�����F.!������F.!������F.!������F.!������ �������� ������� ����������"�������!���������� ����������"�������!���������� ����������"�������!���������� ����������"�������!����������L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K������L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K������L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K������L��MI?N�;���O�C� �L%�N��.!�, ��=�������!����K����������L�����������.!�, ��=�+������L�����������.!�, ��=�+������L�����������.!�, ��=�+������L�����������.!�, ��=�+�����!����K�����!����K�����!����K�����!����K�������::::::::����:4:4:4:4����6�6�6�6������8�8�8�8����75757575����8�8�8�8�����87878787����9�9�9�9�����:-:-:-:-�����--�--�--�--����RSRSRSRS���� H����P!����H����P!����H����P!����H����P!��������F�����������-���������������889F�����������-���������������889F�����������-���������������889F�����������-���������������889������������������88�9-��������������88�9-��������������88�9-��������������88�9-����I�"������������������� ����� �������I�"������������������� ����� �������I�"������������������� ����� �������I�"������������������� ����� �����������A��6A��6A��6A��6����D���D���D���D����������4���4���4���4����E���E���E���E����������4���4���4���4����

51

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 5.2

Provide a �������������������� case in ���� ����� ����� ����� � statements. Including a �������������������� case focuses you on the need to process exceptional conditions.

52

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.11 | ���� ����� ����� ����� � multiple-selection statement UML activity diagram with ���������������� statements.

53

1992-2007 Pearson Education, Inc. All rights reserved.

Good Programming Practice 5.8

Although each �� �� �� �� and the �������������������� case in a ���� ����� ����� ����� � can occur in any order, place the ��������������������

case last. When the default case is listed last, the ���������������� for that case is not required. Some programmers include this ���������������� for clarity and symmetry with other cases.

54

1992-2007 Pearson Education, Inc. All rights reserved.

5.6 ���� ����� ����� ����� � Multiple-Selection Statement (Cont.)

• Expression in each �� �� �� ��

– Constant integral expression• Combination of integer constants that evaluates to a constant

integer value– Character constant

• E.g., ‘A’, ‘7’ or ‘$’– Constant variable

• Declared with keyword ����������������

Page 10: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

10

55

1992-2007 Pearson Education, Inc. All rights reserved.

5.7 ���������������� and ������ ������ ������ ������ Statements

������ ����������� ����������� ����������� ������

– Alter flow of control

����������������� statement – Causes immediate exit from control structure

• Used in ������������, ������������, ������������������������ or ���� ����� ����� ����� � statements

� ������ ������ ������ ������ statement – Skips remaining statements in loop body– Proceeds to next iteration

• Used in ������������, ������������ or ������������������������ statements

56

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

D���F��D���F��D���F��D���F����������������������

;���:;���:;���:;���:

;������;������;������;������ �4�4�4�4

G�����"����!��G�����"����!��G�����"����!��G�����"����!��

1 �����������4��D���F������������������4��D���F������������������4��D���F������������������4��D���F������� 2 ������������"���O����������������"���������������"���O����������������"���������������"���O����������������"���������������"���O����������������"��� 3 !��� � ���!��� � ���!��� � ���!��� � ����D���F����D���F����D���F����D���F�������

4 ####����

5 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

6 ���#���#���#���#����

7 ������������������������������������� ����*�� ����*�� ����*�� ����*���� ����������������������������!���"�������� ����������������������������!���"�������� ����������������������������!���"�������� ����������������������������!���"����� 8 ����������������������������

9 �������������������������������������$� �����)��$� �����)��$� �����)��$� �����)�����*� �����,)�*� �����,)�*� �����,)�*� �����,)��-�-�-�-*� ����22�(�*� ����22�(�*� ����22�(�*� ����22�(������!��-���"������!��-���"������!��-���"������!��-���"� 10 ������#��������#��������#��������#������

11 ���������������������������������������������$� �����))��$� �����))��$� �����))��$� �����))������(��(��(��(������� ���������1������� ���������1������� ���������1������� ���������1� 12 ����������������������������������������������������������������*���������*���������*���������*��������������"�������!�����"�������!�����"�������!�����"�������! 13 14 ���������%.��"�����!�����$���������%.��"�����!�����$���������%.��"�����!�����$���������%.��"�����!�����$�/0��/�/0��/�/0��/�/0��/1� �����(*1� �����(*1� �����(*1� �����(*����

15 ������3�������3�������3�������3������������������������������������� 16 17 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�////>>>>�D�������������!���� �����)�0��D�������������!���� �����)�0��D�������������!���� �����)�0��D�������������!���� �����)�0�>>>>�/�/�/�/1� �����(*1� �����(*1� �����(*1� �����(*����

18 ���3����3����3����3�������"���������"���������"���������"��� 19 3�3�3�3������� ����D���F�������� ����D���F�������� ����D���F�������� ����D���F��

��4�5�6��4�5�6��4�5�6��4�5�6����D�������������!���� �����)��D�������������!���� �����)��D�������������!���� �����)��D�������������!���� �����)������

Loop �- timesExit for statement (����)

when ���� equals �

57

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

�������F���������F���������F���������F����������������������

;���8;���8;���8;���8

;����:;����:;����:;����: �-�-�-�-

G�����"����!��G�����"����!��G�����"����!��G�����"����!��

1 �����������5���������F������������������5���������F������������������5���������F������������������5���������F�������

2 ��� �����������"�����"���������������������������������"������ �����������"�����"���������������������������������"������ �����������"�����"���������������������������������"������ �����������"�����"���������������������������������"���

3 !��� � ���!��� � ���!��� � ���!��� � �����������F�����������F�����������F�����������F�������

4 ####����

5 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

6 ���#���#���#���#����

7 �������������������������������������$��$��$��$�������������� �����)�� �����)�� �����)�� �����)�����*� �����,)�*� �����,)�*� �����,)�*� �����,)��-�-�-�-*� ����22�(�*� ����22�(�*� ����22�(�*� ����22�(������������!��-���"����!��-���"����!��-���"����!��-���"�

8 ������#��������#��������#��������#������

9 ���������������������������������������������$� �����))��$� �����))��$� �����))��$� �����))������(��(��(��(������� ���������1������� ���������1������� ���������1������� ���������1�

10 ������������������������������������������������ ������ ������ ������ ������*������*������*������*������������!��"������� ��������!������!��"������� ��������!������!��"������� ��������!������!��"������� ��������!

11 12 ���������%.��"�����!�����$���������%.��"�����!�����$���������%.��"�����!�����$���������%.��"�����!�����$�/0��/�/0��/�/0��/�/0��/1� �����(*1� �����(*1� �����(*1� �����(*����

13 ������3�������3�������3�������3�������������������������������������

14 15 ������%.��"�����!�����������%.��"�����!�����������%.��"�����!�����������%.��"�����!�����$�$�$�$�////>>>>�M��� �������������!�!���������/�M��� �������������!�!���������/�M��� �������������!�!���������/�M��� �������������!�!���������/�(*�(*�(*�(*����

16 ���3����3����3����3�������"���������"���������"���������"���

17 3�3�3�3������� �����������F�������� �����������F�������� �����������F�������� �����������F�� ��4�5�6�7�8�9�:��-��4�5�6�7�8�9�:��-��4�5�6�7�8�9�:��-��4�5�6�7�8�9�:��-����M��� �������������!�!���������M��� �������������!�!���������M��� �������������!�!���������M��� �������������!�!�������������

Loop �- times

Skip line �4 and proceed to line 8 when ���� equals �

58

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 5.3

Some programmers feel that ���� and ������ ������ ������ ������ violate structured programming. Since the same effects are achievable with structured programming techniques, these programmers do not use ���� or ������.

59

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 5.4

There is a tension between achieving quality software engineering and achieving the best-performing software. Often, one of these goals is achieved at the expense of the other. For all but the most performance-intensive situations, apply the following rule of thumb: First, make your code simple and correct; then make it fast and small, but only if necessary.

60

1992-2007 Pearson Education, Inc. All rights reserved.

5.8 Logical Operators

• Logical operators– Allows for forming more complex conditions– Combines simple conditions

• Java logical operators– TTTTTTTT (conditional AND)– UUUUUUUU (conditional OR)– TTTT (boolean logical AND)– UUUU (boolean logical inclusive OR)– RRRR (boolean logical exclusive OR)– JJJJ (logical NOT)

Page 11: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

11

61

1992-2007 Pearson Education, Inc. All rights reserved.

5.8 Logical Operators (Cont.)

• Conditional AND (TTTTTTTT) Operator– Consider the following �������� statement

�������� $������))�$������))�$������))�$������))��KCA;K�KCA;K�KCA;K�KCA;K TT����=)�TT����=)�TT����=)�TT����=)�7�7�7�7� ((((

22222222������"��������"��������"��������"��****

– Combined condition is ������������

• if and only if both simple conditions are ������������

– Combined condition is ������������

• if either or both of the simple conditions are ������������

62

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.14 | TTTTTTTT (conditional AND) operator truth table.

expression1 expression2 expression1 TTTTTTTT expression2 ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ FFFF��������

63

1992-2007 Pearson Education, Inc. All rights reserved.

5.8 Logical Operators (Cont.)

• Conditional OR (UUUUUUUU) Operator– Consider the following �������� statement

�������� $�$�$�$�$�$�$�$��"���A�����"���A�����"���A�����"���A���� =)�=)�=)�=)�:-:-:-:- (�UU�$�(�UU�$�(�UU�$�(�UU�$�����KO�"����KO�"����KO�"����KO�" =)�=)�=)�=)�:-:-:-:- ((((

%.��"�����%.��"�����%.��"�����%.��"�����!�����!�����!�����!�����$�V%��������������AW�(*$�V%��������������AW�(*$�V%��������������AW�(*$�V%��������������AW�(*

– Combined condition is ������������

• if either or both of the simple condition are ������������

– Combined condition is ������������

• if both of the simple conditions are ������������

64

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.15 | UUUUUUUU (conditional OR) operator truth table.

expression1 expression2 expression1 UUUUUUUU expression2 ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������

65

1992-2007 Pearson Education, Inc. All rights reserved.

5.8 Logical Operators (Cont.)

• Short-Circuit Evaluation of Complex Conditions– Parts of an expression containing TTTTTTTT or UUUUUUUU operators are

evaluated only until it is known whether the condition is true or false

– E.g., $������))�$������))�$������))�$������))��KCA;K�KCA;K�KCA;K�KCA;K (�TT�$����=)�(�TT�$����=)�(�TT�$����=)�(�TT�$����=)�7�7�7�7� ((((

• Stops immediately if gender is not equal to �KCA;K�KCA;K�KCA;K�KCA;K

66

1992-2007 Pearson Education, Inc. All rights reserved.

Common Programming Error 5.8In expressions using operator TTTTTTTT, a condition—we will call this the dependent condition—may require another condition to be true for the evaluation of the dependent condition to be meaningful. In this case, the dependent condition should be placed after the other condition, or an error might occur. For example, in the expression $���J)�-�(�$���J)�-�(�$���J)�-�(�$���J)�-�(�

TT�$��-�����))�4�(1TT�$��-�����))�4�(1TT�$��-�����))�4�(1TT�$��-�����))�4�(1 the second condition must appear after the first condition, or a divide-by-zero error might occur.

Page 12: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

12

67

1992-2007 Pearson Education, Inc. All rights reserved.

5.8 Logical Operators (Cont.)

• Boolean Logical AND (TTTT) Operator– Works identically to TTTTTTTT– Except TTTT always evaluate both operands

• Boolean Logical OR (UUUU) Operator – Works identidally to UUUUUUUU– Except UUUU always evaluate both operands

68

1992-2007 Pearson Education, Inc. All rights reserved.

Error-Prevention Tip 5.4

For clarity, avoid expressions with side effects in conditions. The side effects may look clever, but they can make it harder to understand code and can lead to subtle logic errors.

69

1992-2007 Pearson Education, Inc. All rights reserved.

5.8 Logical Operators (Cont.)

• Boolean Logical Exclusive OR (RRRR)– One of its operands is ������������ and the other is ������������

• Evaluates to ������������

– Both operands are ������������ or both are ������������

• Evaluates to ������������

• Logical Negation (JJJJ) Operator– Unary operator

70

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.16 | RRRR (boolean logical exclusive OR) operator truth table.

expression1 expression2 expression1 RRRR expression2 ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������ ������������

71

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.17 |J�J�J�J�(logical negation, or logical NOT) operator truth table.

expression JJJJexpression ������������ ������������ ������������ ������������

72

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

;��� �L!������;��� �L!������;��� �L!������;��� �L!������������������������������

(1 of 3)

Lines 9-13

Lines 16-20

Lines 23-27

1 �����������9��;��� �L!����������������������9��;��� �L!����������������������9��;��� �L!����������������������9��;��� �L!�����������

2 ���;��� ���!����������;��� ���!����������;��� ���!����������;��� ���!�������

3 4 !��� � ���!��� � ���!��� � ���!��� � ����;��� �L!��������;��� �L!��������;��� �L!��������;��� �L!�����������

5 ####����

6 ������������!��� ������ �����!��� ������ �����!��� ������ �����!��� ������ ������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

7 ���#���#���#���#����

8 ��������� ������������������TT�$ ����������AIE(��!�������������� ������������������TT�$ ����������AIE(��!�������������� ������������������TT�$ ����������AIE(��!�������������� ������������������TT�$ ����������AIE(��!�����

9 ������%.��"�����!������%.��"�����!������%.��"�����!������%.��"�����!�����$������$������$������$�/0�/0�/0�/0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>����>>>>�/�/�/�/1111����

10 ������������������������������������/�����������AIE�$TT(//�����������AIE�$TT(//�����������AIE�$TT(//�����������AIE�$TT(/1�1�1�1�/����TT����//����TT����//����TT����//����TT����/1�1�1�1�$�$�$�$��������������TT��TT��TT��TT��������������(�(�(�(1111����

11 ������������������������������������/����TT����//����TT����//����TT����//����TT����/1�1�1�1�$�$�$�$��������������TT��TT��TT��TT��������������(�(�(�(1�1�1�1�����

12 ������������������������������������/����TT����//����TT����//����TT����//����TT����/1�1�1�1�$�$�$�$��������������TT��TT��TT��TT��������������(�(�(�(1111����

13 ������������������������������������/����TT����//����TT����//����TT����//����TT����/1�1�1�1�$�$�$�$��������������TT��TT��TT��TT��������������(�(�(�(�(*�(*�(*�(*����

14 15 ��������� ������������������UU�$ ����������LP(��!�������������� ������������������UU�$ ����������LP(��!�������������� ������������������UU�$ ����������LP(��!�������������� ������������������UU�$ ����������LP(��!�����

16 ������%.��"�����!�����$������%.��"�����!�����$������%.��"�����!�����$������%.��"�����!�����$�/0��/0��/0��/0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>����>>>>�/�/�/�/1111����

17 ���������������������������������/�����������LP�$UU(/�/�����������LP�$UU(/�/�����������LP�$UU(/�/�����������LP�$UU(/1�1�1�1�/����UU����//����UU����//����UU����//����UU����/1�1�1�1�$�$�$�$��������������UU��UU��UU��UU��������������(�(�(�(1111����

18 ������������������������������������/��/��/��/����UU����/��UU����/��UU����/��UU����/1�1�1�1�$�$�$�$��������������UU��UU��UU��UU��������������(�(�(�(1111����

19 ������������������������������������/����UU����//����UU����//����UU����//����UU����/1�1�1�1�$�$�$�$��������������UU��UU��UU��UU��������������(�(�(�(1111����

20 ���������������������������������/����UU����/�/����UU����/�/����UU����/�/����UU����/1�1�1�1�$�$�$�$��������������UU��UU��UU��UU��������������(�(�(�(�(*�(*�(*�(*����

21 22 ��������� ������������������T�$��������� ��AIE(��!�������������� ������������������T�$��������� ��AIE(��!�������������� ������������������T�$��������� ��AIE(��!�������������� ������������������T�$��������� ��AIE(��!�����

23 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�/0�/0�/0�/0�>>>>�0���0��0���0��0���0��0���0�>>>>�0����0����0����0���0�0�0�0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>����>>>>�/�/�/�/1111����

24 ������������������������������������/D�������� ��AIE�$T(//D�������� ��AIE�$T(//D�������� ��AIE�$T(//D�������� ��AIE�$T(/1�1�1�1�/����T����//����T����//����T����//����T����/1�1�1�1�$�$�$�$��������������T��T��T��T��������������(�(�(�(1111����

25 ������������������������������������/����T����//����T����//����T����//����T����/1�1�1�1�$�$�$�$��������������T��T��T��T��������������(�(�(�(1111����

26 ������������������������������������/����T����//����T����//����T����//����T����/1�1�1�1�$�$�$�$��������������T��T��T��T��������������(�(�(�(1111����

27 ������������������������������������/����T����//����T����//����T����//����T����/1�1�1�1�$�$�$�$��������������T��T��T��T��������������(�(�(�(�(*�(*�(*�(*����

28

Conditional AND truth table

Conditional OR truth table

Boolean logical AND truth table

Page 13: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

13

73

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

;��� �L!������;��� �L!������;��� �L!������;��� �L!������������������������������

(2 of 3)

Lines 30-35

Lines 38-43

Lines 46-47

29 ��������� ������������������U�$��������� ���� �����LP(��!�������������� ������������������U�$��������� ���� �����LP(��!�������������� ������������������U�$��������� ���� �����LP(��!�������������� ������������������U�$��������� ���� �����LP(��!�����

30 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�/0�/0�/0�/0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>����>>>>�/�/�/�/1111����

31 ���������������������������������/D�������� ���� �����LP�$U(/�/D�������� ���� �����LP�$U(/�/D�������� ���� �����LP�$U(/�/D�������� ���� �����LP�$U(/1111����

32 ������������������������������������/����U����//����U����//����U����//����U����/1�1�1�1�$�$�$�$��������������U��U��U��U��������������(�(�(�(1111����

33 ������������������������������������/����U����//����U����//����U����//����U����/1�1�1�1�$�$�$�$��������������U��U��U��U��������������(�(�(�(1111����

34 ������������������������������������/����U����//����U����//����U����//����U����/1�1�1�1�$�$�$�$��������������U��U��U��U��������������(�(�(�(1111����

35 ������������������������������������/����U����//����U����//����U����//����U����/1�1�1�1�$�$�$�$��������������U��U��U��U��������������(�(�(�(�(*�(*�(*�(*����

36 37 ��������� ������������������R�$��������� ��O �����LP(��!�������������� ������������������R�$��������� ��O �����LP(��!�������������� ������������������R�$��������� ��O �����LP(��!�������������� ������������������R�$��������� ��O �����LP(��!�����

38 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�/0�/0�/0�/0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>����>>>>�/�/�/�/1111����

39 ������������������������������������/D�������� ��O �����LP�$R(//D�������� ��O �����LP�$R(//D�������� ��O �����LP�$R(//D�������� ��O �����LP�$R(/1�1�1�1�����

40 ������������������������������������/����R����//����R����//����R����//����R����/1�1�1�1�$�$�$�$��������������R��R��R��R��������������(�(�(�(1111����

41 ������������������������������������/����R����//����R����//����R����//����R����/1�1�1�1�$�$�$�$��������������R��R��R��R��������������(�(�(�(1111����

42 ������������������������������������/����R����//����R����//����R����//����R����/1�1�1�1�$�$�$�$��������������R��R��R��R��������������(�(�(�(1111����

43 ������������������������������������/����/����/����/����R����/R����/R����/R����/1�1�1�1�$�$�$�$��������������R��R��R��R��������������(�(�(�(�(*�(*�(*�(*����

44 45 ��������� ������������������J�$��� ���������(��!�������������� ������������������J�$��� ���������(��!�������������� ������������������J�$��� ���������(��!�������������� ������������������J�$��� ���������(��!�����

46 ������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�������%.��"�����!�����$�/0�/0�/0�/0�>>>>�0���0��0���0��0���0��0���0�>>>>�0���0��0���0��0���0��0���0�>>>>�/�/�/�/1�1�1�1�/;��� ��ILF�$J(//;��� ��ILF�$J(//;��� ��ILF�$J(//;��� ��ILF�$J(/1111����

47 ������������������������������������/J���//J���//J���//J���/1�1�1�1�$�J$�J$�J$�J�������������(�(�(�(1�1�1�1�/J���//J���//J���//J���/1�1�1�1�$�J$�J$�J$�J�������������(�(�(�(�(*�(*�(*�(*����

48 ���3����3����3����3�������"���������"���������"���������"���

49 3�3�3�3������� ����;��� �L!������������ ����;��� �L!������������ ����;��� �L!������������ ����;��� �L!������

Boolean logical inclusive OR truth table

Boolean logical exclusive OR truth table

Logical negation truth table

74

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

;��� �L!������;��� �L!������;��� �L!������;��� �L!������������������������������

(3 of 3)

Program output

�����������AIE�$TT(�����������AIE�$TT(�����������AIE�$TT(�����������AIE�$TT(��������TT�������������TT�������������TT�������������TT�����������������TT�������������TT�������������TT�������������TT�����������������TT�������������TT�������������TT�������������TT�����������������TT�������������TT�������������TT�������������TT������������� �����������LP�$UU(�����������LP�$UU(�����������LP�$UU(�����������LP�$UU(��������UU�������������UU�������������UU�������������UU�����������������UU�������������UU�������������UU�������������UU�����������������UU�������������UU�������������UU�������������UU�����������������UU�������������UU�������������UU�������������UU������������� D�������� ��AIE�$T(D�������� ��AIE�$T(D�������� ��AIE�$T(D�������� ��AIE�$T(��������T�������������T�������������T�������������T�����������������������T�����������T�����������T�����������T�����������������T�������������T�������������T�������������T�����������������T�������������T�������������T�������������T������������� D�������� ���� �����LP�$U(D�������� ���� �����LP�$U(D�������� ���� �����LP�$U(D�������� ���� �����LP�$U(��������U�������������U�������������U�������������U�����������������U�������������U�������������U�������������U�����������������U�������������U�������������U�������������U�����������������U�������������U�������������U�������������U������������� D�������� ��O �����LP�$R(D�������� ��O �����LP�$R(D�������� ��O �����LP�$R(D�������� ��O �����LP�$R(��������R�������������R�������������R�������������R�����������������R�������������R�������������R�������������R�����������������R������������R������������R������������R��������������������R�������������R�������������R�������������R������������� ;��� ��ILF�$J(;��� ��ILF�$J(;��� ��ILF�$J(;��� ��ILF�$J(����J��������J��������J��������J������������J��������J��������J��������J������������

75

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.19 | Precedence/associativity of the operators discussed so far.

Operators Associativity Type 22��22��22��22�� ���� right to left unary postfix 22��22��22��22�� ���2������2������2������2��� ���������J�J�J�J���������$�$�$�$type((((���� right to left unary prefix B���B���B���B�������0����0����0����0���� left to right multiplicative 2���2���2���2��� ���� left to right additive ,��,��,��,���,)��=���=)�,)��=���=)�,)��=���=)�,)��=���=)���� left to right relational ))�))�))�))��J)�J)�J)�J)���� left to right equality TTTT���� left to right boolean logical AND RRRR���� left to right boolean logical exclusive OR UUUU���� left to right boolean logical inclusive OR TTTTTTTT���� left to right conditional AND UUUUUUUU���� left to right conditional OR X�X�X�X����� right to left conditional )���2)�)���2)�)���2)�)���2)����� )�)�)�)��B)���)��B)���)��B)���)��B)���)��0)�0)�0)�0)���� right to left assignment

76

1992-2007 Pearson Education, Inc. All rights reserved.

5.9 Structured Programming Summary

• Sequence structure– “built-in” to Java

• Selection structure– ��������, ���������������� and ���� ����� ����� ����� �

• Repetition structure– ������������, ������������������������ and ������������

77

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.20 | Java’s single-entry/single-exit sequence, selection and repetition statements.

78

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.21 | Rules for forming structured programs.

Rules for Forming Structured Programs

1 Begin with the simplest activity diagram (Fig. 5.22).

2 Any action state can be replaced by two action states in sequence.

3 Any action state can be replaced by any control statement (sequence of action states,

��������, ��������…����, ���� ����� ����� ����� �, ������������, ��������…������������ or ������������).

4 Rules 2 and 3 can be applied as often as you like and in any order.

Page 14: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

14

79

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.22 | Simplest activity diagram.

80

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.23 | Repeatedly applying the stacking rule (rule 2) of Fig. 5.21 to the simplest activity diagram.

81

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.24 | Repeatedly applying the nesting rule (rule 3) of Fig. 5.21 to the simplest activity diagram.

82

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.25 | “Unstructured” activity diagram.

83

1992-2007 Pearson Education, Inc. All rights reserved.

5.10 (Optional) GUI and Graphics Case Study: Drawing Rectangles and Ovals

• Draw rectangles– Method ����P �����P �����P �����P � of H��!�� �H��!�� �H��!�� �H��!�� �

• Draw ovals– Method ����L������L������L������L�� of H��!�� �H��!�� �H��!�� �H��!�� �

84

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

%��!������%��!������%��!������%��!������

(1 of 2)

1 ����������47��%��!����������������47��%��!����������������47��%��!����������������47��%��!������ 2 ���E"����������������������������!�����E"����������������������������!�����E"����������������������������!�����E"����������������������������!�� 3 �"!����"!����"!����"!�������������H��!�� �*����������H��!�� �*����������H��!�� �*����������H��!�� �*����

4 �"!����"!����"!����"!��������O�������QG��*�����O�������QG��*�����O�������QG��*�����O�������QG��*����

5 6 !��� !��� !��� !��� ���� ��� ��� ��� ����%��!���%��!���%��!���%��!��O����O����O����O�����QG���QG���QG���QG������

7 ####����

8 ������������!�����!�����!�����!���������������������� ��� *�� ��� *�� ��� *�� ��� *�������Y�� ��� ������� �����!��������������Y�� ��� ������� �����!��������������Y�� ��� ������� �����!��������������Y�� ��� ������� �����!�������� 9 ����������������

10 ��������������� ������ ��������������Y�� ��� ��� ������ ��������������Y�� ��� ��� ������ ��������������Y�� ��� ��� ������ ��������������Y�� ��� 11 ������������!��� !��� !��� !��� �%��!�$��%��!�$��%��!�$��%��!�$��������������������� �(�������� �(�������� �(�������� �(����

12 ���#���#���#���#����

13 ������ ��� �)�������� *������ ��� �)�������� *������ ��� �)�������� *������ ��� �)�������� *����

14 ���3����3����3����3�������%��!�� ������ ���������%��!�� ������ ���������%��!�� ������ ���������%��!�� ������ ��� 15 ����������������

16 ����������������������� �� ���������!��������������"������!���� ��������������� �� ���������!��������������"������!���� ��������������� �� ���������!��������������"������!���� ��������������� �� ���������!��������������"������!���� ���� 17 ������������!��� !��� !��� !��� ���������������������!������"!����$�H��!�� ����(�!������"!����$�H��!�� ����(�!������"!����$�H��!�� ����(�!������"!����$�H��!�� ����(����

18 ���#���#���#���#����

19 ��������������������������!���!���!���!��!������"!����$���(*�!������"!����$���(*�!������"!����$���(*�!������"!����$���(*����

20 ����������������������������

Page 15: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

15

85

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

%��!������%��!������%��!������%��!������

(2 of 2)

Lines 27-28

Lines 31-32

21 �������������������������������������$��$��$��$����������������)����)����)����)�----*���,�*���,�*���,�*���,��-�-�-�-*��22�(*��22�(*��22�(*��22�(����

22 ������#������#������#������#����

23 ���������������������������������������!� ��������!���������������Y�� ��� ���!� ��������!���������������Y�� ��� ���!� ��������!���������������Y�� ��� ���!� ��������!���������������Y�� ���

24 ���������������������������������������� ����� ����� ����� ��$� ��� �(�$� ��� �(�$� ��� �(�$� ��� �(����

25 ���������#���������#���������#���������#����

26 ������������������������������������������������ �� �� �� ��������������������������� �������������� �������������� �������������� �����

27 ������������������������������������������������������������������P �$�������P �$�������P �$�������P �$��-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-1�1�1�1��-�-�-�-�2���2���2���2���B��B��B��B��-�-�-�-1111��������

28 �������������������������������������������������������������������������-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-1�1�1�1��-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-�(*�(*�(*�(*������������������������������������

29 ����������������������������������������������������������������������������****����

30 ������������������������������������������������ �� �� �� ������4444��������������������������������������������������������

31 ������������������������������������������������������������������L��$�������L��$�������L��$�������L��$��-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-1�1�1�1��-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-1111��������

32 �������������������������������������������������������������������������-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-1�1�1�1��-�-�-�-�2���B��2���B��2���B��2���B��-�-�-�-�(*�(*�(*�(*������������������������������������

33 ����������������������������������������������������������������������������****����

34 ���������3����������3����������3����������3����������� ����������� ����������� ����������� �

35 ������3�������3�������3�������3�������������������������������������

36 ���3����3����3����3�������"�����!������"!����������"�����!������"!����������"�����!������"!����������"�����!������"!����

37 3�3�3�3������� ����%��!������� ����%��!������� ����%��!������� ����%��!�

Draw rectangles

Draw ovals

86

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

%��!�F��%��!�F��%��!�F��%��!�F����������������������

(1 of 2)

1 ����������48��%��!�F�����������������48��%��!�F�����������������48��%��!�F�����������������48��%��!�F�������

2 ���F����!!� ��������������!�.�� ����%��!�����F����!!� ��������������!�.�� ����%��!�����F����!!� ��������������!�.�� ����%��!�����F����!!� ��������������!�.�� ����%��!��

3 �"!����"!����"!����"!��������O�������Q���"*�����O�������Q���"*�����O�������Q���"*�����O�������Q���"*����

4 �"!����"!����"!����"!��������O�������QL!����G��*�����O�������QL!����G��*�����O�������QL!����G��*�����O�������QL!����G��*����

5 6 !��� !��� !��� !��� ���� ��� ��� ��� ����%��!�F���%��!�F���%��!�F���%��!�F������

7 ####����

8 ������������!��� !��� !��� !��� ��������� ����� ����� ����� ���������������������"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(�"���$�%����������&'�(����

9 ���#���#���#���#����

10 �������������������������������������Y�� ��� �������������Y�� ��� �������������Y�� ��� �������������Y�� ���

11 ������%��������!���)�QL!����G�������?�!��E����$������%��������!���)�QL!����G�������?�!��E����$������%��������!���)�QL!����G�������?�!��E����$������%��������!���)�QL!����G�������?�!��E����$����

12 ������������������������������������/K��������������� �����/K��������������� �����/K��������������� �����/K��������������� �����>>>>�/�/�/�/�2�2�2�2����

13 ������������������������������������/K����4�������������//K����4�������������//K����4�������������//K����4�������������/�(*�(*�(*�(*����

14 ����������������������������

15 ������������������������������������� ��� �)�?�����!���?��$���!���(*�� ��� �)�?�����!���?��$���!���(*�� ��� �)�?�����!���?��$���!���(*�� ��� �)�?�����!���?��$���!���(*���� ��������!������������ ��������!������������ ��������!������������ ��������!���������

16 ����������������������������

17 ��������������������������� �������!��������������Y����!����� �������!��������������Y����!����� �������!��������������Y����!����� �������!��������������Y����!��

18 ������%��!��!���)�������%��!��!���)�������%��!��!���)�������%��!��!���)����������%��!�$� ��� �(*�%��!�$� ��� �(*�%��!�$� ��� �(*�%��!�$� ��� �(*����

19 ����������������������������

20 ������Q���"��!!� ������)�������Q���"��!!� ������)�������Q���"��!!� ������)�������Q���"��!!� ������)����������Q���"$(*��Q���"$(*��Q���"$(*��Q���"$(*���� ����������Q���"��� ����������Q���"��� ����������Q���"��� ����������Q���"

21 22 �������!!� ��������E�������L!������$��������!!� ��������E�������L!������$��������!!� ��������E�������L!������$��������!!� ��������E�������L!������$�Q���"Q���"Q���"Q���"�KN?FZLIZ�;L%K�KN?FZLIZ�;L%K�KN?FZLIZ�;L%K�KN?FZLIZ�;L%K�(*�(*�(*�(*����

23 �������!!� ���������$�!���(*��������!!� ���������$�!���(*��������!!� ���������$�!���(*��������!!� ���������$�!���(*�����������!������������"����������!������������"����������!������������"����������!������������"

24 �������!!� ��������%�+$��������!!� ��������%�+$��������!!� ��������%�+$��������!!� ��������%�+$�5--5--5--5--1�1�1�1�5--5--5--5--�(*��(*��(*��(*������������������+�����������������+�����������������+�����������������+

25 �������!!� ��������[����$��������!!� ��������[����$��������!!� ��������[����$��������!!� ��������[����$��������������(*��(*��(*��(*���������������"��������������"��������������"��������������"

26 ���3����3����3����3�������"���������"���������"���������"���

27 3�3�3�3������� ��������� ��������� ��������� ����%��!�F���%��!�F���%��!�F���%��!�F��

87

1992-2007 Pearson Education, Inc. All rights reserved.

Outline

%��!�F��%��!�F��%��!�F��%��!�F����������������������

(2 of 2)

Program output

88

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.28 | Drawing concentric circles.

89

1992-2007 Pearson Education, Inc. All rights reserved.

5.11 (Optional) Software Engineering Case Study: Identifying Object’s State and Activities

• State Machine Diagrams– Commonly called state diagram– Model several states of an object– Show under what circumstances the object changes state– Focus on system behavior– UML representation

• State– Rounded rectangle

• Initial state– Solid circle

• Transitions– Arrows with stick arrowheads

90

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.29 | State diagram for the ATM object.

Page 16: Control Statements: Part 2kraemere/cse491/Notes/JavaHTP7e_05_6pp.pdf · Ó 1992-2007 Pearson Education, Inc. A l rig hts reserved. 7 – –

16

91

1992-2007 Pearson Education, Inc. All rights reserved.

Software Engineering Observation 5.5

Software designers do not generally create state diagrams showing every possible state and state transition for all attributes—there are simply too many of them. State diagrams typically show only key states and state transitions.

92

1992-2007 Pearson Education, Inc. All rights reserved.

5.11 (Optional) Software Engineering Case Study (Cont.)

• Activity Diagrams– Focus on system behavior– Model an object’s workflow during program execution– Model the actions the object will perform and in what

order– UML representation

• Action state ( rectangle with its left and right sides replaced by arcs curving outwards)

• Action order ( arrow with a stick arrowhead)• Initial state (solid circle)• Final state (solid circle enclosed in an open circle)

93

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.30 | Activity diagram for a BalanceInquiry object.

94

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.31 | Activity diagram for a withdrawal transaction.

95

1992-2007 Pearson Education, Inc. All rights reserved.

Fig. 5.32 | Activity diagram for a deposit transaction.