1 / 18

Basic Notions Review

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, Camel Back-style identifiers? what is variable type? what types have we studied?

Download Presentation

Basic Notions Review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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, Camel Back-style 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?

  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, 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

  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 <iostream> main (){ char first, second; cout << ”Enter your initials: ”; cin >> first >> second; cout << ”Hello ” << first << second << endl; cout << ”pleased to meet you\n”; }

  5. bool Type 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 identifiers

  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. Named Constants • there are problems with using literal constants • 9.8 does not give an idea as to what it means in the program • hard to modify if used in multiple places in program • named constant gives a name to a value; have to be declared: constintwindowCount = 5; const double taxRate = 9.8; • style: • use named constants rather than literal constants • use constants rather than variables

  8. 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,boolscan also be in int. Even though you compiler allows it, it obscures the meaning of the variables and should be avoided

  9. Expressions expression is a mechanism of calculating new values expression (similar to variable and constant) has type and value simplest expression literal constant named constant complex expression consists of operands joined by operators operand – (sub)expression operator – computes new values based on operands arity - number of operands the operator uses binary operator – two operands unary operator – one operand

  10. Binary Integer Operators Operator Example addition + 2+3 a+4 b+1 subtraction - count-2 4-7 multiplication * 5*6 width*height division / 12/3 4/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” fraction12%5 produces 2

  11. Binary Double/Mixed Operators Operator 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 operator with floating point if there are integer and floating-point operands then the integersare first converted (by compiler) to floating-point operands and then the expression is evaluated:45.34 * 2 is converted to 45.34 * 2.0

  12. Unary Operators, Precedence unary operators: +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) * 48 + 7 % 48 + (7 % 4)

  13. Whole Numbers in Division when you divide int byint 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);

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

  15. Compound Assignment compound assignment - joins assignment with another operator syntax: variable operator = expression; operator takes variable and expression as operands computes the value, assigns it to variable can be used for different types: int and double shorthand for regular assignment 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 this equivalent to?

  16. Increment and Decrement increment – increase value by one, unary operator: ++ decrement – decrease value by one, unary operator: -- can be used as a standalone statement ++k; or --k; in an expression a = ++k + 5; or a = --k + 5; two forms: prefix – preceding operand, and postfix/suffix – following operand both forms can be used in standalone statements and expressions no difference in standalone statements (stylistic preference) 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 aftervalue is used in expression int k=5; a = k++ +5; // a is 10, k is 6 modern style: avoid postfix form (may be inefficient for complex types)

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

  18. Initialization any declared variable contains a value what is wrong with this code? int desiredNumber; desiredNumber=desiredNumber+5; initialization – assigning a value to the variable at its declaration two 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);

More Related