today’s lecture literal constant precedence rules more assignment rules program style

10
Today’s Lecture Literal Constant Precedence rules More assignment rules Program Style

Upload: toby-king

Post on 19-Jan-2016

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Today’s Lecture

Literal

Constant

Precedence rules

More assignment rules

Program Style

Page 2: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

C++ Variables

C++ Identifiers Keywords/reserved words Variables Program name Case-sensitivity and validity of identifiers

Variables A memory location to store data for a program Must declare all data before use in program

Page 3: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Literal Data

Literals Examples:

2 // Literal constant int 5.75 // Literal constant double "Z" // Literal constant char "Hello World" // Literal constant string

Cannot change values during execution

Page 4: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Escape Sequences

"Extend" character set Backslash, \ preceding a character

Instructs compiler: a special "escapecharacter" is coming

For example: “\n” means new cline

Page 5: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Constants

Naming your constants Literal constants are "OK", but provide

little meaning e.g., seeing 24 in a program, tells nothing about

what it represents

Use named constants instead Meaningful name to represent data

const int NUMBER_OF_STUDENTS = 24;

Page 6: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Precedence Rules

There may be many operators in one expression, which one do you execute first?

Examplesint x;

x = 5 + 6 * 7 – 8

Rules Level1: () Level 2: *, /, % Level 3: +, - Level 4: =

Page 7: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Shorthand Operators: Two Options

Post-IncrementintVar++ Uses current value of variable, THEN increments it

Pre-Increment++intVar Increments variable first, THEN uses new value

No difference if "alone" in statement:intVar++; and ++intVar; identical result

Page 8: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Post-Increment in Action

Post-Increment in Expressions:int n = 2,

Value;Value = 2 * (n++);cout << Value << endl;cout << n << endl; This code segment produces the output:

43

Since post-increment was used

Page 9: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Pre-Increment in Action

Now using Pre-increment:int n = 2,

Value;Value = 2 * (++n);cout << Value << endl;cout << n << endl; This code segment produces the output:

63

Because pre-increment was used

Page 10: Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style

Program Style

Comments, two methods: // Two slashes indicate entire line is to be

ignored /*Delimiters indicates everything between is

ignored*/

Identifier naming MEANINGFUL NAMES!