Download presentation
Presentation is loading. Please wait.
1
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types
2
Today We Are Going To: Introduce the concept of program requirements Introduce the concept of program requirements Develop a strategy for tackling programs Develop a strategy for tackling programs Describe identifiers and their use Describe identifiers and their use Describe the primitive data types used by C++ and their operations Describe the primitive data types used by C++ and their operations Use assignment statements Use assignment statements
3
Topic Tackling a Programming Problem: Project 2.1
4
Step 1: Specify Requirements Usually what is presented to a programmer is a task to be done: Usually what is presented to a programmer is a task to be done: Write a program to convert an input Fahrenheit temperature to Celsius. Begin by making sure you understand the requirements Begin by making sure you understand the requirements Then go on to break requirements into simpler pieces Then go on to break requirements into simpler pieces
5
Step 2: Analyze Next step is to determine what the user of the program is going to see Next step is to determine what the user of the program is going to see identify input quantities identify input quantities identify output quantities identify output quantities For temperature conversion: For temperature conversion: input is an integer number (Fahrenheit) input is an integer number (Fahrenheit) output is an integer number (Celsius) output is an integer number (Celsius)
6
Identifiers Names for entities used in calculation Names for entities used in calculation Rules: Rules: begin with letter (upper or lower case), or an underscore (_) begin with letter (upper or lower case), or an underscore (_) composed of these characters and digits composed of these characters and digits can be of any length can be of any length cannot be a reserved word cannot be a reserved word case sensitive: Area is different from area case sensitive: Area is different from area
7
Identifiers (cont.) Important, but not rules: Important, but not rules: obey naming conventions obey naming conventions make names descriptive make names descriptive In this case let us choose: In this case let us choose: fahrenheit : for the input temp fahrenheit : for the input temp celsius : for the output temp celsius : for the output temp
8
Integer Data Types Integers of different sizes allow programmers to use storage smartly Integers of different sizes allow programmers to use storage smartly signed (first bit indicates + or -) signed (first bit indicates + or -) unsigned (range from 0 to maximum) unsigned (range from 0 to maximum) short: 16 bits (-32768 to 32767) short: 16 bits (-32768 to 32767) int: 32 bits (-2147483648 to 2147483647) int: 32 bits (-2147483648 to 2147483647) long: 64 bits (way bigger) long: 64 bits (way bigger)
9
Other Data Types float: 32 bits (6 or 7 significant digits) float: 32 bits (6 or 7 significant digits) double: 64 bits (14 or 15 significant digits) double: 64 bits (14 or 15 significant digits) char: 2 bytes, or 16 bits char: 2 bytes, or 16 bits boolean: true or false (two values) boolean: true or false (two values)
10
Step 2: Analyze (Complete) Combine knowledge of identifiers and data types to declare quantities Combine knowledge of identifiers and data types to declare quantities For now still just planning this out For now still just planning this out For temperature conversion: For temperature conversion: int fahrenheit; int fahrenheit; int celsius; int celsius;
11
Step 3: Design Describe the calculation in detail Describe the calculation in detail Prompt the user for input Prompt the user for input Read input number: fahrenheit temp Read input number: fahrenheit temp Convert to celsius by: Convert to celsius by: subtracting 32 subtracting 32 multiplying by 5/9 multiplying by 5/9 Display the result Display the result
12
Step 4: (Begin) Implementing Now transfer your thoughts into the file Now transfer your thoughts into the file Create the project Create the project Create program structure Create program structure Declare variables Declare variables Write COMMENTS that describe design Write COMMENTS that describe design
13
Variable Declarations First step is to declare the variables that have been identified thus far First step is to declare the variables that have been identified thus far (Note that you must expect to have to add to that list later.) (Note that you must expect to have to add to that list later.) Identify other quantities that can be declared as constants Identify other quantities that can be declared as constants In this case the conversion factors would make good constants In this case the conversion factors would make good constants
14
Literals Literal: way to represent a data value explicitly Literal: way to represent a data value explicitly boolean: true or false boolean: true or false integers: enter number of appropriate size; use l or L to specify long values integers: enter number of appropriate size; use l or L to specify long values char: enclose in single quotes → 'A' char: enclose in single quotes → 'A' string (not a primitive type) double quotes → “The quick brown fox...” string (not a primitive type) double quotes → “The quick brown fox...”
15
Literals (cont.) special characters (in chars and strings): special characters (in chars and strings): quotes: must “escape” compiler’s interpretation in order to display an actual quote character quotes: must “escape” compiler’s interpretation in order to display an actual quote character other special characters include tabs, linefeeds, and backslashes other special characters include tabs, linefeeds, and backslashes backslash (\) is the escape character: backslash (\) is the escape character: → \” will display an actual double-quote → \” will display an actual double-quote
16
Literals (cont.) floating point types: floating point types: decimal notation: 1.45 decimal notation: 1.45 exponential notation: 1.3E-5 exponential notation: 1.3E-5 to specify float append f or F: 126f to specify float append f or F: 126f to specify double append d or D: 126d to specify double append d or D: 126d
17
Variable Initialization In most cases need to give a variable an initial value in addition to declaring it In most cases need to give a variable an initial value in addition to declaring it This is critical for constants This is critical for constants In our case do not need to initialize the variables fahrenheit and celsius, but we will have to initialize our constants In our case do not need to initialize the variables fahrenheit and celsius, but we will have to initialize our constants const double FACTOR = 1.8; const double OFFSET = 32.0;
18
Step 4: (Cont.) Implementing Have specified quantities Have specified quantities At this point it is probably easiest to specify the program output At this point it is probably easiest to specify the program output Now go back to your comments and write code which will execute the actions you describe Now go back to your comments and write code which will execute the actions you describe
19
Operators An operator is a symbol or sequence of symbols that causes an action to happen An operator is a symbol or sequence of symbols that causes an action to happen Expression: an action containing a sequence of operators and variables Expression: an action containing a sequence of operators and variables
20
Numerical/Mathematical Operators Ordinary mathematics: Ordinary mathematics: multiplication: * multiplication: * division: / division: / addition: + addition: + subtraction: - subtraction: - unary negation: - unary negation: - remainder (integers): % remainder (integers): %
21
Numerical/Mathematical Operators (cont.) Increment/Decrement: Increment/Decrement: var++; // increment by one AFTER use ++var; // increment by one BEFORE use var--; // decrement by one AFTER use --var; // decrement by one BEFORE use
22
Numerical/Mathematical Operators (cont.) Assignment shortcuts: Assignment shortcuts: += adds expression on right to the one on the left, assigns result to name on left -= subtracts right from left, assigns result to variable on left *= multiplies right and left, assigns result to variable on left /= divides left by right, assigns result to variable on left
23
Boolean/Logical Operators Testing for true and false: Testing for true and false: and: && and: && or: || or: || exclusive or: ^ exclusive or: ^ logical negation: ! logical negation: ! comparisons:, =, ==, != comparisons:, =, ==, !=
24
Other Operators String concatenation: + String concatenation: + Class-member access:. Class-member access:. Precedence: ( ) Precedence: ( )
25
Operator Precedence and Evalutation Order Rules are too much to review here; look for them in your book Rules are too much to review here; look for them in your book Pay attention to them; they will be the source of many difficult bugs Pay attention to them; they will be the source of many difficult bugs Remember that you can control order with the “precedence operator”: ( ) Remember that you can control order with the “precedence operator”: ( )
26
Step 4: (Finish) Implementing Complete the implementation by coding the actions Complete the implementation by coding the actions Note that in this case we can combine them onto one line Note that in this case we can combine them onto one line celsius = (fahrenheit – OFFSET)/FACTOR;
27
Step 5a: Compile The first step in validating your code is the compilation step The first step in validating your code is the compilation step Run the compilation Run the compilation Interpret error messages Interpret error messages Fix problems, starting at TOP and recompiling frequently Fix problems, starting at TOP and recompiling frequently (Remember, one mistake may cause several errors) (Remember, one mistake may cause several errors)
28
Step 5b: Test Compilation is crucial, but you must also check for other kinds of errors Compilation is crucial, but you must also check for other kinds of errors Doing so requires that you run your program several times to prove that you get the right answer Doing so requires that you run your program several times to prove that you get the right answer 32°F is 0°C 32°F is 0°C 212°F is 100°C 212°F is 100°C 86°F is 30°C 86°F is 30°C
29
Step 5b: Test (cont.) Can see that there is something going wrong with our program – can you guess what it is? Can see that there is something going wrong with our program – can you guess what it is? Have a round-off problem Have a round-off problem For now we can solve that problem by adding 0.5 to the result of our calculation For now we can solve that problem by adding 0.5 to the result of our calculation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.