1 / 21

Today’s Lecture

Today’s Lecture. Variable Arithmetic Operators Data type. C++ Variables. A name used to represent data that can be changed while the program or procedure is running Example Int x; char y; A variable must have a data type

Download Presentation

Today’s Lecture

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. Today’s Lecture • Variable • Arithmetic Operators • Data type

  2. C++ Variables • A name used to represent data that can be changed while the program or procedure is running • Example Int x; char y; • A variable must have a data type • A variable must be one word, but cannot be keyword or reserved word • Keyword (reserved word): Appendix I • Example: int, if, else, …

  3. Data Types: Display 1.2 Simple Types (1 of 2)

  4. Data Types: Display 1.2 Simple Types (2 of 2)

  5. C++ Variables • Example int age = 7; float salary = 4680.76; char input = ‘A’; bool pass_exam_c101 = true;

  6. C++ Operators • Assignment operator = (assignment) • Arithmetic operators + (addition) - (subtraction) * (multiplication) / (division) % (modulo, reminder)

  7. C++ arithmetic operator • % operator 10 % 5 equals 0 10 % 3 equals 1 3 % 10 equals 3

  8. Assigning Data • Initializing data in declaration statement • 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"

  9. Data Assignment Rules • Compatibility of Data Assignments • Type mismatches • General Rule: Cannot place value of one type into variable of another type • Int Var = 2.99; // 2 is assigned to Var! • Only integer part "fits", so that’s all that goes • Called "implicit" or "automatic type conversion"

  10. Arithmetic Precision • Precision of Calculations • "Highest-order operand" determines typeof arithmetic "precision" performed

  11. Arithmetic Precision Examples • Examples: • 17 / 5 evaluates to 3 in C++! • Both operands are integers • Integer division is performed! • 17.0 / 5 equals 3.4 in C++! • Highest-order operand is "double type" • Double "precision" division is performed! • int intVar1 =1, intVar2=2;intVar1 / intVar2; • Performs integer division! • Result: 0!

  12. Individual Arithmetic Precision • Calculations done "one-by-one" • 1 / 2 / 3.0 / 4 performs 3 separate divisions. • First 1 / 2 equals 0 • Then 0 / 3.0 equals 0.0 • Then 0.0 / 4 equals 0.0

  13. Type Casting • Two types • Implicit—also called "Automatic" • Done FOR you, automatically17 / 5.5This expression causes an "implicit type cast" totake place, casting the 17  17.0 • Explicit type conversion • Programmer specifies conversion with cast operator(double)17 / 5.5 Same expression as above, using explicit cast(double)myInt / myDouble More typical use; cast operator on variable

  14. Type Casting • Example • 17/5 equals 3 • (double)17/5 equals 3.4

  15. Console Input/Output • I/O objects cin, cout • Defined in the C++ library called<iostream> • Must have these lines (called pre-processor directives) near start of file: • #include <iostream>using namespace std; • Tells C++ to use appropriate library so we canuse the I/O objects cin, cout

  16. Console Output • What can be outputted? • Any data can be outputted to display screen • Variables • Expressions (which can include all of above) • cout << numberOfGames << " games played.";2 values are outputted: "value" of variable numberOfGames, string " games played."

  17. Separating Lines of Output • New lines in output • Recall: "\n" is escape sequence for the char "newline" • A second method: object endl • Examples: cout << "Hello World\n"; cout << "Hello World" << endl; • Same result as above

  18. Input Using cin • cin for input, cout for output • cin >> num; • Waits on-screen for keyboard entry • Value entered at keyboard is "assigned" to num

  19. Prompting for Input: cin and cout • Every cin should have cout prompt in order to maximizes user-friendly input/output cout << "Enter number of dragons: "; cin >> numOfDragons; • Note no "\n" in cout. Prompt "waits" on sameline for keyboard input as follows: Enter number of dragons: ____ • Underscore above denotes where keyboard entryis made

  20. Arithmetic Operators:Display 1.4 Named Constant (1 of 2) • Standard Arithmetic Operators • Precedence rules – standard rules

  21. Arithmetic Operators:Display 1.4 Named Constant (2 of 2)

More Related