Basic Notions Review - Kent State University

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

what is an input token? 1 Types, Expressions, More on Assignment 2 int and double type is the kind of data that is stored in variables int - whole numbers double numbers with fractions (called floating point numbers)

since storage is limited, fraction can contain only a limited number of digits (usually up to 14) double number always has dot. Trailing or leading zero may be skipped. 2.0 -3.23 +0.0456 .45 3. the largest allowable number differs by architecture. Usually: int - up to 32767 double - up to 10308 int or double (or any other type in C++) cannot contain a comma in this course, use int 3 char Type a character variable stores a single character, e.g. a, A, %, 1 declared char varName; note that 1is also a character, different from integer type 1 note that 1 is a string, not a character // asks for initials and outputs greeting #include main (){ char first, second; cout << Enter your initials: ; cin >> first >> second; cout << Hello << first << second << endl;

cout << pleased to meet you\n; } 4 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 5 Literal Constants

literal constant is an explicitly stated value examples: 23 34.4 a true a literal constant has value and type what are the types of the above constants? 6 Named Constants there are problems with using literal constants 9.8 unclear how is used in program hard to modify if used in multiple places in program named constant gives a name to a value; have to be declared: const int windowCount = 5; const double taxRate = 9.8; style:

use named constants rather than literal constants use named constants rather than variables 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' 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 makes the program less understandable and should be avoided 8 Expressions expression is a mechanism of calculating new values expression (similar to variable and constant) has type and value expression evaluation is computing the value of an expression during program execution simple expression: literal constant, named constant, variable complex expression consists of operands joined by operators operand (sub) expression operator computes new values based on operands arity is number of operands the operator uses binary operator two operands unary operator one operand

9 Binary Integer Operators Name addition subtraction multiplication division remainder Symbol + * / %

Examples 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 (not rounded up or down) for expression 11/3 which should be (3.6666) the value is 3 not 4 remainder given two integers a and b: a%b = r if b*q + r = a and r < |b| can be used to catch the missing fraction of the value example: 12%10 is 2 10 Binary Double/Mixed Operators

Name addition subtraction multiplication division Symbol + * / Examples 2.3 + 3.4 2.45 - 1.3 5.4*2.3 12.4 / 5.0

there is no remainder operator with floating-point operands 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 Operators, Precedence unary operators: +23 -2.34 precedence: order of operator evaluation follows mathematical convention: 1. unary +, 2. binary *, /, % 3. binary - and + use () to change precedence: (2+3)*2 changes default precedence

2 / 3 + 5 is equivalent to -8 * 4 8 + 7 % 4 (2 / 3) + 5 (-8) * 4 8 + (7 % 4) 12 Whole Numbers in Division int is divided by int the result is int may be problematic in expression; 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);

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

Compound Assignment compound assignment - joins assignment with another operator syntax: variable binaryOperator = expression; binaryOperator takes variable and expression as operands evaluates this, assigns value to variable can be used for different types: int and double shorthand for regular assignment: variable = variable binaryOperator expression; examples: int i = 3; i += 4; // equivalent to i=i+4; cout << i << endl; 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 this equivalent to? 15 Increment and Decrement Intro 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; side effect: using construct for purpose other than primary

usually undesirable using increment/decrement in an expression creates a side effect of updating variable value tolerated 16 Increment and Decrement Prefix/Postfix Forms two forms: prefix preceding operand ++k postfix/suffix following operand k++ 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 after value 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 Examples int k; k=4; ++k; k++; int i; i= k++; int j; j= ++k; // // // // int m; int n; // what is the value of m, k ?

// what is the value of m, k ? m = k--; n = --k; k k i j is is is is 5 6 6, k is 7 8, k is 8 18 Initialization

any declared variable contains a value what is wrong with this code? int desiredNumber; desiredNumber=desiredNumber+5; initialization explicitly assigning initial value to a variable two forms for initialization at declaration: 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); 19

Recently Viewed Presentations

  • 1. What was the old Imperialism? 2. Where

    1. What was the old Imperialism? 2. Where

    Old Imperialism: Spanish & Portuguese Empires, 1700 Spanish Portuguese The setting for the new imperialism was Africa and Asia, from approximately 1870 to 1960. The leaders of the new imperialism were Britain, France, Italy, Germany, and Belgium. New Conquests B...
  • Extremism - preventforfeandtraining.org.uk

    Extremism - preventforfeandtraining.org.uk

    [email protected] or talk to us in person) Your Course Manager or a tutor that you trust. Your parents or carers or social worker. Phone the Safeguarding number on the back of your ID - 07500 661340 or after 5.00 o'clock...
  • Big Question: - scottsboro.org

    Big Question: - scottsboro.org

    Big Question: How can knowing another language create understanding? Monday. Tuesday. Wednesday. Thursday. ... jen and me made a copy of one line of hieroglyphs, it took a long time ... You can use Greek and Latin roots to help...
  • LE RETI INFORMATICHE - Altervista

    LE RETI INFORMATICHE - Altervista

    LE RETI INFORMATICHE 1 Prof. Papi Serena Telematica L'evoluzione della tecnologia nelle telecomunicazioni e la necessità di una condivisione delle informazioni come: invio di documenti in formato elettronico (fax e/o posta elettronica), uso massiccio di carte di credito e bancomat,...
  • YEVO International Inc. USA Headquarters YEVO Manufacturing Street

    YEVO International Inc. USA Headquarters YEVO Manufacturing Street

    David possesses over 20 years of experience as a corporate lawyer and President/CEO in the nutritional industry, as well as serving as a member of several boards for nutrition research organizations. David has served as CEO of Metabolife and as...
  • Team Sit at your table group Cultural Group

    Team Sit at your table group Cultural Group

    AGENDA. Develop your quick identity summary (15 min) Meet and Greet (30 min) Naming Ceremony (10 mins prep) Debrief(20 min) Introduce our first reading
  • CNK Template - baroda-icai.org

    CNK Template - baroda-icai.org

    Imply that the matter has not been appropriately resolved by the auditor in forming the opinion on the financial statements. Contain or imply discrete opinions on separate elements of the financial statements ( a "piecemeal opinion")
  • Revisions Incorporated into the 2009 MUTCD Revisions to

    Revisions Incorporated into the 2009 MUTCD Revisions to

    Revisions to Part 5 - Traffic Control Devices for Low-Volume Roads Part 5 is not applicable on neighborhood residential streets Part 5 applies only outside of built-up areas of cities, towns, and communities Applicable Not applicable Typical sizes for signs...