cse 2221 - operators, expressions, statements, control...
TRANSCRIPT
![Page 1: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/1.jpg)
Operators, Expressions, Statements, Control Flow
7 January 2019 OSU CSE 1
![Page 2: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/2.jpg)
Operators• An operator is a symbol (or combination
of a couple symbols) that is used with variables and values to simplify how you write certain program expressions– Usually, operators are designed to mimic
mathematical notation—but do not be fooled into confusing programming and mathematics!
7 January 2019 OSU CSE 2
![Page 3: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/3.jpg)
Most Common Operators
7 January 2019 OSU CSE 3
String boolean char int double
! ++ --
+ || + - + -
&& * / % * /
== !=
< ><= >=== !=
< ><= >=== !=
< >
![Page 4: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/4.jpg)
Most Common Operators
7 January 2019 OSU CSE 4
String boolean char int double
! ++ --
+ || + - + -
&& * / % * /
== !=
< ><= >=== !=
< ><= >=== !=
< >
Best Practice: do not use == or !=
with Strings, but rather the equals
method;details later.
![Page 5: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/5.jpg)
Most Common Operators
7 January 2019 OSU CSE 5
String boolean char int double
! ++ --
+ || + - + -
&& * / % * /
== !=
< ><= >=== !=
< ><= >=== !=
< >
Operators foror (||) and
and (&&) use short-circuit evaluation.
![Page 6: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/6.jpg)
Most Common Operators
7 January 2019 OSU CSE 6
String boolean char int double
! ++ --
+ || + - + -
&& * / % * /
== !=
< ><= >=== !=
< ><= >=== !=
< >
Best Practice: be careful with the
remainder (%) operator: the second operand
must be positive; this is, unfortunately, not “clock
arithmetic”;details later.
![Page 7: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/7.jpg)
Most Common Operators
7 January 2019 OSU CSE 7
String boolean char int double
! ++ --
+ || + - + -
&& * / % * /
== !=
< ><= >=== !=
< ><= >=== !=
< ><= >=
Best Practice: do not check doubles
for equality;details later.
![Page 8: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/8.jpg)
Expressions• An expression is a “syntactically well-
formed and meaningful fragment” (roughly analogous to a word in natural language)
• Meaningful?– It has a value (of some type, of course)
7 January 2019 OSU CSE 8
![Page 9: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/9.jpg)
Some Expressions• Examples of code fragments that are
expressions:ij + 7"Hello" + " World!"keyboardIn.nextLine()n == 0new SimpleWriter1L()
7 January 2019 OSU CSE 9
![Page 10: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/10.jpg)
Some Expressions• Examples of code fragments that are
expressions:ij + 7"Hello" + " World!"keyboardIn.nextLine()n == 0new SimpleWriter1L()
7 January 2019 OSU CSE 10
What is the type of each of these expressions?
![Page 11: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/11.jpg)
Some Expressions• Examples of code fragments that are
expressions:ij + 7"Hello" + " World!"keyboardIn.nextLine()n == 0new SimpleWriter1L()
7 January 2019 OSU CSE 11
This fragment creates a new object of type
SimpleWriter1L, and its value is a reference
to that object;details later.
![Page 12: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/12.jpg)
Statements• A statement is a “smallest complete unit
of execution” (roughly analogous to a sentence in natural language)
• A simple statement is terminated with a semi-colon ';'
7 January 2019 OSU CSE 12
![Page 13: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/13.jpg)
Simple Statements• Some examples of simple statements:i = 12;j += 7;k++;SimpleWriter fileOut =
new SimpleWriter1L("foo.txt");
fileOut.print("Hi, Mr. Foo.");
7 January 2019 OSU CSE 13
![Page 14: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/14.jpg)
Simple Statements• Some examples of simple statements:i = 12;j += 7;k++;SimpleWriter fileOut =
new SimpleWriter1L("foo.txt");
fileOut.print("Hi, Mr. Foo.");
7 January 2019 OSU CSE 14
This is the same asj = j + 7;
![Page 15: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/15.jpg)
Assignment Statement
• Assignment statement form:variable = expression;
• Copies the value of the expression on the right side of the assignment operator =to the variable on the left side
• The = in Java code does not mean “equals” like in math!– Recall the tracing table earlier?
7 January 2019 OSU CSE 15
![Page 16: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/16.jpg)
Compound Statements/Blocks
• Any sequence of zero or more statements enclosed in {…} is a block
• Example:{
String s = in.nextLine();
out.println ("s = " + s);
}
7 January 2019 OSU CSE 16
![Page 17: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/17.jpg)
Compound Statements/Blocks
• Any sequence of zero or more statements enclosed in {…} is a block
• Example:{
String s = in.nextLine();
out.println ("s = " + s);
}
The scope of variable sis just the block in which
it is declared.
7 January 2019 OSU CSE 17
![Page 18: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/18.jpg)
Compound Statements/Blocks
• Any sequence of zero or more statements enclosed in {…} is a block
• Example:{
String s = in.nextLine();
out.println ("s = " + s);
}
There is no semi-colon after a block.
7 January 2019 OSU CSE 18
![Page 19: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/19.jpg)
Control Flow• Conditional or selection statements
– if– if-else– if-else-if– switch
• Loop or iteration statements– while– for– do-while
7 January 2019 OSU CSE 19
![Page 20: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/20.jpg)
Control Flow• Conditional or selection statements
– if– if-else– if-else-if– switch
• Loop or iteration statements– while– for– do-while
We will normally use these, but you may use a switch
statement if you like;details later.
7 January 2019 OSU CSE 20
![Page 21: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/21.jpg)
Control Flow• Conditional or selection statements
– if– if-else– if-else-if– switch
• Loop or iteration statements– while– for– do-while
We will normally use whileloops, but you may use the
others if you like.
7 January 2019 OSU CSE 21
![Page 22: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/22.jpg)
if Statement
testfalse
true
then_block
if (test) {
then_block
}
7 January 2019 OSU CSE 22
![Page 23: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/23.jpg)
if Statement
testfalse
true
then_block
if (test) {
then_block
}
Any boolean expression may go here.
7 January 2019 OSU CSE 23
![Page 24: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/24.jpg)
if Statement
testfalse
true
then_block
if (test) {
then_block
}
Best Practice: even a single statement here should be in a block.
7 January 2019 OSU CSE 24
![Page 25: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/25.jpg)
if-else Statement
if (test) {
then_block
} else {
else_block
}
test falsetrue
then_block else_block
7 January 2019 OSU CSE 25
![Page 26: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/26.jpg)
if-else Statement
if (test) {
then_block
} else {
else_block
}
test falsetrue
then_block else_block
Best Practice: even a single statement here should be in a block.
7 January 2019 OSU CSE 26
![Page 27: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/27.jpg)
if-else-if Statementif (test_1) {
then_block_1
} else if (test_2) {
then_block_2
} else {else_block
}
The else if part may be repeated.
7 January 2019 OSU CSE 27
![Page 28: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/28.jpg)
if-else-if Statement
Can you draw a flow-chart for this statement?
if (test_1) {
then_block_1
} else if (test_2) {
then_block_2
} else {else_block
}
7 January 2019 OSU CSE 28
![Page 29: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/29.jpg)
while Statement
test false
true
while_block
while (test) {
while_block
}
7 January 2019 OSU CSE 29
![Page 30: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/30.jpg)
while Statement
test false
true
while_block
while (test) {
while_block
}
7 January 2019 OSU CSE 30
Control flow here can go backward, which creates a
loop in the flow chart.
![Page 31: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/31.jpg)
if-else Statement
if (test) {
then_block
} else {
else_block
}
test falsetrue
then_block else_block
7 January 2019 OSU CSE 31
Control flow for if cannot go backward; there is no such thing as an “if loop”!
![Page 32: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/32.jpg)
Expressions and Statements
7 January 2019 OSU CSE 32
![Page 33: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/33.jpg)
Best Practices for boolean
7 January 2019 OSU CSE 33
If you want to say this, e.g., in an if or while
condition:Say this instead:
b == true b
b == false !b
if (b) {return true;
} else {return false;
}
return b;
![Page 34: CSE 2221 - Operators, Expressions, Statements, Control Flowweb.cse.ohio-state.edu/software/2221/web-sw1/extras/... · 2019-01-08 · Operators • An . operator. is a symbol (or combination](https://reader034.vdocuments.us/reader034/viewer/2022042114/5e9169c65bcb417e9c571a1e/html5/thumbnails/34.jpg)
Resources• Java for Everyone, Chapter 3• Java for Everyone, Chapter 4
– http://osu.worldcat.org/title/java-for-everyone-late-objects/oclc/808511232
7 January 2019 OSU CSE 34