Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Types and Arithmetic Operators
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Data types and variables
The Fundamentals of C++ Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Introduction to Python
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
2440: 211 Interactive Web Programming Expressions & Operators.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
Chapter 2: Using Data.
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Primitive Variables.
Chapter 2 Variables.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
1 Comments Allow prose or commentary to be included in program Importance Programs are read far more often than they are written Programs need to be understood.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Operators and Expressions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to C++ Programming
Chapter 2 Variables.
elementary programming
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
The Fundamentals of C++
Presentation transcript:

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types

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

Topic Tackling a Programming Problem: Project 2.1

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

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)

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

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

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 ( to 32767) short: 16 bits ( to 32767) int: 32 bits ( to ) int: 32 bits ( to ) long: 64 bits (way bigger) long: 64 bits (way bigger)

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)

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;

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

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

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

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...”

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

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

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;

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

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

Numerical/Mathematical Operators Ordinary mathematics: Ordinary mathematics: multiplication: * multiplication: * division: / division: / addition: + addition: + subtraction: - subtraction: - unary negation: - unary negation: - remainder (integers): % remainder (integers): %

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

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

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:, =, ==, !=

Other Operators String concatenation: + String concatenation: + Class-member access:. Class-member access:. Precedence: ( ) Precedence: ( )

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”: ( )

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;

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)

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

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