Operators, Expressions, Statements, Control Flow 7 January 2019 - - PowerPoint PPT Presentation

operators expressions statements control flow
SMART_READER_LITE
LIVE PREVIEW

Operators, Expressions, Statements, Control Flow 7 January 2019 - - PowerPoint PPT Presentation

Operators, Expressions, Statements, Control Flow 7 January 2019 OSU CSE 1 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


slide-1
SLIDE 1

Operators, Expressions, Statements, Control Flow

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Operators

  • An operator is a symbol (or combination
  • f 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

slide-3
SLIDE 3

Most Common Operators

7 January 2019 OSU CSE 3

String boolean char int double ! ++ -- + || + - + - && * / % * / == != < > <= >= == != < > <= >= == != < >

slide-4
SLIDE 4

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.

slide-5
SLIDE 5

Most Common Operators

7 January 2019 OSU CSE 5

String boolean char int double ! ++ -- + || + - + - && * / % * / == != < > <= >= == != < > <= >= == != < > Operators for

  • r (||) and

and (&&) use short- circuit evaluation.

slide-6
SLIDE 6

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.

slide-7
SLIDE 7

Most Common Operators

7 January 2019 OSU CSE 7

String boolean char int double ! ++ -- + || + - + - && * / % * / == != < > <= >= == != < > <= >= == != < > <= >= Best Practice: do not check doubles for equality; details later.

slide-8
SLIDE 8

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

slide-9
SLIDE 9

Some Expressions

  • Examples of code fragments that are

expressions:

i j + 7 "Hello" + " World!" keyboardIn.nextLine()

n == 0 new SimpleWriter1L()

7 January 2019 OSU CSE 9

slide-10
SLIDE 10

Some Expressions

  • Examples of code fragments that are

expressions:

i j + 7 "Hello" + " World!" keyboardIn.nextLine()

n == 0 new SimpleWriter1L()

7 January 2019 OSU CSE 10

What is the type of each of these expressions?

slide-11
SLIDE 11

Some Expressions

  • Examples of code fragments that are

expressions:

i j + 7 "Hello" + " World!" keyboardIn.nextLine()

n == 0 new 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.

slide-12
SLIDE 12

Statements

  • A statement is a “smallest complete unit
  • f execution” (roughly analogous to a

sentence in natural language)

  • A simple statement is terminated with a

semi-colon ';'

7 January 2019 OSU CSE 12

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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 as j = j + 7;

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Compound Statements/Blocks

  • Any sequence of zero or more statements

enclosed in {…} is a block

  • Example:

{ String s = in.nextLine();

  • ut.println ("s = " + s);

}

7 January 2019 OSU CSE 16

slide-17
SLIDE 17

Compound Statements/Blocks

  • Any sequence of zero or more statements

enclosed in {…} is a block

  • Example:

{ String s = in.nextLine();

  • ut.println ("s = " + s);

}

The scope of variable s is just the block in which it is declared.

7 January 2019 OSU CSE 17

slide-18
SLIDE 18

Compound Statements/Blocks

  • Any sequence of zero or more statements

enclosed in {…} is a block

  • Example:

{ String s = in.nextLine();

  • ut.println ("s = " + s);

}

There is no semi-colon after a block.

7 January 2019 OSU CSE 18

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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

slide-21
SLIDE 21

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 while loops, but you may use the

  • thers if you like.

7 January 2019 OSU CSE 21

slide-22
SLIDE 22

if Statement

test false true then_block

if (test) { then_block }

7 January 2019 OSU CSE 22

slide-23
SLIDE 23

if Statement

test false true then_block

if (test) { then_block }

Any boolean expression may go here.

7 January 2019 OSU CSE 23

slide-24
SLIDE 24

if Statement

test false 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

slide-25
SLIDE 25

if-else Statement

if (test) { then_block } else { else_block }

test false true then_block else_block

7 January 2019 OSU CSE 25

slide-26
SLIDE 26

if-else Statement

if (test) { then_block } else { else_block }

test false true then_block else_block Best Practice: even a single statement here should be in a block.

7 January 2019 OSU CSE 26

slide-27
SLIDE 27

if-else-if Statement

if (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

slide-28
SLIDE 28

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

slide-29
SLIDE 29

while Statement

test false true while_block

while (test) { while_block }

7 January 2019 OSU CSE 29

slide-30
SLIDE 30

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.

slide-31
SLIDE 31

if-else Statement

if (test) { then_block } else { else_block }

test false true 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”!

slide-32
SLIDE 32

Expressions and Statements

7 January 2019 OSU CSE 32

slide-33
SLIDE 33

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;

slide-34
SLIDE 34

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