Chapter 2 Summary: Identifiers
Chapter 2 Summary: Identifiers
Identifiers
• Created by the programmer
• Use descriptive names
• Cannot be a reserved word
• case-sensitive
• Start with letter, continue with letters, digits and underscore “_”.
Data Types
The forms of variables used in a program
• bool, 1 bit, true or false; 1 or 0
• char, 8 bits = 1 byte, covers bool
o can hold a small integer –128 .. 127
o usually used to hold an ASCII character
• short, 16 bits = 2 byte, covers all chars
o integer, -32768 .. 32767
• int == long == long int, 32 bits = 4 bytes, covers all shorts
o integer, -2,147,483,648 to 2,147,483,647
• float, 32 bits = 4 bytes
o approx. 10^-38 to 10^38
• double, 64 bits = 8 bytes
o approx. 10^308 to 10^308
o float and double are for real numbers, with fractional parts, also
very large and very small.
o The set of doubles covers the set of floats and ints.
Declaration
data_type variable_name;
• data_type is typically int, double, or short, etc.
• variable_name is an identifier that you make up.
• Every variable must be declared before it’s used.
• Several variables can be declared using commas:
int i1, i2, i3;
• Can initialize in a declaration:
int i1=20, i2=-35, i3=0;
Data Type Conversion
Conversion from a “smaller” to a “larger” data type is usually automatic by the
compiler.
• causes no information loss
• For example, conversion of a char to an int is always exact
Conversion from a “larger” to a “smaller” will generate a compiler complaint,
unless you use a cast or special function to force it.
• there’s information loss, in general
• For example, converting a double to an int will always lose the fractional
part. The double may also be much too large for an int.
Input/Output
About cout –
• Sends information to the screen or redirected to a file
• Converts data types to character sequences
• Knows all of the above types
• Send a character string like this: cout << “size= “;
• Send “\n” or endl to send a line feed
• Double output can be formatted for precision, showpoint, fixed
• More details in chaper 5
About cin –
• Gets information from the keyboard or file indirection
• Always skips spaces, tabs and line feeds before reading
• Converts input character sequence to appropriate data type
• More details and options in chapter 5
Operators
Arithmetic (use between two numbers)
+ - * / %
• These obey algebraic precedence
• Use (...) to override the precedence rules
• - can be used to negate a number
• % (remainder) can only be used with integers
Assignment (compute the right side, set the left side. Left side: variable)
=
Comparisons (use between two numbers):
< > <= >= == !=
• Don’t write this:
a < b < c
• Write this instead:
(a < b) && (b < c)
Boolean (use between two bools):
&& ||
Boolean complement (use with a single bool):
!
Increment or decrement a variable
++ --
• Must be a variable, not some expression
• This is OK:
++MyVar
• This is WRONG:
(a+b)++
• Can be prefix or postfix.
Shorthand operators (there are more)
+= -= *= /=
Parentheses
( )
• Used to overcome precedence rules – operations inside ( ) is done first
• Parens have other uses in C, as we’ll discover
Statement group
{ }
• Braces are used to group a sequence of statements.
• The statement group can usually replace a statement followed by a
semicolon.
• Each statement in the group must be followed by a semicolon
cout operator
<<
• cout is on the left.
• variables, expressions or strings listed to the right
• use << between pairs of things being printed
• requires #include <iostream>
cin operator
>>
• cin is on the left.
• variables ONLY listed to the right
• use >> between pairs of things being input
• skips blanks, tabs and line feeds before reading input characters
• requires #include <iostream>
Array brackets
[ ]
• Brackets are used with arrays. (see chapter 10)
Flow of Control
How a program is organized – “what happens next”
if – then
if (B) S;
false true
B
• B is a Boolean
• B must be in parentheses
• S; can be { S1; S2; ... Sn; }
• Note: there’s no “then” in this.
• NO semicolon after (B).
if – then – else
if (B) S1; else S2;
false true
B
S2 S1
• there’s no “then”
• “else” must be there
while – do
while (B) S;
false true
B
do – while
(note the change – there is no do-until form in C or C++ -- my mistake)
do S; while (B);
true false
B
for (A; B; C) S;
false true
B
Comments
Two styles of comment are provided in C++:
/* a comment that
can extend over many lines
*/
Naming Constants
• NEVER just drop a numeric constant into a program, like this:
distance *= 1280;
• Someone else reading this will be puzzled about what the “1280” means. Feet in
a mile?
• INSTEAD, declare constants, like this:
const double PI= 3.14159265; // pi
const int FeetPerMile= 1280; // feet in one
mile
• THEN you can write
distance *= FeetPerMile;