Chapter 2 Primitive Data Types and Operations Primitive

  • Slides: 18
Download presentation
Chapter 2 Primitive Data Types and Operations • Primitive Data types: – Integer data

Chapter 2 Primitive Data Types and Operations • Primitive Data types: – Integer data types: • int • byte • short • long - Floating-point types: • double • float - Character type: • char

 • Identifiers – Variables • must start with a letter or underscore •

• Identifiers – Variables • must start with a letter or underscore • consists of letters, digits & underscores • cannot be a reserved word • Cannot be true, false, or null first. Score score 1 score 3 scores score-1 my. Score • Capitalize “middle” words • Usually start variables with lowercase, classes with uppercase • C++ is case sensitive!!!! Score score

Declaring Variables – C++ is Strongly Typed Syntax: type identifier 1, identifier 2, …;

Declaring Variables – C++ is Strongly Typed Syntax: type identifier 1, identifier 2, …; int num 1; float num 2, num 3; float num 4; Assignment Statement variable = expression; We should initialize our variables. int num 1 = 0; double num 2 = 0. 0, num 3 = 0. 0;

Numeric Operators Arithmetic + - * / % Consider m * x + b

Numeric Operators Arithmetic + - * / % Consider m * x + b • Is it equivalent to (m * x) + b • Is it equivalent to m * (x + b) Expression evaluation rules Use operator precedence! Standard precedence order () Evaluated first, if nested then the innermost pair is evaluated, and so on * / % Evaluated second, from left to right +- Evaluated third, from left to right

 • Trace the program fragment below int total=0, new. Total=0; total = 5;

• Trace the program fragment below int total=0, new. Total=0; total = 5; new. Total = 10; total = new. Total + 8; new. Total = total + 3; new. Total = new. Total + 1; total = total + new. Total; cout << new. Total << endl; cout << total << endl; • Write C++ expressions from arithmetic expressions celsius = (fahrenheit - 32) * 5 / 9;

#include <iostream> using namespace std; int main() { // receive the amount cout <<

#include <iostream> using namespace std; int main() { // receive the amount cout << "Enter an amount in double, for example 11. 56: "; double amount; cin >> amount; int remaining. Amount = static_cast<int>(amount * 100); // find the number of dollars int number. Of. One. Dollars= remaining. Amount / 100; remaining. Amount = remaining. Amount % 100; // find the number of quarters int number. Of. Quarters = remaining. Amount/25; remaining. Amount = remaining. Amount % 25; // find the number of dimes in the remaining amount int number. Of. Dimes = remaining. Amount / 10; remaining. Amount = remaining. Amount % 10; // find the number of nickels in the remaining amount int number. Of. Nickels = remaining. Amount / 5; remaining. Amount = remaining. Amount % 5; // find the number of pennies int number. Of. Pennies = remaining. Amount; // Display the results cout << "Your amount " << amount << " constists of n" << "t" << number. Of. One. Dollars << " dollars n" << "t" << number. Of. Quarters << " quarters n" << "t" << number. Of. Dimes << " dimes n“ << "t" << number. Of. Nickels << " nickels n" << "t" << number. Of. Pennies << " penniesn"; return 0; }

 • A literal is a constant value that appears directly in a program.

• A literal is a constant value that appears directly in a program. int number = 34; double distance = 12. 35; • Character Data Type: represent a single character char first. Letter = 'A'; //initialize a character data type named “first. Letter” with value ‘A’ • A character literal is enclosed in single quotation marks. Which of the following is a character literal? 'a' 'A' ' ' '$' '7' 'ab' ' a ' “a” • A string literal must be enclosed in double quotation marks.

Assignment Compatibilities int -> float -> double Be careful when doing assignments! int sum;

Assignment Compatibilities int -> float -> double Be careful when doing assignments! int sum; double average; sum = 50; sum = 10. 5; average = 50 / 4; average = 50. 4;

Type Casting: temporarily changing a value’s type Some type casting is automatic int num

Type Casting: temporarily changing a value’s type Some type casting is automatic int num 1; double num 2; num 1 = 7; num 2 = num 1; However, we have to be careful: double distance; distance = 9. 0; int points; points = distance; We must cast the value: points = static_cast<int>distance; Assigns the integer part of distance to points. This DOES NOT CHANGE distance! Consider the code: char letter = 7; letter = ‘A’ + 5;

 • ASCII code table: computers use a unique numeric code to represent each

• ASCII code table: computers use a unique numeric code to represent each character • Each printable character (letter, number, punctuation mark, space, and tab) is assigned a different integer code • See Appendix B on page 654 for the ASCII character codes Integer Ascii Value 32 33 48 49 50 65 66 97 98 Character Space ! 0 1 2 A B a b

 • Simple Output must include <iostream> – cout – << is the extraction

• Simple Output must include <iostream> – cout – << is the extraction operator cout cout << << << “One” << endl; “Two” << endl; “Three” << endl; “One”; “Two”; “Three”; • How could we keep the words separated? • We can use the extraction operator to output more than one value. cout << num << “ Cokes”<< endl;

 • Simple Input – cin – >> is the insertion operator int cin

• Simple Input – cin – >> is the insertion operator int cin cin num 1, num 2, num 3 >> num 1; >> num 2; >> num 1 >> num 2; Remember the operator points in the direction of data flow!

Formatting Output We’d like to be able to format floats (and doubles) to limit

Formatting Output We’d like to be able to format floats (and doubles) to limit the number of digits after the decimal point. It’s EASY! Suppose you have the following: double amount = 197. 55 double tax = amount *. 07 cout << “the tax is $” << static_cast<int>(tax * 100)/100. 0; This will drop the decimal amounts beyond the two decimals that we want. Change the multiplier and divisor to change the number of decimal places required.

 • Naming Conventions: (1) variable names should begin with a lowercase letter, capitalize

• Naming Conventions: (1) variable names should begin with a lowercase letter, capitalize the first letter of subsequent words; (2) capitalize the first letter of each word in a class name. • Programming Errors: – We have discussed compiler errors, which are usually syntax errors or omissions. The compiler discovers these errors for us. – When our program compiles successfully, it means that it is free of syntax errors. It does not mean that the program will necessarily run correctly! – Programs which do not run correctly contain runtime errors (which cause the program to terminate abnormally) and/or logic errors (which cause the program to produce incorrect results). We discover these errors by testing the program.

 • Seven steps to good programming habits – – – – Analyze the

• Seven steps to good programming habits – – – – Analyze the problem Develop the algorithm (design!) Write code for the program (implement) Compile the code Run the program Test the results Document the program!

READ CHAPTER 2 – to really understand it all, punch in all sample programs.

READ CHAPTER 2 – to really understand it all, punch in all sample programs. • • • Chapter 2 review questions 2. 1 - 2. 5 2. 9 - 2. 10 2. 12 – 2. 13 2. 27