Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.

Similar presentations


Presentation on theme: "Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators."— Presentation transcript:

1 Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators

2 2 double int * cin / variable data types

3 We have done some programming last week. We are left with some un-answered questions. We will try to re-visit these questions this week. ▫So programming is not mysterious, and is EASY! 3

4 Objectives Distinguish among a variable, a named constant, and a literal constant Explain how data is stored in memory Declare and initialize a memory location Use an assignment statement to assign data to a variable 4

5 Objectives (continued) Include arithmetic operators and arithmetic assignment operators in a statement Get string input using the getline() function Ignore characters using the ignore() function Format floating-point output 5

6 More Problem-Solving Process 6

7 Variables and Named Constants First question – What are variables? Declare a memory location for each input, processing, and output item in IPO chart ▫A variable is a type of memory location whose contents can change while program is running ▫Values of named constant items remain the same each time the program is executed 7

8 Variables and Named Constants 8 Requires four memory locations:  Two input items radius  variable pi  named constant  One processing item radius squared  variable  One output item area  variable

9 Selecting a Name for a Memory Location Names – No Spaces! Identifiers should be descriptive and follow some rules: 9

10 Selecting a Name for a Memory Location Most programmers: ▫Use uppercase letters for named constants ▫Use lowercase letters for variables ▫Use camel case if a variable’s name contains two or more words 10

11 Selecting a Name for a Memory Location 11

12 Selecting a Data Type for a Memory Location 12 These data types, except string, are fundamental data types What are Data Types!

13 Selecting a Data Type for a Memory Location string is a class ▫Program must include: #include using std::string; 13

14 Selecting a Data Type for a Memory Location C++ contains one or more data types for storing ▫Integers (whole numbers) ▫Floating-point numbers (with a decimal place) ▫Characters (letters, symbols, and numbers that will not be used in calculations) ▫Boolean values (true and false) 14

15 Selecting a Data Type for a Memory Location The data type to use for a memory location depends on the values it will store 15

16 How Data is Stored? Many questions about data types can be answered if there’s understanding of how it is stored in the computer’s memory. Questions such as “what’s the difference between double and float ?” 16

17 How Data is Stored in Internal Memory 17

18 How Data is Stored in Internal Memory 18

19 Selecting an Initial Value for a Memory Location To initialize is to assign an initial value to a memory location ▫Typically a literal constant  Type can be: numeric, character, or string ▫A location with bool data type can be initialized with keywords true or false ▫Typical initialization values  0  0.0  ‘ ‘  “”  true, false 19

20 Selecting an Initial Value for a Memory Location 20

21 Type Conversions Need to understand this for next laboratory session Implicit type conversions can occur when assigning a value to a memory location ▫Or, when processing calculation statements ▫Value can be promoted or demoted  Implicit demotion can adversely affect output Use explicit type conversion (type casting) to convert an item from one data type to another ▫static_cast operator 21

22 22

23 23

24 Declaring a Named Constant 24

25 Declaring a Variable 25

26 Using an Assignment Statement to Store Data in a Variable 26

27 Using an Assignment Statement to Store Data in a Variable 27

28 Arithmetic Operators Basics of arithmetic operations are similar to how you would write in your mathematics class! Precedence numbers indicate order in which computer performs the operation in an expression ▫Use parentheses to override order of precedence 28

29 Arithmetic Operators 29 These are different!

30 Arithmetic Operators (continued) 30

31 Arithmetic Assignment Operators 31

32 Getting Data from the Keyboard Use >> to get numeric, character, or string values from the keyboard and store them in a variable ▫Stops reading characters when it encounters a white- space character in the input  Blank, tab, or newline ▫An alternative is to use getline()  For example, if you want to read a sentence. 32

33 getline() and ignore() Will explain only here, you are expected to try this and figure out in this week’s lab. 33

34 The getline() Function When getline() encounters the delimiter character in the input, it consumes the character 34 Items between parentheses in a function’s syntax are the arguments newline character

35 The ignore() Function ignore() instructs computer to read and consume characters entered at keyboard 35

36 The ignore() Function 36

37 Formatting Floating-Point Numbers Use fixed stream manipulator to display a floating-point number in fixed-point notation #include using std::fixed; Use scientific stream manipulator for e notation #include using std::scientific; Setprecision stream manipulator controls number of decimal places displayed #include using std::setprecision; 37

38 Formatting Floating-Point Numbers 38

39 Formatting Floating-Point Numbers 39

40 Summary Programs have variables, constants (named, literal), and arithmetic operators (to perform calculations) const dataType constantName = value; dataType variableName [= initialValue]; Use assignment statement to store data in a variable variableName = expression; ▫When assigning a value to a memory location, the value should fit the memory location’s data type  Use static_cast operator to convert an item of data from one data type to another 40

41 Summary (continued) Arithmetic operators have a precedence number ▫Use parentheses to override order of precedence Arithmetic assignment operators abbreviate an assignment statement varName arithmeticAssignmentOp expr; getline() gets a string of characters ignore() reads and consumes characters entered at the keyboard fixed and scientific stream manipulators format the display of floating-point numbers 41


Download ppt "Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators."

Similar presentations


Ads by Google