Presentation is loading. Please wait.

Presentation is loading. Please wait.

End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular.

Similar presentations


Presentation on theme: "End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular."— Presentation transcript:

1 End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular.

2 Programming and natural languages share three elements. Vocabulary: Words and symbols Syntax: Rules for combining statements Think: grammar Semantics: Rules for interpreting well-formed statements Think: meaning Order of precedence Think: PEMDAS on steroids Resolve ambiguity: “Jack jumped and Sally laughed or Jill skipped” ((Jack jumped and Sally laughed) or Jill skipped) (Jack jumped and (Sally laughed or Jill skipped))

3 Vocabulary (AKA Lexicon) Also: for, if, while, do, et cetera.

4 Reserved words

5 Syntax Mechanical rules that define which sentences are well-formed (i.e., acceptable.) For example, the rule for building sentences with ‘+’ is roughly: – If int is a well-formed expression that evaluates to an integer, double is a well-formed expression that evaluates to a double, and string is a well-formed expression that evaluates to a string, then: – int + int is well-formed – double + double is well-formed – string + string is well-formed Note: we still don’t know what these expressions mean, we just know whether they’re syntactically correct (i.e., well-formed) ‘Colorless green ideas sleep furiously’ is grammatically (i.e., syntactically) correct English, but it’s still gibberish

6 Or is it? By the way, I didn’t make this example up; a linguist named Noam Chomsky did. Also, someone else made this beautiful art someone else

7 The compiler complains if you write syntactically incorrect code

8

9 All wrong in different ways

10 Semantics Say what well-formed sentences mean Int + Int means add the two integers. So, 5+3 evaluates to 8. String + String means concatenate the two strings. So, “abc” + “def” evaluates to “abcdef” When your program runs, the actual commands that are executed depend on semantics.

11 Some operators (and precedence)

12 Casting The casting operator is a unary operator, like the negative sign. (Addition, multiplication, division, etc. are binary operators) Changes the type E.g., (double) 4 (int) “4” ??? – Integer.parseInt(“4”);

13 Mixed-mode arithmetic 3 + 4.0 = ?7? 7.0? 3 / 2.0 = ?1? 1.5? Very common source of bugs Might lead to division by zero, or other such catastrophe

14 Data types Primitive Defined by a class (instantiated as an object) Syntax for data types: Primitive: combined using operator (+, -, etc) e.g., 3+4 Objects: send messages. (Exception: Strings) e.g., Integer four = new Integer(); four.add(3);

15 Literals: express value – primitive data types and Strings Variables – Must be declared (can be declared and initialized at same time) Int age Instantiation (objects) – new ( ) Constants – Final = ;

16 Strings Literals Assigned to variables Concatenated using assignment operator Have methods – The Length =.length(); Send messages **some special characters: \ For example: \”, \’, \\ ‘\’ is called the “escape character” \n this is for new line \t this is for tab

17 Programming errors Syntax (we just saw a few. Doesn’t compile) Run-time (compiles, but fails when you try to run. E.g., bad input, running off the end of an array. – More syntax errors= fewer runtime errors. One idea behind “strongly typed” languages, like Java Logic (Compiles, but doesn’t do what you think it does. Like, if you calculated Nautical miles wrong) Source: XKCD (web comic)


Download ppt "End of the beginning Let’s wrap up some details and be sure we are all on the same page Good way to make friends and be popular."

Similar presentations


Ads by Google