Operator Precedence - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

Operator Precedence

Description:

. Z M ? false. true. M X. 106. Translation. if ( x y ) if ... class Sum1ToN. public static void main (String args[ ]) throws IOException // read the input ... – PowerPoint PPT presentation

Number of Views:99
Avg rating:3.0/5.0
Slides: 7
Provided by: alanwi8
Category:

less

Transcript and Presenter's Notes

Title: Operator Precedence


1
Operator Precedence
  • What is the order of evaluation in the following
    expressions?

a b c d e
a b c - d / e
1
2
3
4
3
1
4
2
a / (b c) - d e
2
1
4
3
a / (b (c (d - e)))
4
3
2
1
88
2
Example
  • Givens X, Y, Z (three numbers)
  • Result M (the largest given value)
  • Header M?Max3( X, Y, Z )
  • Two solutions
  • sequence of branch structures
  • nested branch structures
  • Translate the latter into Java

m x if ( y gt m ) m y else //
do nothing if ( z gt m ) m z else
// do nothing
M ? Y
Ø
Z gt M ?
M ? Z
Ø
106
3
Translation
if ( x gt y ) if ( x gt z ) m x
else m z else if
( y gt z ) m y else
m z
108
4
Translation
a 0 b 0 if ( x gt 0 ) a 5 b a
1 else if ( y gt x ) a 4
b 0 else // do nothing

111
5
Translate to Program
  • import java.io.
  • class Sum1ToN
  • public static void main (String args )
    throws IOException
  • // read the input
  • System.out.println("Enter a positive
    integer N.")
  • int n CSI1100.readInt( )
  • // initialize counter and accumulator sum
  • int count 1
  • int sum 0
  • // loop
  • while (count lt n)
  • sum sum count
  • count count 1
  • // output the result

114
6
FOR loop to add 1 to N
Sum ? 0
Count ? 1
Count ? N ?
true
Sum ? Sum Count
false
Count ? Count 1
  • Translate

sum 0 for ( count 1 count lt n count
count 1 ) sum sum count
117
Write a Comment
User Comments (0)
About PowerShow.com