1 / 38

CHAPTER 2

CHAPTER 2. Variables, Expressions, & Output. Syntax vs. Semantics. Syntax language rules how valid instructions are written in a programming language GRAMMAR. English. Semantics rules determining MEANING of instructions in programming language. Identifiers.

Download Presentation

CHAPTER 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2 Variables, Expressions, & Output

  2. Syntax vs. Semantics • Syntax • language rules • how valid instructions are written in a programming language • GRAMMAR English

  3. Semantics • rules determining MEANING • of instructions in programming language

  4. Identifiers

  5. What is an identifier? • A name associated with a process or object used to refer to that process or object • John, Sue, Larry • mass, volume, pay

  6. What are rules for valid identifiers? • Identifiers are made of • letters • digits • underscore • The first character must be a • letter • underscore

  7. VALID IDENTIFIERS • sum_of_squares J9 GetData • box_22A count Bin3D4 • INVALID • 40Hours Get Data Box-22 • stock_# if return • RESERVED WORDS • word that has special meaning in cannot be used as identifier (if)

  8. Why is it important to use meaningful identifier names? • Easier to understand • Self documenting • Compare printtopportion PRINTTOPPORTION PrintTopPortion • Last is best

  9. Case Sensitive • Identifiers with different capitalization are entirely distinct. • Total total • Two different identifiers.

  10. Variable Declarations

  11. What is a declaration? • Associating identifier (name) with • data object • function • So programmer • can refer to that item • by name (naming object)

  12. What is a variable? • A location in memory • referenced by an identifier • in which a data value is stored • value can change

  13. Example Declarations • var Identifier [, Identifier, …]; • var empNum; • var studentCount, maxScore, sumOfScores; • or better • var studentCount; // Description • var maxScore; // Description 2 • var sumOfScores; // etc. • best, declare when needed

  14. What Does a Variable Declaration Do? var ageOfDog; var taxRateY2K; var middleInitial; • Tells the compiler to • allocate enough memoryto hold value of this type • associate the identifierwith this location. 4 bytes for taxRateY2K 1 byte for middleInitial

  15. When to Declare? • Declare vars when needed • NOT at top of the main block

  16. Assignment Statements

  17. What is an assignment statement? • A statement • gives the value of an expression • to a variable.

  18. Valid Assignment Statements • Variable = Expression; • var num; var alpha; • var rate; var ch; • alpha = 2856; • rate = 0.36; • ch = 'B'; • num = alpha;

  19. Valid Expressions • constant variable • constant and/or variables combined with operators • Examples • alpha + 2 rate - 6.0 • 4 - alpha rate • alpha * num 12

  20. Unary and Binary Operators • Unary Operator • an operator that has just one operand • + - • Binary Operator • an operator that has two operands • + - * / %

  21. Modulo Arithmetic (%) • % operator • yields remainder of integer division • Use with integers only • 5 % 3 = 2 • 9 % 4 = 1 • 8 % 2 = 0 • 3 % 5 = 3

  22. 3 + 6 = ? 9 3.4 - 6.1 = ? -2.7 2 * 3 = ? 6 8 / 2 = ? 4 8.0 / 2.0 = ? 4.0 8 / 8 = ? 1

  23. 8 / 9 = ? 0 8 / 7 = ? 1 8 % 8 = ? 0 8 % 9 = ? 8 8 % 7 = ? 1 0 % 7 = ? 0

  24. Valid Assignment Statements • Variable = Expression; • var num = i; var alpha = j; • alpha = num + 6; • alpha = num / 2; • num = alpha * 2; • num = 6 % alpha;

  25. What is a literal value? • Any constant value written in a program. • Integers, real numbers, characters, strings of characters: • 34 921.2196 • 'D' "Hi There"

  26. Output

  27. How do you output data? • println("Hello"); • println (Processing, not JS) • Writes to the text area of the Processing environment's console. • Each call to this function creates a new line of output. • Individual elements can be separated with quotes ("") and joined with the string concatenation operator (+).

  28. Items are separated by + • literals are in quotes, • variable names or expressions allowed. • println(ExprOrString + ExprOrString);

  29. Examples: • var x; var y; var z; • x = 1; y = 2.3; z = 'A'; • println("x =" + x); • println("y =" + y + "z =" + z); • println("***"); • Output: x =1 y =2.3z =A *** • Insert white spaces in the output where needed!

  30. var x = -12345; • var y = 1.23456789; • var z = 'B'; • println(x + " " + y + " " + z); Output: -12345 1.23457 B

  31. The assignment statement is not an equality. • Var = Expr; • The assignment statement • takes the value of the expression • and assigns it to the variable. • It does NOT determine if the values are equal.

  32. Assignment Statement not equality • alpha = alpha + 1; • --Legal assignment, not equality • num = num + alpha;

  33. Increment and Decrement • ++ -- • num++; • ++num; ===> num = num + 1; • num--; • --num; ===> num = num - 1; • Use as standalone statements.

  34. String Literals • a string is a sequence of characters enclosed in double quotes • string literal sample values "Hello" "Year 2000" "1234" • the empty string (null string) contains no characters and is written as ""

  35. String Concatenation (+) • concatenation is a binary operation that uses the + operator • at least one of the operands must be a string variable or named constant--the other operand can be string type or var type

  36. Concatenation Example var WHEN = "Tomorrow" ; var EXCLAMATION = '!' ; var message1 ; var message2 ; message1 = "Yesterday " ; message2 = "and " ; message1 = message1 + message2 + WHEN + EXCLAMATION ; println(message1);

  37. How does one add comments to a program? • Comments giving explanatory notes • They may appear anywhere in a program. • /* */ around text. • // Comment the rest of a line • It is good programming style to add descriptive comments to your program. • Paste document.cpp to top of program

More Related