DOC PREVIEW
UD CISC 181 - Lecture 3 Part 1

This preview shows page 1-2-3-21-22-23-42-43-44 out of 44 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 Part 1 September 8C++ VariablesData Types: Display 1.2 Simple Types (1 of 2)Data Types: Display 1.2 Simple Types (2 of 2)Assigning DataData Assignment RulesLiteral DataArithmetic Operators: Display 1.4 Named Constant (1 of 2)Arithmetic Operators: Display 1.4 Named Constant (2 of 2)Clicker Question1.24 ArithmeticSlide 12A couple of Examples2.11 Assignment Operators2.12 Increment and Decrement OperatorsSlide 16Slide 17Slide 18Slide 19fig02_14.cpp (1 of 2)fig02_14.cpp (2 of 2) fig02_14.cpp output (1 of 1)Arithmetic Operators and Operand TypeSlide 23Arithmetic PrecisionArithmetic Precision ExamplesType CastingSlide 27Slide 28Different Clicker QuestionA Question to Ponder…1.25 Decision Making: Equality and Relational OperatorsSlide 32Boolean Expressions: Display 2.1 Comparison OperatorsEvaluating Boolean ExpressionsEvaluating Boolean Expressions: Display 2.2 Truth TablesDisplay 2.3 Precedence of Operators (1 of 4)Display 2.3 Precedence of Operators (2 of 4)Display 2.3 Precedence of Operators (3 of 4)Display 2.3 Precedence of Operators (4 of 4)Precedence Examplesfig01_14.cpp (1 of 2)fig01_14.cpp (2 of 2) fig01_14.cpp output (1 of 2)fig01_14.cpp output (2 of 2)Slide 441CISC181 Introduction to Computer ScienceDr. McCoyLecture 3 Part 1September 81-2C++ Variables•C++ Identifiers–Keywords/reserved words vs. Identifiers–Case-sensitivity and validity of identifiers–Meaningful names!•Variables–A memory location to store data for a program–Must declare all data before use in programCopyright © 2010 Pearson Addison-Wesley. All rights reserved.Data Types: Display 1.2 Simple Types (1 of 2)1-3Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Data Types: Display 1.2 Simple Types (2 of 2)1-4Copyright © 2010 Pearson Addison-Wesley. All rights reserved.1-5Assigning Data•Initializing data in declaration statement–Results "undefined" if you don’t!•int myValue = 0;•Assigning data during execution–Lvalues (left-side) & Rvalues (right-side)•Lvalues must be variables•Rvalues can be any expression•Example:distance = rate * time;Lvalue: "distance"Rvalue: "rate * time" Copyright © 2010 Pearson Addison-Wesley. All rights reserved.1-6Data Assignment Rules•Compatibility of Data Assignments–Type mismatches•General Rule: Cannot place value of one type into variable of another type–intVar = 2.99; // 2 is assigned to intVar!•Only integer part "fits", so that’s all that goes•Called "implicit" or "automatic type conversion" –Literals•2, 5.75, "Z", "Hello World"•Considered "constants": can’t change in programCopyright © 2010 Pearson Addison-Wesley. All rights reserved.1-7Literal Data•Literals–Examples:•2 // Literal constant int•5.75 // Literal constant double•"Z" // Literal constant char•"Hello World" // Literal constant string•Cannot change values during execution•Called "literals" because you "literally typed"them in your program!Copyright © 2010 Pearson Addison-Wesley. All rights reserved.1-8Arithmetic Operators:Display 1.4 Named Constant (1 of 2)•Standard Arithmetic Operators–Precedence rules – standard rulesCopyright © 2010 Pearson Addison-Wesley. All rights reserved.Arithmetic Operators:Display 1.4 Named Constant (2 of 2)1-9Copyright © 2010 Pearson Addison-Wesley. All rights reserved.Clicker Question•What is the output of the following program lines when they are embedded in a correct program that declares all variables of type char?a = ‘b’;b = ‘c’;c = a;cout << a << b << c << ‘c’;ANS: (1) a b c c(2) b c a c(3) b c b c(4) a b a c1-10 2003 Prentice Hall, Inc. All rights reserved.111.24 Arithmetic•Arithmetic calculations–* •Multiplication –/ •Division•Integer division truncates remainder–7 / 5 evaluates to 1–%•Modulus operator returns remainder –7 % 5 evaluates to 2 2003 Prentice Hall, Inc. All rights reserved.121.24 Arithmetic•Rules of operator precedence–Operators in parentheses evaluated first•Nested/embedded parentheses–Operators in innermost pair first–Multiplication, division, modulus applied next•Operators applied from left to right–Addition, subtraction applied last•Operators applied from left to rightOperator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right. *, /, or % Multiplication Division Modulus Evaluated second. If there are several, they re evaluated left to right. + or - Addition Subtraction Evaluated last. If there are several, they are evaluated left to right.13A couple of Examples•Z = a + b * c / d – e % f;•Z = (a + b) * ((c / d) – e) % f;•Z = p + q * (r – z) + ( s + ( 3 + 7 * 2)) – r; 2003 Prentice Hall, Inc. All rights reserved.142.11 Assignment Operators•Assignment expression abbreviations–Addition assignment operator c = c + 3; abbreviated to c += 3; •Statements of the formvariable = variable operator expression;can be rewritten asvariable operator= expression;•Other assignment operatorsd -= 4 (d = d - 4)e *= 5 (e = e * 5)f /= 3 (f = f / 3)g %= 9 (g = g % 9) 2003 Prentice Hall, Inc. All rights reserved.152.12 Increment and Decrement Operators•Increment operator (++) - can be used instead of c += 1•Decrement operator (--) - can be used instead of c -= 1–Preincrement•When the operator is used before the variable (++c or –c)•Variable is changed, then the expression it is in is evaluated.–Posincrement•When the operator is used after the variable (c++ or c--)•Expression the variable is in executes, then the variable is changed. 2003 Prentice Hall, Inc. All rights reserved.162.12 Increment and Decrement Operators•Increment operator (++)–Increment variable by one–c++•Same as c += 1•Decrement operator (--) similar –Decrement variable by one–c-- 2003 Prentice Hall, Inc. All rights reserved.172.12 Increment and Decrement Operators•Preincrement–Variable changed before used in expression•Operator before variable (++c or --c)•Postincrement–Incremented changed after expression•Operator after variable (c++, c--) 2003 Prentice Hall, Inc. All rights reserved.182.12 Increment and Decrement Operators•If c = 5, then –cout << ++c; •c is changed to


View Full Document
Download Lecture 3 Part 1
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 3 Part 1 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 3 Part 1 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?