Engineering Science 1036A/B Lecture Notes - Lecture 6: Level Of Measurement, Reserved Word
C++ Variables (Objects)
Memory spaces must be reserved to process data/info
Reservations are done using variable/constant names
○
•
Variables: boxes/placeholders in the memory that can hold things
•
Each variable has to be identified by a name, which has to be declared before using it
•
The name of any type of variable/constant/ function/namespace/class is called an
identifier
•
Size of the variable depends on the type of things we are planning to store there
•
We have to tell the compiler in advance (declare):
Names of each of the variables we want
○
The type of things that will be put in each variable
○
•
Identifier Characteristics
An identifier may consist of:
Alphabetical letters
Capital or lowercase
§
○
Numerical Digit (0-9)
○
Special characters: "_" and "$"
○
•
An identifier cannot start with a digit
•
An identifier cannot be a reserved word
•
An identifier can be of any length unless C++ complier has restrictions
Usually are under 31 characters to ensure portability
○
•
Reserved Words
Reserved words (keywords): The words which are reserved for a particular
programming language (grammar)
•
Each keyword has a particular meaning to the programming language
•
Variable Declaration
A variable has to declared by writing the name of the variable as well as the data type it
represents
•
Declaring a variable tells the compiler to allocate enough memory space to hold a value
of this data type and to associate the identifier with this location
•
Re-declaring a variable is a syntax-error in any block statement•
Built-in Data Types
Mixing different data types
Numerical data types (int,float,double) cannot be mixed with non-numerical data
types (char,string)
•
char
Holds a single character•
•
toupper() & tolower()
int toupper (int c);
Converts argument "c" to its uppercase equivalent if "c" is a lowercase letter
Otherwise, it returns the same letter
§
anyChracter = toupper (anyChracter);
§
○
•
int tolower (int C);
Converts argument "C" to its uppercase equivalent if "C" is a lowercase letter
Otherwise, it returns the same letter
§
anyChracter = tolower (anyChracter);
§
○
•
string
Holds a sequence of characters•
Needs #include <string> directive•
•
cin extraction for a string type variable stops reading data as soon as it finds any white
space character, so only one word will be inputted
•
Assigning a Value to a Variable
Variable initialization: After a variable is declared, it can be assigned a value•
Variable value can also be assigned using an assignment statement•
The syntax for assignment statement is: Variable = expression
An expression represents a computation involving:
Values
§
Variables
§
Operators that evaluates to a value
§
○
•
Assignment operator: (=)•
Literals
A constant value that appears directly in the program•
"hard-coding"•
•
Assignment Expression
A variable can appear in both sides of the assignment operator but the left hand side has
to be a writable memory location
•
Constants
A named constant is a location in memory (box) in which a data value cannot be
changed
•
Modifying value of a constant is a syntax error•
Must be given a value in its declaration
Otherwise results in a syntax error
○
•
Practice: capitalize constant names•
Named constants can also be defines using pre-processor directive•
Variables
C++ Variables (Objects)
Memory spaces must be reserved to process data/info
Reservations are done using variable/constant names
○
•
Variables: boxes/placeholders in the memory that can hold things
•
Each variable has to be identified by a name, which has to be declared before using it
•
The name of any type of variable/constant/ function/namespace/class is called an
identifier
•
Size of the variable depends on the type of things we are planning to store there
•
We have to tell the compiler in advance (declare):
Names of each of the variables we want
○
The type of things that will be put in each variable
○
•
Identifier Characteristics
An identifier may consist of:
Alphabetical letters
Capital or lowercase
§
○
Numerical Digit (0-9)
○
Special characters: "_" and "$"
○
•
An identifier cannot start with a digit
•
An identifier cannot be a reserved word
•
An identifier can be of any length unless C++ complier has restrictions
Usually are under 31 characters to ensure portability
○
•
Reserved Words
Reserved words (keywords): The words which are reserved for a particular
programming language (grammar)
•
Each keyword has a particular meaning to the programming language
•
Variable Declaration
A variable has to declared by writing the name of the variable as well as the data type it
represents
•
Declaring a variable tells the compiler to allocate enough memory space to hold a value
of this data type and to associate the identifier with this location
•
Re-declaring a variable is a syntax-error in any block statement•
Built-in Data Types
Mixing different data types
Numerical data types (int,float,double) cannot be mixed with non-numerical data
types (char,string)
•
char
Holds a single character•
•
toupper() & tolower()
int toupper (int c);
Converts argument "c" to its uppercase equivalent if "c" is a lowercase letter
Otherwise, it returns the same letter
§
anyChracter = toupper (anyChracter);
§
○
•
int tolower (int C);
Converts argument "C" to its uppercase equivalent if "C" is a lowercase letter
Otherwise, it returns the same letter
§
anyChracter = tolower (anyChracter);
§
○
•
string
Holds a sequence of characters•
Needs #include <string> directive•
•
cin extraction for a string type variable stops reading data as soon as it finds any white
space character, so only one word will be inputted
•
Assigning a Value to a Variable
Variable initialization: After a variable is declared, it can be assigned a value•
Variable value can also be assigned using an assignment statement•
The syntax for assignment statement is: Variable = expression
An expression represents a computation involving:
Values
§
Variables
§
Operators that evaluates to a value
§
○
•
Assignment operator: (=)•
Literals
A constant value that appears directly in the program•
"hard-coding"•
•
Assignment Expression
A variable can appear in both sides of the assignment operator but the left hand side has
to be a writable memory location
•
Constants
A named constant is a location in memory (box) in which a data value cannot be
changed
•
Modifying value of a constant is a syntax error•
Must be given a value in its declaration
Otherwise results in a syntax error
○
•
Practice: capitalize constant names•
Named constants can also be defines using pre-processor directive•
Variables
C++ Variables (Objects)
Memory spaces must be reserved to process data/info
Reservations are done using variable/constant names
○
•
Variables: boxes/placeholders in the memory that can hold things•
Each variable has to be identified by a name, which has to be declared before using it•
The name of any type of variable/constant/ function/namespace/class is called an
identifier
•
Size of the variable depends on the type of things we are planning to store there•
We have to tell the compiler in advance (declare):
Names of each of the variables we want
○
The type of things that will be put in each variable
○
•
Identifier Characteristics
An identifier may consist of:
Alphabetical letters
Capital or lowercase
§
○
Numerical Digit (0-9)
○
Special characters: "_" and "$"
○
•
An identifier cannot start with a digit•
An identifier cannot be a reserved word•
An identifier can be of any length unless C++ complier has restrictions
Usually are under 31 characters to ensure portability
○
•
Reserved Words
Reserved words (keywords): The words which are reserved for a particular
programming language (grammar)
•
Each keyword has a particular meaning to the programming language•
Variable Declaration
A variable has to declared by writing the name of the variable as well as the data type it
represents
•
Declaring a variable tells the compiler to allocate enough memory space to hold a value
of this data type and to associate the identifier with this location
•
Re-declaring a variable is a syntax-error in any block statement
•
Built-in Data Types
Mixing different data types
Numerical data types (int,float,double) cannot be mixed with non-numerical data
types (char,string)
•
char
Holds a single character
•
•
toupper() & tolower()
int toupper (int c);
Converts argument "c" to its uppercase equivalent if "c" is a lowercase letter
Otherwise, it returns the same letter
§
anyChracter = toupper (anyChracter);
§
○
•
int tolower (int C);
Converts argument "C" to its uppercase equivalent if "C" is a lowercase letter
Otherwise, it returns the same letter
§
anyChracter = tolower (anyChracter);
§
○
•
string
Holds a sequence of characters•
Needs #include <string> directive•
•
cin extraction for a string type variable stops reading data as soon as it finds any white
space character, so only one word will be inputted
•
Assigning a Value to a Variable
Variable initialization: After a variable is declared, it can be assigned a value•
Variable value can also be assigned using an assignment statement•
The syntax for assignment statement is: Variable = expression
An expression represents a computation involving:
Values
§
Variables
§
Operators that evaluates to a value
§
○
•
Assignment operator: (=)•
Literals
A constant value that appears directly in the program•
"hard-coding"•
•
Assignment Expression
A variable can appear in both sides of the assignment operator but the left hand side has
to be a writable memory location
•
Constants
A named constant is a location in memory (box) in which a data value cannot be
changed
•
Modifying value of a constant is a syntax error•
Must be given a value in its declaration
Otherwise results in a syntax error
○
•
Practice: capitalize constant names•
Named constants can also be defines using pre-processor directive•
Variables
Document Summary
Memory spaces must be reserved to process data/info. Variables: boxes/placeholders in the memory that can hold things. Each variable has to be identified by a name, which has to be declared before using it. The name of any type of variable/constant/ function/namespace/class is called an identifier. Size of the variable depends on the type of things we are planning to store there. We have to tell the compiler in advance (declare): Names of each of the variables we want. The type of things that will be put in each variable. An identifier can be of any length unless c++ complier has restrictions. Usually are under 31 characters to ensure portability. Reserved words (keywords): the words which are reserved for a particular programming language (grammar) Each keyword has a particular meaning to the programming language. A variable has to declared by writing the name of the variable as well as the data type it represents it represents.