cs 200 - programming i: branches€¦ · cs200-programmingi:branches marcrenault...
TRANSCRIPT
![Page 1: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/1.jpg)
CS 200 - Programming I: Branches
Marc Renault
Department of Computer SciencesUniversity of Wisconsin – Madison
Fall 2019TopHat Sec 3 (1:20 PM) Join Code: 682357TopHat Sec 4 (3:30 PM) Join Code: 296444
![Page 2: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/2.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Boolean Statements
![Page 3: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/3.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Booleans
Primitiveboolean b;
Values: true or falseWrapper class: Boolean
1/33
![Page 4: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/4.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Boolean Expressions
An expression that evaluates to a boolean. I.e. true or false.
2/33
![Page 5: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/5.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Boolean Expressions
An expression that evaluates to a boolean. I.e. true or false.
Comparison OperatorsBinary (expr1 oper expr2 ):
== : Equal to!= : Not equal to> : Greater than< : Less than>= : Greater than or equal<= : Less than or equal
2/33
![Page 6: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/6.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Boolean Expressions
An expression that evaluates to a boolean. I.e. true or false.
Logical OperatorsUnary (oper expr ):
! : Logical NOTBinary (expr1 oper expr2 ):
&& : Logical AND|| : Logical OR
Don’t confuse with the bitwise operators: &, |, and ˆ.
2/33
![Page 7: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/7.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Operator Precedence
Level Operator Description Associativity
higher ( <expression> ) grouping with parentheses left to right[ ] ( ) . array index, method call, mem-
ber access (dot operator)left to right
++ – post-increment, post-decrement left to right++ – + - ! pre-increment, unary plus/mi-
nus, logical negationright to left
(type) new casting and creating object right to left* / % multiplication, division, modu-
lusleft to right
+ - + addition, subtraction, concate-nation
left to right
< <= > >= instanceof relational and Java’s instanceofoperator
left to right
== != equality left to right&& conditional AND (short-
circuits)left to right
|| conditional OR (short-circuits) left to right
lower ? : ternary conditional right to left= += -= *= /= %= assignment, arithmetic (com-
pound) assignmentright to left
3/33
![Page 8: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/8.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0
0 0 0 1
0 1
0 1 1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 9: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/9.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0
0 0 1
0 1
0 1 1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 10: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/10.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0
0 1
0 1
0 1 1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 11: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/11.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0
1
0 1
0 1 1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 12: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/12.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1
0 1 1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 13: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/13.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0
1 1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 14: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/14.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1
1 1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 15: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/15.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1
1
1 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 16: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/16.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0
0 1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 17: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/17.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0
1 1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 18: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/18.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1
1 0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 19: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/19.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1
0
1 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 20: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/20.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1
1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 21: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/21.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1
1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 22: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/22.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1
0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 23: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/23.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0
0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 24: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/24.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Truth Tables
a b a ∧ b a ∨ b a⊕ b ¬a0 0 0 0 0 10 1 0 1 1 11 0 0 1 1 01 1 1 1 0 0
Logic Rules∧ AND (&&, &) – True only if both operands are true.∨ OR (||, |) – True if either operand is true.⊕ XOR (ˆ) – Exclusive OR; True if one (but not both)
operand(s) is true.¬ NOT (!) – Flips the truth value.
4/33
![Page 25: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/25.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 1
If A is 1 and B is 1, what is !A || B ?a. 1b. 0
5/33
![Page 26: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/26.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Bitwise OperationsPerforms the logical operations on each of the correspondingbits of the operands.
E.g. 7 & 5 = 5Consider the bits:
111& 101-----
101
Bitwise OperatorsBinary (expr1 oper expr2 ):
& – Bitwise AND| – Bitwise ORˆ – Bitwise XOR
6/33
![Page 27: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/27.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Bitwise OperationsPerforms the logical operations on each of the correspondingbits of the operands.E.g. 7 & 5 = 5Consider the bits:
111& 101-----
101
Bitwise OperatorsBinary (expr1 oper expr2 ):
& – Bitwise AND| – Bitwise ORˆ – Bitwise XOR
6/33
![Page 28: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/28.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 2
What is 7 | 5 ?a. 12b. 7c. 5d. 3
7/33
![Page 29: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/29.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Comparing References
![Page 30: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/30.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Reference ComparisonEquality Operator (==)
[Resp: Inequality Operator (!=)]
Generally, the equality operator compares the values oftwo variables.Recall: value of a reference type is the referral information.
Stack
ijk
6
6
Heap Integer i = 6;Integer j = 6;Integer k =
new Integer (6);
8/33
![Page 31: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/31.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Reference ComparisonEquality Operator (==)
[Resp: Inequality Operator (!=)]
Generally, the equality operator compares the values oftwo variables.Recall: value of a reference type is the referral information.
Stack
ijk
6
6
Heap Integer i = 6;Integer j = 6;Integer k =
new Integer (6);
8/33
![Page 32: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/32.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Reference ComparisonEquality Operator (==) [Resp: Inequality Operator (!=)]
Generally, the equality operator compares the values oftwo variables.Recall: value of a reference type is the referral information.
Stack
ijk
6
6
Heap Integer i = 6;Integer j = 6;Integer k =
new Integer (6);
8/33
![Page 33: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/33.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 3
What is the output?
Integer i = 5;Integer j = 5;Integer k = new Integer (5);
System.out.print(i == j);System.out.print(j == k);
9/33
![Page 34: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/34.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Comparing Values of Primitive WrappersInteger i = 6;Integer j = 5;Integer k = new Integer (6);int m = 5;
Instance Methods for Comparisonequals(otherObj) – True if values of the objects are equal.False otherwise.E.g. i.equals(k) // true
compareTo(otherObj) – 0 if equal, negative if less thanotherObj, and positive if greater than otherObj.E.g. i.compareTo(j) // +ve
Other Comparison Operators(In)Equality exception: j == m // true
Other operators work as expected: <, <=, >, >=.
10/33
![Page 35: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/35.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Comparing Values of Primitive WrappersInteger i = 6;Integer j = 5;Integer k = new Integer (6);int m = 5;
Instance Methods for Comparisonequals(otherObj) – True if values of the objects are equal.False otherwise.E.g. i.equals(k) // true
compareTo(otherObj) – 0 if equal, negative if less thanotherObj, and positive if greater than otherObj.E.g. i.compareTo(j) // +ve
Other Comparison Operators(In)Equality exception: j == m // true
Other operators work as expected: <, <=, >, >=.
10/33
![Page 36: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/36.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Comparing Values of Primitive WrappersInteger i = 6;Integer j = 5;Integer k = new Integer (6);int m = 5;
Instance Methods for Comparisonequals(otherObj) – True if values of the objects are equal.False otherwise.E.g. i.equals(k) // true
compareTo(otherObj) – 0 if equal, negative if less thanotherObj, and positive if greater than otherObj.E.g. i.compareTo(j) // +ve
Other Comparison Operators(In)Equality exception: j == m // true
Other operators work as expected: <, <=, >, >=.10/33
![Page 37: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/37.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 4
What is the output?
Scanner sc = new Scanner("foo");String s = "foo";String t = sc.next ();System.out.print(s == t);
11/33
![Page 38: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/38.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
String ComparisonOnly Instance Methods
equals(otherStr)
compareTo(otherStr)
equalsIgnoreCase(otherStr)
compareToIgnoreCase(otherStr)
Lexicographical ComparisonStarting from index 0 and compare each pair of characters atindex x according to the following rules:
1 The character with the smaller Unicode value is consideredsmaller lexicograpically.
2 No character is smaller than an existing character. (E.g."foo" is smaller than "foobar")
12/33
![Page 39: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/39.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
String ComparisonOnly Instance Methods
equals(otherStr)
compareTo(otherStr)
equalsIgnoreCase(otherStr)
compareToIgnoreCase(otherStr)
Lexicographical ComparisonStarting from index 0 and compare each pair of characters atindex x according to the following rules:
1 The character with the smaller Unicode value is consideredsmaller lexicograpically.
2 No character is smaller than an existing character. (E.g."foo" is smaller than "foobar")
12/33
![Page 40: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/40.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
String ComparisonOnly Instance Methods
equals(otherStr)
compareTo(otherStr)
equalsIgnoreCase(otherStr)
compareToIgnoreCase(otherStr)
Lexicographical ComparisonStarting from index 0 and compare each pair of characters atindex x according to the following rules:
1 The character with the smaller Unicode value is consideredsmaller lexicograpically.
2 No character is smaller than an existing character. (E.g."foo" is smaller than "foobar")
12/33
![Page 41: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/41.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 5
Arrange these words in lexicographical order (smallest tolargest):
a. Javascriptb. Cc. Javad. C++e. C#
13/33
![Page 42: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/42.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Conditional Statements
![Page 43: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/43.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If Statement
if (cond) {trueStatement;
.
.
.lastTrueStatement;
}
Control Flow
cond
True Stmt Block
Followingstatements
truefalse
14/33
![Page 44: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/44.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If Statement
if (cond) {trueStatement;
.
.
.lastTrueStatement;
}
Control Flow
cond
True Stmt Block
Followingstatements
truefalse
14/33
![Page 45: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/45.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else Statement
if (cond) {trueStatement;
.
.
.lastTrueStatement;
}else {
falseStatement;...
lastFalseStatement;}
Control Flow
cond
TrueStmtBlock
FalseStmtBlock
Followingstatements
true false
15/33
![Page 46: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/46.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else Statement
if (cond) {trueStatement;
.
.
.lastTrueStatement;
}else {
falseStatement;...
lastFalseStatement;}
Control Flow
cond
TrueStmtBlock
FalseStmtBlock
Followingstatements
true false
15/33
![Page 47: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/47.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 6
What is printed out in be following code block:
boolean toBe = false;if(toBe || ! toBe) {
System.out.print("Tautology");}else {
System.out.print("Contradiction");}
16/33
![Page 48: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/48.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 7
What is the boolean expression to complete the followingcode?Assume that i is an int variable. Replace ?????? in the codewith the appropriate boolean expression.if( ?????? ) {
System.out.println(i + " is divisible by 3");}else {
System.out.println(i + " is not divisible by 3");}
17/33
![Page 49: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/49.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 8
What is the output?
int i = 3;if ((i = 5) > 4) {
System.out.print("a");} else {
System.out.print("b");}
18/33
![Page 50: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/50.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else If StatementEnding Else
if (cond1) {trueStatements1;
}else if (cond2) {
trueStatements2;}...
else {allFalseStatements;
}
Control Flow
cond1True 1StmtBlock
cond2True 2StmtBlock
Else StmtBlock
Followingstatements
All false
true
truefalse
19/33
![Page 51: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/51.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else If StatementEnding Else
if (cond1) {trueStatements1;
}else if (cond2) {
trueStatements2;}...
else {allFalseStatements;
}
Control Flow
cond1True 1StmtBlock
cond2True 2StmtBlock
Else StmtBlock
Followingstatements
All false
true
truefalse
19/33
![Page 52: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/52.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else If StatementNo Ending Else
if (cond1) {trueStatements1;
}else if (cond2) {
trueStatements2;}...
else if (condN) {trueStatementsN;
}
Control Flow
cond1True 1StmtBlock
cond2True 2StmtBlock
condNTrue NStmtBlock
Followingstatements
true
true
true
false
false
20/33
![Page 53: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/53.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else If StatementNo Ending Else
if (cond1) {trueStatements1;
}else if (cond2) {
trueStatements2;}...
else if (condN) {trueStatementsN;
}
Control Flow
cond1True 1StmtBlock
cond2True 2StmtBlock
condNTrue NStmtBlock
Followingstatements
true
true
true
false
false
20/33
![Page 54: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/54.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Multiple Ifs
if (cond1) {trueStatements1;
}if (cond2) {
trueStatements2;}...
if (condN) {trueStatementsN;
}
Control Flow
cond1True 1StmtBlock
cond2True 2StmtBlock
condNTrue NStmtBlock
Followingstatements
true
true
true
false
false
21/33
![Page 55: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/55.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Multiple Ifs
if (cond1) {trueStatements1;
}if (cond2) {
trueStatements2;}...
if (condN) {trueStatementsN;
}
Control Flow
cond1True 1StmtBlock
cond2True 2StmtBlock
condNTrue NStmtBlock
Followingstatements
true
true
true
false
false
21/33
![Page 56: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/56.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else If vs Multiple IfsControl Flow – If-Else If
cond1True 1StmtBlock
cond2True 2StmtBlock
condNTrue NStmtBlock
Followingstatements
true
true
true
false
false
Control Flow – Multiple Ifs
cond1True 1StmtBlock
cond2True 2StmtBlock
condNTrue NStmtBlock
Followingstatements
true
true
true
false
false
22/33
![Page 57: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/57.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 9
What does the following code print?
int v1 = 3, v2 = 6;if (v1 + 2 < v2) System.out.print(" tic ");else if (v1 + 4 < v2) System.out.print(" tac ");else if (v1 + 4 > v2) System.out.print(" toe ");
23/33
![Page 58: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/58.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 10
What does the following code print?
int v1 = 3, v2 = 6;if (v1 + 2 < v2) System.out.print(" tic ");if (v1 + 4 < v2) System.out.print(" tac ");if (v1 + 4 > v2) System.out.print(" toe ");
24/33
![Page 59: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/59.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Switch Statements
Control flow depends onbreak;
switch (expr) {case const1:
case1Statements;break;
case const2:case2Statements;break;
.
.
.default:
defStatements;break;
}
Control Flow
expr == const1Case 1StmtBlock
expr == const2Case 2StmtBlock
DefaultBlock
Followingstatements
No match
true
true
false
25/33
![Page 60: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/60.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Switch Statements
Control flow depends onbreak;
switch (expr) {case const1:
case1Statements;break;
case const2:case2Statements;break;
.
.
.default:
defStatements;break;
}
Control Flow
expr == const1Case 1StmtBlock
expr == const2Case 2StmtBlock
DefaultBlock
Followingstatements
No match
true
true
false
25/33
![Page 61: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/61.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Switch StatementsControl flow depends onbreak;
switch (expr) {case const1:
case1Statements;break;
case const2:case2Statements;break;
.
.
.default:
defStatements;break;
}
Control Flow
expr == const1Case 1StmtBlock
expr == const2Case 2StmtBlock
DefaultBlock
Followingstatements
No match
true
true
false
25/33
![Page 62: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/62.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Switch StatementsControl flow depends onbreak;
switch (expr) {case const1:
case1Statements;
case const2:case2Statements;break;
.
.
.default:
defStatements;break;
}
Control Flow
expr == const1Case 1StmtBlock
expr == const2Case 2StmtBlock
DefaultBlock
Followingstatements
No match
true
true
falseno break;
25/33
![Page 63: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/63.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Switch Statements Keys
RulesFinds the firstmatch going from the top to the bottom inorder.Fall through – A case without a breakwill fall through tothe next case’s statements.
Good PracticesOnly use fall though when the cases execute the same code.Generally, it is best to always have a default case at theend.
26/33
![Page 64: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/64.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Switch Statements Keys
RulesFinds the firstmatch going from the top to the bottom inorder.Fall through – A case without a breakwill fall through tothe next case’s statements.
Good PracticesOnly use fall though when the cases execute the same code.Generally, it is best to always have a default case at theend.
26/33
![Page 65: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/65.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else if and Switch Statementsif (i == 1) {
System.out.println("one");}else if (i == 2) {
System.out.println("two");}else {
System.out.println("Not 1 or 2");}
switch (i) {case 1:
System.out.println("one");break;
case 2:System.out.println("two");break;
default:System.out.println("Not 1 or 2");break;
}
27/33
![Page 66: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/66.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
If-Else if and Switch Statementsif (i == 1) {
System.out.println("one");}else if (i == 2) {
System.out.println("two");}else {
System.out.println("Not 1 or 2");}
switch (i) {case 1:
System.out.println("one");break;
case 2:System.out.println("two");break;
default:System.out.println("Not 1 or 2");break;
} 27/33
![Page 67: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/67.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 11What does the following code print?int month = 8;switch (month) {
case 1: System.out.print("January");break;
case 2: System.out.print("February");break;
case 3: System.out.print("March");break;
case 4: System.out.print("April");break;
case 5: System.out.print("May");break;
case 6: System.out.print("June");break;
case 7: System.out.print("July");break;
case 8: System.out.print("August");break;
case 9: System.out.print("September");break;
case 10: System.out.print("October");break;
case 11: System.out.print("November");break;
case 12: System.out.print("December");break;
default: System.out.print("Invalid month");break;
} 28/33
![Page 68: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/68.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
TopHat Question 12What does the following code print?
int month = 8;switch (month) {
case 1: System.out.print("January");case 2: System.out.print("February");case 3: System.out.print("March");case 4: System.out.print("April");case 5: System.out.print("May");case 6: System.out.print("June");case 7: System.out.print("July");case 8: System.out.print("August");case 9: System.out.print("September");case 10: System.out.print("October");case 11: System.out.print("November");case 12: System.out.print("December");
break;default: System.out.print("Invalid month");
break;
29/33
![Page 69: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/69.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Ordinal Abbreviation Exercise
Write a method that takes an integer as input and returns astring with the integer converted to an abbreviated ordinal. I.e.1 becomes 1st, 2 becomes 2nd, 3 becomes, 3rd, etc...
30/33
![Page 70: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/70.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Using Eclipse
![Page 71: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/71.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
EclipseEclipse
Integrated development environment (IDE) that we willuse in the course.In 2017, 40.5% use Eclipse (48% use IntelliJ) a
In 2016, 48.2% use Eclipse (43.6% use IntelliJ) b
aSource: http://www.baeldung.com/java-in-2017bSource: http://www.baeldung.com/java-ides-2016
Installing and UsingInstallation:https://cs200-www.cs.wisc.edu/wp/install-eclipse/
Tutorials: https://cs200-www.cs.wisc.edu/wp/resources/eclipse-tutorial/
31/33
![Page 72: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/72.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
EclipseEclipse
Integrated development environment (IDE) that we willuse in the course.In 2017, 40.5% use Eclipse (48% use IntelliJ) a
In 2016, 48.2% use Eclipse (43.6% use IntelliJ) b
aSource: http://www.baeldung.com/java-in-2017bSource: http://www.baeldung.com/java-ides-2016
Installing and UsingInstallation:https://cs200-www.cs.wisc.edu/wp/install-eclipse/
Tutorials: https://cs200-www.cs.wisc.edu/wp/resources/eclipse-tutorial/
31/33
![Page 73: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/73.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Style Guidehttps://cs200-www.cs.wisc.edu/wp/style-guide/Examples:
http://pages.cs.wisc.edu/~cs200/resources/Frame.java
http://pages.cs.wisc.edu/~cs200/resources/Circle.java
Key ElementsFile Comment HeaderClass Comment HeaderMethod Comment HeaderGood names with proper formattingProper use of whitespace
32/33
![Page 74: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/74.jpg)
Boolean Statements Comparing References Conditional Statements Using Eclipse
Further Reading
COMP SCI 200: Programming IzyBooks.com, 2015.zyBook code:WISCCOMPSCI200Fall2019
Chapter 4. Branches
33/33
![Page 75: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/75.jpg)
Appendix References
Appendix
![Page 76: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/76.jpg)
Appendix References
References
![Page 77: CS 200 - Programming I: Branches€¦ · CS200-ProgrammingI:Branches MarcRenault DepartmentofComputerSciences UniversityofWisconsin–Madison Fall2019 TopHatSec3(1:20PM)JoinCode:682357](https://reader034.vdocuments.mx/reader034/viewer/2022042221/5ec74560a8b2df7f2d565012/html5/thumbnails/77.jpg)
Appendix References
Image Sources I
https://brand.wisc.edu/web/logos/
http://www.zybooks.com/
34/33