simple java - stanford universityweb.stanford.edu/.../lectures/6-simplejava/6-simplejava.pdf ·...
TRANSCRIPT
Piech,CS106A,StanfordUniversity
• Operations on numerical types• Operations:
+ “addition”- “subtraction”* “multiplication”/ “division” (different for int vs. double)% “remainder”
• Precedence (in order):() highest*, /, %+, - lowest
Operators in same precedence category evaluated left to right
Review: Operations
Piech,CS106A,StanfordUniversity
int x = 3;
x = x + 1; x += 1; x++;
x = x + 5; x += 5;
x = x – 1; x -= 1; x--;
x = x * 3; x *= 3;
x = x / 2; x /= 2;
Expressions Short Hand
Piech,CS106A,StanfordUniversity
• Boolean expression is just a test for a condition• Essentially, evaluates to true or false
• Value comparisons:== “equals” (note: not single =)!= “not equals” (cannot say <>)> “greater than”< “less than”>= “greater than or equal to”<= “less than or equal to”
Review: Boolean Expressions
Piech,CS106A,StanfordUniversity
Today’sGoal1. How to use constants
2. Basics of boolean variables3. Understand For loops4. Know variable scope
Piech,CS106A,StanfordUniversity
Value comparisons (in order of precidence):! “not”!p
&& “and”p && q
|| “or”p || q
Boolean Expressions
Ifpistruethen!pisfalse(andviceversa)
Evaluatestotrueifbothsidesaretrue
Evaluatestotrueifeitherporq(orboth)aretrue
boolean p = (x != 1) || (x != 2);boolean p = (x != 1) && (X != 2);
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Xwaslookingforlove!
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Xwaslookingforlove!
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
x was definitelylooking for love
Xwaslookingforlove!
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5 Hi, I’m y
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5Wow
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5 We have so much
in common
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5 We both have
value 5!
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5 Maybe one day
we can…
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5 println together?
Andmety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5
Theygotalong
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5
TragedyStruck
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
y5
TragedyStruck
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
TragedyStruck
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Whenaprogramexitsacodeblock…
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Allvariablesdeclaredinsidethatblock..
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Getdeletedfrommemory!
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Sinceywasdeclaredintheif-block
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Itgetsdeletedfrommemoryhere
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Anddoesn’texisthere
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;
}println(x + y);
x5
Error. Undefined variable y.
Anddoesn’texisthere
Piech,CS106A,StanfordUniversity
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}
Variableshavealifetime(calledscope)
Piech,CS106A,StanfordUniversity
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}
Variableshavealifetime(calledscope)
Piech,CS106A,StanfordUniversity
v
Comestolifehere
8
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}
Vars cometoexistencewhendeclared
Piech,CS106A,StanfordUniversity
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}
Thisistheinnermostcodeblockinwhichitwasdeclared….
v4
Liveuntilendoftheircodeblock
Piech,CS106A,StanfordUniversity
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}
Stillalivehere…
v4
Liveuntilendoftheircodeblock
Piech,CS106A,StanfordUniversity
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}v4
Liveuntilendoftheircodeblock
Itdieshere(attheendofitscodeblock)
Piech,CS106A,StanfordUniversity
public void run(){double v = 8;if(condition){
v = 4;…somecode
}…someothercode
}Itdieshere(attheendofitscodeblock)
Liveuntilendoftheircodeblock
Piech,CS106A,StanfordUniversity
public void run(){…somecodeif(condition){
int w = 4;…somecode
}…someothercode
}
Example2
Thisisthescopeofw
Piech,CS106A,StanfordUniversity
public void run(){…somecodeif(condition){
int w = 4;…somecode
}…someothercode
}
wdieshere(attheendofitscodeblock)
Example2
wcomestolifehere
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;println(x + y);
}
x5
x waslookingforlove!
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;println(x + y);
}
x5
x was definitelylooking for love
x waslookingforlove…
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;println(x + y);
}
x5
y5
x mety
Piech,CS106A,StanfordUniversity
int x = 5;if(lookingForLove()) {int y = 5;println(x + y);
}
x5
y5
Sincetheywereboth“inscope”
Piech,CS106A,StanfordUniversity
• The scope ofavariablereferstothesectionofcodewhereavariablecanbeaccessed.
• Scopestartswherethevariableisdeclared.• Scopeendsattheterminationoftheinner-most
codeblockinwhichthevariablewasdefined.
• Acodeblockisachunkofcodebetween{}brackets
Scope Formally
Piech,CS106A,StanfordUniversity
Choose a Doorint door = readInt("Door: ");// while the input is invalidwhile(door < 1 || door > 3) {
// tell the user the input was invalidprintln("Invalid door!");// ask for a new inputdoor = readInt("Door: ");
}
|| or&& and
Piech,CS106A,StanfordUniversity
int prize = 3;if(door == 1) {
prize = 2 + 9 / 10 * 100;} else if(door == 2) {
boolean locked = prize % 2 != 1;if(!locked) {
prize += 7;}
} else if(door == 3) {prize++;
}
The Door Logic
Piech,CS106A,StanfordUniversity
println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);println(“Nick rocks socks!”);
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 100; i++) {println(“Nick rocks socks!”);
}
Enters the loop if this condition
passes
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
For Loop Redux
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
i 0
For Loop Redux
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
i 0
For Loop Redux
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
i 0
For Loop Redux
Nick rocks socks
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
i 0
For Loop Redux
Nick rocks socks
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
i 1
For Loop Redux
Nick rocks socks
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
i 1
For Loop Redux
Nick rocks socks
Piech,CS106A,StanfordUniversity
ForLoopRedux
for(int i = 0; i < 3; i++) {println(“Nick rocks socks!”);
}
For Loop Redux
Nick rocks socksNick rocks socks
Nick rocks socks
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < NUM_NUMS; i++) {println(i * 2);
}
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
For Loop Redux
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
For Loop Redux
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 0
For Loop Redux
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 0
For Loop Redux
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 0
For Loop Redux
0
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 1
For Loop Redux
0
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 1
For Loop Redux
0
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 1
For Loop Redux
0
2
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 2
For Loop Redux
0
2
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 2
For Loop Redux
0
2
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 2
For Loop Redux
0
2
4
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 3
For Loop Redux
0
2
4
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 3
For Loop Redux
0
2
4
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
i 3
For Loop Redux
0
2
4
Piech,CS106A,StanfordUniversity
PrintingEvenNumbers
for(int i = 0; i < 3; i++) {println(i * 2);
}
For Loop Redux
0
2
4