today’s lecture literal constant precedence rules more assignment rules program style
TRANSCRIPT
![Page 1: Today’s Lecture Literal Constant Precedence rules More assignment rules Program Style](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022083009/5697c0191a28abf838cce6c0/html5/thumbnails/10.jpg)
Program Style
Comments, two methods: // Two slashes indicate entire line is to be
ignored /*Delimiters indicates everything between is
ignored*/
Identifier naming MEANINGFUL NAMES!