Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.

Similar presentations


Presentation on theme: "Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what."— Presentation transcript:

1 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?

2 Types, Expressions, More on Assignment

3 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 10 308 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

4 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 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”; }

5 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

6 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?

7 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, bool s can also be in int. Even though your compiler allows it, it obscures the meaning of the variables and should be avoided.

8 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

9 Binary Integer Operations Operation Example addition + 2+3a+4 ‘b’+1 subtraction - count-2 4-7 multiplication * 5*6width*height division / 12/34/5 remainder % 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

10 Binary Double/Mixed Operations Operation Example addition + 2.3 + 3.4 subtraction - 2.45 - 1.3 multiplication * 5.4*2.3 division / 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

11 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 % 48 + (7 % 4)

12 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;

13 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; // i is now 2 cout << i << endl; y = j; // y is now 10.0 cout << y << endl;

14 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; // equivalent to i=i+4; cout << i << endl; double a = 3.2; a *= 2.0; // equivalent to a=a*2.0; cout << a << endl; examples of other compound assignments : time /= rush_factor; change %=100; amount *= cnt1+ cnt2; what is the equivalent of the last operation?

15 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

16 Increment and Decrement Example int k; k=4; ++k; // k is 5 k++; // k is 6 cout << k << endl; int i; i= k++; // i is 6, k is 7 cout << i << " " << k << endl; int j; j= ++k; // j is 8, k is 8 cout << j << " " << k << endl;

17 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);


Download ppt "Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what."

Similar presentations


Ads by Google