operators expressions statements control flow
play

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


  1. Operators, Expressions, Statements, Control Flow 7 January 2019 OSU CSE 1

  2. 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

  3. Most Common Operators String boolean char int double ! ++ -- + || + - + - && * / % * / < > < > < > <= >= <= >= == != == != == != 7 January 2019 OSU CSE 3

  4. Most Common Operators String boolean char int double ! ++ -- Best Practice : do not use == or != + || + - + - with String s, but rather the equals && * / % * / method; details later. < > < > < > <= >= <= >= == != == != == != 7 January 2019 OSU CSE 4

  5. Most Common Operators String boolean char int double Operators for ! ++ -- or ( || ) and and ( && ) use short- + || + - + - circuit evaluation . && * / % * / < > < > < > <= >= <= >= == != == != == != 7 January 2019 OSU CSE 5

  6. Most Common Operators 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. < > < > < > <= >= <= >= == != == != == != 7 January 2019 OSU CSE 6

  7. Most Common Operators String boolean char int double ! ++ -- Best Practice : do not check double s + || + - + - for equality; details later. && * / % * / < > < > < > <= >= <= >= <= >= == != == != == != 7 January 2019 OSU CSE 7

  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

  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

  10. Some Expressions • Examples of code fragments that are expressions: i What is the type of j + 7 each of these expressions? "Hello" + " World!" keyboardIn.nextLine() n == 0 new SimpleWriter1L() 7 January 2019 OSU CSE 10

  11. Some Expressions • Examples of code fragments that are expressions: This fragment creates a i new object of type j + 7 SimpleWriter1L , and "Hello" + " World!" its value is a reference to that object; keyboardIn.nextLine() details later. n == 0 new SimpleWriter1L() 7 January 2019 OSU CSE 11

  12. 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

  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

  14. Simple Statements • Some examples of simple statements: i = 12; This is the same as j += 7; j = j + 7; k++; SimpleWriter fileOut = new SimpleWriter1L("foo.txt"); fileOut.print("Hi, Mr. Foo."); 7 January 2019 OSU CSE 14

  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

  16. 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

  17. Compound Statements/Blocks • Any sequence of zero or more statements enclosed in {…} is a block The scope of variable s • Example: is just the block in which { it is declared. String s = in.nextLine(); out.println ("s = " + s); } 7 January 2019 OSU CSE 17

  18. Compound Statements/Blocks • Any sequence of zero or more statements enclosed in {…} is a block • Example: There is no semi-colon after a block. { String s = in.nextLine(); out.println ("s = " + s); } 7 January 2019 OSU CSE 18

  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

  20. Control Flow • Conditional or selection statements – if We will normally use these, but you may use a switch – if-else statement if you like; – if-else-if details later. – switch • Loop or iteration statements – while – for – do-while 7 January 2019 OSU CSE 20

  21. Control Flow • Conditional or selection statements – if – if-else – if-else-if – switch • Loop or iteration statements – while We will normally use while – for loops, but you may use the others if you like. – do-while 7 January 2019 OSU CSE 21

  22. if Statement false if (test) { test then_block true } then_block 7 January 2019 OSU CSE 22

  23. if Statement Any boolean expression may go here. false if (test) { test then_block true } then_block 7 January 2019 OSU CSE 23

  24. if Statement Best Practice : even a single statement here should be in a block. false if (test) { test then_block true } then_block 7 January 2019 OSU CSE 24

  25. if-else Statement if (test) { then_block true false test } else { else_block } then_block else_block 7 January 2019 OSU CSE 25

  26. if-else Statement Best Practice : even a if (test) { single statement here then_block true false should be in a block. test } else { else_block } then_block else_block 7 January 2019 OSU CSE 26

  27. if-else-if Statement if (test_1) { then_block_1 } else if (test_2) { The else if part may then_block_2 be repeated. } else { else_block } 7 January 2019 OSU CSE 27

  28. if-else-if Statement if (test_1) { Can you draw a flow- then_block_1 chart for this statement? } else if (test_2) { then_block_2 } else { else_block } 7 January 2019 OSU CSE 28

  29. while Statement false while (test) { test while_block true } while_block 7 January 2019 OSU CSE 29

  30. while Statement Control flow here can go backward, which creates a loop in the flow chart. false while (test) { test while_block true } while_block 7 January 2019 OSU CSE 30

  31. if-else Statement Control flow for if cannot go backward; there is no such thing as an “if loop”! if (test) { then_block true false test } else { else_block } then_block else_block 7 January 2019 OSU CSE 31

  32. Expressions and Statements 7 January 2019 OSU CSE 32

  33. Best Practices for boolean If you want to say this, e.g., in an if or while Say this instead: condition: b == true b b == false !b if (b) { return true ; } else { return b; return false ; } 7 January 2019 OSU CSE 33

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend