+ All Categories
Home > Documents > CHAPTER 2

CHAPTER 2

Date post: 01-Jan-2016
Category:
Upload: demetria-richmond
View: 23 times
Download: 2 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
38
CHAPTER 2 Variables, Expressions, & Output
Transcript

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

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

What are rules for valid identifiers?

Identifiers are made of letters digits underscore

The first character must be a letter underscore

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)

Why is it important to use meaningful identifier names? Easier to understand Self documenting

Compare

printtopportionPRINTTOPPORTIONPrintTopPortion

Last is best

Case Sensitive Identifiers with different capitalization

are entirely distinct.

Total total

Two different identifiers.

Variable Declarations

What is a declaration? Associating identifier (name) with

data object function

So programmer can refer to that item by name (naming object)

What is a variable? A location in

memory referenced by an

identifier

in which a data value is stored value can change

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

What Does a Variable Declaration Do?

Tells the compiler to • allocate enough memory to hold value of this type• associate the identifier with this location.

var ageOfDog;var taxRateY2K;var middleInitial;

4 bytes for taxRateY2K 1 byte for middleInitial

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

Assignment Statements

What is an assignment statement?

A statement gives the value of an expression to a variable.

Valid Assignment Statements Variable = Expression;

var num; var alpha; var rate; var ch;

alpha = 2856; rate = 0.36; ch = 'B'; num = alpha;

Valid Expressions constant variable constant and/or variables combined

with operators

Examples alpha + 2 rate - 6.0 4 - alpha rate alpha * num 12

Unary and Binary Operators Unary Operator

an operator that has just one operand + -

Binary Operator an operator that has two operands + - * / %

Modulo Arithmetic (%) % operator

yields remainder of integer division Use with integers only

5 % 3 = 2 9 % 4 = 1 8 % 2 = 0 3 % 5 = 3

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

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

Valid Assignment Statements Variable = Expression;

var num = i; var alpha = j;

alpha = num + 6; alpha = num / 2; num = alpha * 2; num = 6 % alpha;

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"

Output

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 (+).

Items are separated by + literals are in quotes, variable names or expressions allowed.

println(ExprOrString + ExprOrString);

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!

var x = -12345; var y = 1.23456789; var z = 'B'; println(x + " " + y + " " + z);

Output:

-12345 1.23457 B

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.

Assignment Statement not equality

alpha = alpha + 1; --Legal assignment, not equality

num = num + alpha;

Increment and Decrement ++ --

num++; ++num; ===> num = num + 1;

num--; --num; ===> num = num - 1;

Use as standalone statements.

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 ""

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

Concatenation Example

var WHEN = "Tomorrow" ;

var EXCLAMATION = '!' ;

var message1 ;

var message2 ;

message1 = "Yesterday " ;

message2 = "and " ;

message1 = message1 + message2 +

WHEN + EXCLAMATION ;

println(message1);

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


Recommended