Basic Notions Review - Kent State University
Download
Report
Transcript Basic Notions Review - Kent State University
Basic Notions Review
what is a variable? value? address? memory location?
what is an identifier? variable name? keyword?
what is legal identifier? what identifiers are legal?
what are C-style, Pascal-style, Camel Back identifiers?
what is variable type? what types have we studied?
what is 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?
1
Types, Expressions,
More on Assignment
2
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, 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
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
3
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>
main (){
char first, second;
cout << ”Enter your initials: ”;
cin >> first >> second;
cout << ”Hello ” << first << second << endl;
cout << ”pleased to meet you\n”;
}
4
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
5
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?
6
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'
but still compiles the program discarding the fractional part
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
you compiler allows it, it obscures the meaning of the variables and
should be avoided
7
Expressions
expression is a mechanism of calculating new values of objects from old
ones;
each expression (similar to variables and constants) has type and value
simplest expression – literal constant with no operator applied;
examples: 23
18.53
’a’
expression consists of operands and operators
arity - the number of operands the operator uses
binary operator – two operands
unary operator – one operand
8
Binary Integer Operators
Operator
addition
subtraction
multiplication
division
remainder
+
*
/
%
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
9
Binary Double/Mixed Operators
Operator
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 operator 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
10
Unary Operators, Precedence
unary operators are allowed: +23
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.34
2 / 3 + 5 is equivalent to (2 / 3) + 5
-8 * 4
(-8) * 4
8 + 7 % 4
8 + (7 % 4)
11
Whole Numbers in Division
when you divide int by int the result is int. It may be
problematic in expression and the problem is hard to spot
the compiler would not complain
this program converts feet into miles. Is there anything wrong with
it?
double totalPrice;
int feet;
cin >> feet;
totalPrice = 5000 * (feet/5280);
12
Assignment Conversions
if double expression is assigned to an integer variable, its fractional
part is dropped
if 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
13
Compound Assignment
C++ has a large set of operators for applying an operation to a 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;
// equivalent to i=i+4;
double a = 3.2;
a *= 2.0;
cout << a << endl;
// 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 operator?
14
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 operator can be used as a standalone statement ++k;
or in an expression a = ++k + 5;
operator 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 postfix form is avoided: may be inefficient if complex
types
15
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;
16
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);
17