Transcript operations

Basic Notions Review











what is a variable? value? address? memory location?
what is an identifier? variable name? keyword?
what is a legal identifier? what identifiers are legal?
what are C-style, Pascal-style, Camel Back identifiers?
what is a variable type? what types have we studied?
what is a variable declaration? where (in the program) is a variable
declaration placed?
what is an assignment?
what is a stream? input stream? output stream? cout? cin?
what is an extraction/insertion operator?
what is an escape sequence?
what is an input token?
Types, Expressions,
More on Assignment
int, double and Others
 type is the kind of data that is stored in variables
 int - whole numbers
 double – numbers with fractions (called floating point)
 since the storage is limited, the fraction can contain only a limited number
of digits (usually up to 14)
 two ways to write a double number in C++
 regular:
2.0 -3.23 +0.0456 .45 dot needs to be there
 scientific or floating point (explicitly states mantissa and exponent):
5.89e5 .045E-4 5e+5 dot does not need to be there
 mantissa is limited in size.
 The largest allowable number differs for every architecture. Usually:
 int - up to 32767 or 2147483647
 double - up to 10308
 int or double (or any other type in C++) cannot contain a comma
 other possible types are short, float and long double
 in this course, use int
Character Type
 a variable stores a single character, e.g. ’a’, ’A’, ’%’, ’1’
 declared char varName;
 note that ’1’is also a character, not the same thing as numeric type 1
 note also that ”1” is a string, not a character
// asks for initials and outputs greeting
#include <iostream>
using std::cin; using std::cout; using std::endl;
main (){
char first, second;
cout << ”Enter your initials: ”;
cin >> first >> second;
cout << ”Hello ” << first << second << endl;
cout << ”pleased to meet you\n”;
}
Type bool
 bool (short for boolean) is used for branching and looping statements
 a boolean variable can have only two values true or false
bool result;
result = true;
 true and false are keywords and cannot be used as identfiers
Literal Constants
 an explicitly stated value is a literal constant
 examples: 23 34.4 ’a’ true
 a literal constant has value and type
 what are the types of the above constants?
Type Compatibility
 As a rule you cannot store a value of one type in a variable of
another type
 trying to do it leads to type mismatch
int intvar;
intvar = 2.99;
 compiler prints this:
warning: assignment to ’int' from ’double'
 it still compiles the program discarding the fractional part, giving
you a warning.
 it is usually a bad idea (but some programmers do it) to store
char values in variables of type int, bools can also be in int.
Even though your compiler allows it, it obscures the meaning of
the variables and should be avoided.
Expressions
 An expression is a mechanism of calculating new values of
objects from old ones;
 An expression is composed of operands and operations
 each expression (similar to variables and constants) has type
and value
 simplest expressions – a variable or literal constant with no
operation applied;
examples: 23
18.53 ’a’ intvar
Binary Integer Operations





addition
subtraction
multiplication
division
remainder
Operation
+
*
/
%
Example
2+3
a+4
‘b’+1
count-2
4-7
5*6
width*height
12/3 4/5
10%3
23%4
 for positive integers: if the integer division is not even, then the
fractional part of the result is discarded: 12/5 produces 2
 note that the fractional part is discarded and the result is never
rounded up:
11/3 which should be (3.6666…) produces 3 not 4
 the remainder can be used to “catch” the “missing” fraction
12%5 produces 2
Binary Double/Mixed Operations
Operation




addition
subtraction
multiplication
division
+
*
/
Example
2.3 + 3.4
2.45 - 1.3
5.4*2.3
12.4 / 5.0
 there is no remainder operations with floating point
 if there are integer and floating-point operands then the integers
are first converted (by compiler) to floating-point operands and
then the expression is evaluated:
45.34 * 2
is converted to
45.34 * 2.0
Unary Operations, Precedence
 Unary operations are allowed: +23 -2.34
 precedence (order of execution) follows mathematical
conventions:
1. Unary +, 2. Binary *, /, %
3. Binary - and +
 you can use () to change precedence:
 (2+3)*2 changes default precedence
2 / 3 + 5 is equivalent to (2 / 3) + 5
-8 * 4
(-8) * 4
8 + 7 % 4
8 + (7 % 4)
Whole Numbers in Division
 When you divide int by int the result is int. It may be
problematic in expressions and the problem is hard to spot
 the compiler will not complain
 this program converts feet into miles. Is there anything wrong
with it?
double miles;
int feet;
cin >> feet;
miles = feet/5280;
Assignment Conversions
 if a double expression is assigned to an integer variable, its
fractional part is dropped
 if an int expression is assigned to a double variable, the
expression is converted to double with zero fractional part
 Consider
double y = 2.7;
int i = 15;
int j = 10;
i = y;
cout << i << endl;
y = j;
cout << y << endl;
// i is now 2
// y is now 10.0
Compound Assignment
 C++ has a large set of operators for applying an operation to an
variable and then writing the result back into the variable. Can be
used for both int and double
 examples:
int i = 3;
i += 4;
cout << i << endl;
double a = 3.2;
a *= 2.0;
cout << a << endl;
// equivalent to i=i+4;
// equivalent to a=a*2.0;
 examples of other compound assignments :
time /= rush_factor;
change %=100;
amount *= cnt1+ cnt2;
 what is the equivalent of the last operation?
Increment and Decrement
 C++ has special operators for incrementing (increasing by one) or
decrementing (decreasing by one) a variable value stored by a variable
 such operation can be used as a standalone statement ++k;
or in an expression a = ++k + 5;
 operations have prefix and postfix(suffix) form
 allowed in both standalone statements and expressions
 no difference in standalone statements (stylistic issue)
 in expressions,
– in prefix form, the operation applies before value used in expression
int k=5;
a = ++k+5; // a is 11, k is 6
– in suffix form, the operation applies after value is used in expression
int k=5;
a = k++ +5; // a is 10, k is 6
 in modern programming the postfix form is avoided since it may be inefficient
for complex types
Increment and Decrement Example
int k;
k=4;
++k;
k++;
cout << k
int i; i=
cout << i
int j; j=
cout << j
// k is 5
// k is 6
<< endl;
k++;
// i is 6, k is 7
<< " " << k << endl;
++k;
// j is 8, k is 8
<< " " << k << endl;
Uninitialized Variables,
Assignment at Declaration
 any declared variable contains a value
 what is wrong with this program fragment?
int desiredNumber;
desiredNumber=desiredNumber+5;
 to avoid uninitialized variable problem and make programs more
concise C++ allows assignment at initialization:
 there are two allowed forms:
 primary
int count=0, limit=10;
double distance=5.723, pi=3.14;
double step=limit/2.0;
 alternative
int count(0), limit(10);
double distance(5.723), pi(3.14);
double step(limit/2.0);