Syntax if (condition) { //code } //rest of program
Syntax if (condition) { //code } else { //code }
What gets printed? i=10; j=5; z=2; i=10; j=5; z=2; if(i == 10) { if(i == 10) if(i > j) if(i > j) z *= 2; z *= 2; else } z *= 4; else { System.out.println(“z=“ + z); z *= 4; } System.out.println(“z=“ + z);
A) z=2 z=2
B) z=4 C) z=4 z=4 z=8
D)z=2 z=8
E)z=8 C.E.
if – else-if
if else-if syntax if (condition) { //code } else if (condition) { //code } else if (condition) { //code } else { //code }
Conditions • <, >, <=, >=, ==, != – Note: =>, =<, =! Will give compiler errors
• = is not the same as ==
Order of operations ()
Compute anything inside 1st
! pre ++ -- (e.g. ++var, --var)
Left to right
Type cast
Left to right
* / %
Left to right
+ -
Left to right
< <= > >=
Left to right
== !=
Left to right
&& ||
Left to right
=
assignment
post ++ -- (e.g. var++, var--)
Left to right
()
! pre ++ -- (e.g. ++var, --var) Type cast * / %
What’s the value of exp1?
+ < <= > >=
int a = int b = int c = boolean a) true
7; -1; 2; exp1 = !(a + b * c >= a + c * b); b) false
c) C.E.
d) False
== != && || = post ++ -- (e.g. var++, var--)
e) B and D are the same
Logical Operators a && b -- a AND b. both must be true a || b – a OR b. One or both must be true !a – NOT a. flips a
Which of these evaluates to true int i =10; char c=‘A’; a) i > 10 || i < 20 b) !(i > 10 && i < 20) c) i*7+c/2 > 34 || c == ‘A’ d) a and c e) All of the above
Lec 2
Compute anything inside 1st ! pre ++ -- (e.g. ++var, --var) Left to right. Type cast. Left to right. * / %. Left to right. + -. Left to right. < <= > >= Left to right. == != Left to right. && ||. Left to right. = assignment post ++ -- (e.g. var++, var--). Left to right. Page 14. What's the value of exp1? int a = 7; int b = -1; int c = 2; boolean exp1 = !(a + ...