week4 5 selection
TRANSCRIPT
-
8/12/2019 Week4 5 Selection
1/33
2003 Prentice Hall, Inc. All rights reserved.
1
Outline4.1 Introduction
4.2 Algorithms4.3 Pseudocode4.4 Control Structures4.5 if Single-Selection Statement4.6 if else Selection Statement4.7 Block Statement4.8 switch Multiple-Selection Statement4.9 Conditional And Operator4.10 Compound Assignment Operators
4.11 Increment and Decrement Operators4.12 Primitive Types
Chapter 4 - Control Structures: Part 1
-
8/12/2019 Week4 5 Selection
2/33
2003 Prentice Hall, Inc. All rights reserved.
2
4.1 Introduction
We learn about Control Structures
Structured-programming principle
Control structures help build and manipulate objects(Chapter 8)
-
8/12/2019 Week4 5 Selection
3/33
2003 Prentice Hall, Inc. All rights reserved.
3
4.2 Algorithms
Algorithm
Series of actions in specific order
The actions executed The order in which actions execute
Program control
Specifying the order in which actions execute Control structures help specify this order
-
8/12/2019 Week4 5 Selection
4/33
2003 Prentice Hall, Inc. All rights reserved.
4
4.3 Pseudocode
Pseudocode
Informal language for developing algorithms
Not executed on computers
Helps developers think out algorithms
-
8/12/2019 Week4 5 Selection
5/33
2003 Prentice Hall, Inc. All rights reserved.
5
4.4 Control Structures
Sequential execution
Program statements execute one after the other
Transfer of control Three control statements can specify order of statements
Sequence structure
Selection structure Repetition structure
Activity diagram
Models the workflow Action-state symbols
Transition arrows
-
8/12/2019 Week4 5 Selection
6/33
2003 Prentice Hall, Inc. All rights reserved.
6
Fig 4.1 Sequence structure activity diagram.
add grade to total
add 1 to counter
Corresponding Java statement:total = total + grade;
Corresponding Java statement:counter = counter + 1;
-
8/12/2019 Week4 5 Selection
7/33
2003 Prentice Hall, Inc. All rights reserved.
7
Java Keywords
abstract assert boolean break bytecase catch char class continuedefault do double else extendsfinal finally float for ifimplementsimport instanceofint interfacelong native new package privateprotected public return short staticstrictfp super switch synchronizedthisthrow throws transient try voidvolatile whileKeywords that are reserved, but not currently used
const gotoFig. 4.2 Java keywords.
-
8/12/2019 Week4 5 Selection
8/33
2003 Prentice Hall, Inc. All rights reserved.
8
4.4 Control Structures
Java has a sequence structure built-in
Java provides three selection structures if
Ifelse
switch
Java provides three repetition structures while dowhile
do
Each of these words is a Java keyword
-
8/12/2019 Week4 5 Selection
9/33
2003 Prentice Hall, Inc. All rights reserved.
9
4.5 if Single-Selection Statement
Single-entry/single-exit control structure
Perform action only when condition is true
Action/decision programming model
-
8/12/2019 Week4 5 Selection
10/33
2003 Prentice Hall, Inc. All rights reserved.
10
Fig 4.3 if single-selections statement activity diagram.
[grade >= 60]
[grade < 60]
print Passed
-
8/12/2019 Week4 5 Selection
11/33
2003 Prentice Hall, Inc. All rights reserved.
11
Example:
If student's grade is greater than or equal to 60
Print "Passed"
The preceding pseudocode If statement may be
written in Java as:
if ( studentGrade >= 60 )
System.out.println( "Passed" );
-
8/12/2019 Week4 5 Selection
12/33
2003 Prentice Hall, Inc. All rights reserved.
12
4.6 ifelse Selection Statement
Perform action only when condition is true
Perform different specified action when condition
is false
For example, the pseudocode statement
If student's grade is greater than or equal to 60
Print "Passed"
Else
Print "Failed"
-
8/12/2019 Week4 5 Selection
13/33
2003 Prentice Hall, Inc. All rights reserved.
13
Fig 4.4 ifelse double-selections statement activity diagram.
[grade >= 60][grade < 60]print Failed print Passed
-
8/12/2019 Week4 5 Selection
14/33
2003 Prentice Hall, Inc. All rights reserved.
14
4.6 ifelse Selection Statement
The preceding If...Else pseudocode statement can
be written in Java asif ( grade >= 60 )
System.out.println( "Passed" );
Else
System.out.println( "Failed" );
-
8/12/2019 Week4 5 Selection
15/33
2003 Prentice Hall, Inc. All rights reserved.
15
4.6 ifelse Selection Statement
Conditional operator (?:)
Can be used in place of an if...else statement
For example, the statement
System.out.println( studentGrade >= 60 ? "Passed"
: "Failed" );
-
8/12/2019 Week4 5 Selection
16/33
2003 Prentice Hall, Inc. All rights reserved.
16
4.6 Nested if...else Statements
A program can test multiple cases by placing
if...else statements inside other if...else statements
to create nested if...else statements.
For example, the following pseudocode represents
a nested if...else that :
prints A for exam grades greater than or equal to 81
B for grades in the range 70 to 79
C for grades in the range 60 to 69
D for grades in the range 50 to 59 E for all other grades
-
8/12/2019 Week4 5 Selection
17/33
2003 Prentice Hall, Inc. All rights reserved.
17
4.6 Nested if...else Statements
Pseudocode:If student's grade is greater than or equal to 81
Print "A"else
If student's grade is greater than or equal to 70Print "B"
else
If student's grade is greater than or equal to 60Print "C"else
If student's grade is greater than or equal to 50Print "D"
elsePrint E"
-
8/12/2019 Week4 5 Selection
18/33
2003 Prentice Hall, Inc. All rights reserved.
18
4.6 Nested if...else Statements
Most Java programmers prefer to write the precedingif...else statement as
if ( studentGrade >= 81 )System.out.println( "A" );
else if ( studentGrade >= 70 )
System.out.println( "B" );
else if ( studentGrade >= 60 )
System.out.println( "C" );
else if ( studentGrade >= 50 )
System.out.println( "D" );else
System.out.println( E" );
-
8/12/2019 Week4 5 Selection
19/33
2003 Prentice Hall, Inc. All rights reserved.
19
4.6 Nested if...else Statements
if ( x > 5 )
if ( y > 5 )
System.out.println( "x and y are > 5" );
else
System.out.println( "x is 5" );}
else
System.out.println( "x is
-
8/12/2019 Week4 5 Selection
20/33
2003 Prentice Hall, Inc. All rights reserved.
20
4.7 Block Statement
To include several statements in the body of an if
(or the body of an else for an if...else statement),
enclose the statements in braces ({ and }).
A set of statements contained within a pair of
braces is called a block.
A block can be placed anywhere in a program that
a single statement can be placed.
-
8/12/2019 Week4 5 Selection
21/33
2003 Prentice Hall, Inc. All rights reserved.
21
4.7 Block Statement
The following example includes a block in the else-
part of an if...else statement:
if ( grade >= 60 )
System.out.println( "Passed" );
else
{
System.out.println( "Failed" );
System.out.println( "You must take this course again." );
}
-
8/12/2019 Week4 5 Selection
22/33
2003 Prentice Hall, Inc. All rights reserved.
22
4.8 switch Multiple-Selection Statement
switch statement
Used for multiple selections
Perform different actions based on the possible values of an
integer variable or expression
Each action. is associated with the value of a constant
integral expression (i.e., a constant value of type byte,
short, int or char, but not long) that the variable or
expression on which the switch is based may assume
-
8/12/2019 Week4 5 Selection
23/33
2003 Prentice Hall, Inc. All rights reserved.
23
Fig. 4.5 switch multiple-selection statement activity diagram with break statements.
case a action(s) break
default action(s)
[true]
case b action(s) break
case z action(s) break
.
.
.
[false]
case a
[true]
[true]
case b
case z
[false]
[false]
-
8/12/2019 Week4 5 Selection
24/33
2003 Prentice Hall, Inc. All rights reserved.
24
4.8 switch Multiple-Selection Statement
switch ( nilai/10){
case 10:case 9:
case 8: huruf='A';break;
case 7:huruf='B';break;
case 6:huruf='C';break;
case 5:huruf='D';
break;default:huruf='E';break;
}
-
8/12/2019 Week4 5 Selection
25/33
2003 Prentice Hall, Inc. All rights reserved.
25
4.9 Conditional And Operator
if ( studentGrade >= 81 && studentGrade = 70 && studentGrade < 81 )
System.out.println( "B" );
else if ( studentGrade >= 60 && studentGrade < 70)
System.out.println( "C" );else if ( studentGrade >= 50 && studentGrade < 60 )
System.out.println( "D" );
elseSystem.out.println( E" );
26
-
8/12/2019 Week4 5 Selection
26/33
2003 Prentice Hall, Inc. All rights reserved.
26
4.10 Compound Assignment Operators
Assignment Operators
Abbreviate assignment expressions
Any statement of form variable = variable operator expression;
Can be written as
variable operator= expression;
e.g., addition assignment operator +=
c = c + 3
can be written as
c += 3
27
-
8/12/2019 Week4 5 Selection
27/33
2003 Prentice Hall, Inc. All rights reserved.
27
Assignmentoperator
Sampleexpression
Explanation Assigns
Assume:intc = 3,d = 5, e = 4, f
= 6, g = 12;+= c += 7 c = c + 7 10to c-= d -= 4 d = d - 4 1to d*= e *= 5 e = e * 5 20to e/= f /= 3 f = f / 3 2to f%=
g %= 9
g = g % 9
3to g
Fig. 4.12 Arithmet ic assignment operators.
28
-
8/12/2019 Week4 5 Selection
28/33
2003 Prentice Hall, Inc. All rights reserved.
28
4.11 Increment and Decrement Operators
Unary increment operator (++) Increment variables value by 1
Unary decrement operator (--) Decrement variables value by 1
Preincrement / predecrement operator
Post-increment / post-decrement operator
29
-
8/12/2019 Week4 5 Selection
29/33
2003 Prentice Hall, Inc. All rights reserved.
29
Operator Ca lled Sampleexpression
Explanation
++ preincrement ++a Increment aby 1, then use the newvalue of ain the expression inwhich aresides.
++ postincrement a++ Use the current value of ain theexpression in which aresides, thenincrement aby 1.
-- predecrement --b Decrement bby 1, then use the
new value of bin the expression inwhich bresides.-- postdecrement b-- Use the current value of bin the
expression in which bresides, thendecrement bby 1.
Fig. 4.13 The inc rement a nd dec rement opera tors.
-
8/12/2019 Week4 5 Selection
30/33
31
-
8/12/2019 Week4 5 Selection
31/33
2003 Prentice Hall, Inc. All rights reserved.
31
Operators Assoc iativity Type++ -- right to left unary postfix
++ -- + - (type) right to left unary* / % left to right multiplicative+ - left to right additive< >= left to right relational== != left to right equality?: right to left conditional= += -= *= /= %= right to left assignment
Fig. 4.15 Prec ed enc e a nd assoc ia tivity of the opera tors d isc ussed so fa r.
32
-
8/12/2019 Week4 5 Selection
32/33
2003 Prentice Hall, Inc. All rights reserved.
4.12 Primitive Types
Primitive types
building blocks for more complicated types
Java is strongly typed All variables in a Java program must have a type
Java primitive types
portable across computer platforms that support Java
33
-
8/12/2019 Week4 5 Selection
33/33
2003 Prentice Hall, Inc. All rights reserved.
Typ e Size in b its Va lues Sta nd ardboolean trueor false
[Note:The representation of a boolean is
specific to the Java Virtual Machine on each
computer platform.]char 16 '\u0000'to '\uFFFF'
(0 to 65535)
(ISO Unicode character set)
byte 8 128 to +127(2
7to 2
7 1)
short 16 32,768 to +32,767(2
15to 2
15 1)
int 32 2,147,483,648 to +2,147,483,647(2
31to 2
31 1)
long 64 9,223,372,036,854,775,808 to
+9,223,372,036,854,775,807(2
63to 2
63 1)
float 32 Negative range:3.4028234663852886E+38 to
1.40129846432481707e45
Positive range:
1.40129846432481707e45 to
3.4028234663852886E+38
(IEEE 754 floating point)
double 64 Negative range:1.7976931348623157E+308 to
4.94065645841246544e324
Positive range:
4.94065645841246544e324 to
1.7976931348623157E+308
(IEEE 754 floating point)
Fig. 4.16The Java primitive typ es.