C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.

Slides:



Advertisements
Similar presentations
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Advertisements

Introduction to C Programming
Introduction to C Programming
C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 2: Introduction to C++.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Chapter Two: Fundamental Data Types
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Input, Output, and Processing
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
IB Computer Science Unit 1 – Introduction to Java Variables.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Today’s Lecture  Literal  Constant  Precedence rules  More assignment rules  Program Style.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter Two: Fundamental Data Types
Fundamental Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Introduction to Computer Science / Procedural – 67130
Introduction to Programming
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Basic Elements of C++ Chapter 2.
Fundamental Data Types
2.1 Parts of a C++ Program.
Introduction to Programming
Wednesday 09/23/13.
Introduction to Programming
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Introduction to Programming
Presentation transcript:

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13

Reading Read Chapter 2

C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Goals To be able to define and initialize variables and constants To understand the properties and limitations of integer and floating-point numbers To write arithmetic expressions and assignment statements in C++ To appreciate the importance of comments and good code layout To create programs that read and process input, and display the results To process strings, using the standard C++ string type

Variables A variable –is used to store information: can contain one piece of information at a time. –has an identifier: – The programmer picks a good name A good name describes the contents of the variable or what the variable will be used for the contents of the variable: the name of the variable C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variables Parking garages store cars. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Each parking space is identified – like a variable’s identifier Variables A each parking space in a garage “contains” a car – like a variable’s current contents. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variables and each space can contain only one car and only cars, not buses or trucks C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variable Definitions When creating variables, the programmer specifies the type of information to be stored. –(more on types later) Unlike a parking space, a variable is often given an initial value. –Initialization is putting a value into a variable when the variable is created. –Initialization is not required. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The following statement defines a variable. int cans_per_pack = 6; cans_per_pack i s the variable’s name. Variable Definitions int indicates that the variable cans_per_pack will be used to hold integers. = 6 indicates that the variable cans_per_pack will initially contain the value 6. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Must obey the rules for valid names Variable Definitions C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variable Definitions C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Number Types A number written by a programmer is called a number literal. There are rules for writing literal values: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Number Types A number written by a programmer is called a number literal. There are rules for writing literal values: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Number Types C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variable Names When you define a variable, you should pick a name that explains its purpose. For example, it is better to use a descriptive name, such as can_volume, than a terse name, such as cv. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variable Names In C++, there are a few simple rules for variable names: C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variable Names – Rules apply to all identifiers 1.Variable names must start with a letter or the underscore (_) character, and the remaining characters must be letters numbers, or underscores. 2.You cannot use other symbols such as $ or %. Spaces are not permitted inside names; you can use an underscore instead, as in can_volume. 3.Variable names are case-sensitive, that is, can_volume and can_Volume are different names. For that reason, it is a good idea to use only lowercase letters in variable names. 4.You cannot use reserved words such as double or return as names; these words are reserved exclusively for their special C++ meanings. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Variable Names C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Which of these names are valid? taxes2013 _cattle_guard 99bottles main News news C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Assignment Statement The contents in variables can “vary” over time (hence the name!). Variables can be changed by –assigning to them The assignment statement –using the increment or decrement operator –inputting into them The input statement C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Assignment Statement An assignment statement stores a new value in a variable, replacing the previously stored value. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

cans_per_pack = 8; This assignment statement changes the value stored in cans_per_pack to be 8. The previous value is replaced. The Assignment Statement C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Assignment Statement C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Assignment Statement There is an important difference between a variable definition and an assignment statement: int cans_per_pack = 6; // Variable definition... cans_per_pack = 8; // Assignment statement The first statement is the definition of cans_per_pack. The second statement is an assignment statement. An existing variable’s contents are replaced. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Assignment Statement The = in an assignment does not mean the left hand side is equal to the right hand side as it does in math. = is an instruction to do something: copy the value of the expression on the right into the variable on the left. Consider what it would mean, mathematically, to state: counter = counter + 2; counter EQUALS counter + 2 ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Start Here 9/11/13

Question from Last Time First: question about concatenation. Let's look at /ch2/concatenation.

The Assignment Statement counter = 11; // set counter to 11 counter = counter + 2; // increment C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

counter = 11; // set counter to 11 counter = counter + 2; // increment 1.Look up what is currently in counter (11) The Assignment Statement C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The Assignment Statement counter = 11; // set counter to 11 counter = counter + 2; // increment 1.Look up what is currently in counter (11) 2.Add 2 to that value (13) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

counter = 11; // set counter to 11 counter = counter + 2; // increment 1.Look up what is currently in counter (11) 2.Add 2 to that value (13) 3.copy the result of the addition expression into the variable on the left, changing counter cout << counter << endl; 13 is shown The Assignment Statement counter = 11; // set counter to 11 counter = counter + 2; // increment 1.Look up what is currently in counter (11) 2.Add 2 to that value (13) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Constants Sometimes the programmer knows certain values just from analyzing the problem, for this kind of information, programmers use the reserved word const. The reserved word const is used to define a constant. A const is a variable whose contents cannot be changed and must be set when created. (Most programmers just call them constants, not variables.) Constants are commonly written using capital letters to distinguish them visually from regular variables: const double BOTTLE_VOLUME = 2; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Constants Another good reason for using constants: double volume = bottles * 2; What does that 2 mean? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Constants If we use a constant there is no question: double volume = bottles * BOTTLE_VOLUME; Any questions? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Constants And still another good reason for using constants: double bottle_volume = bottles * 2; double can_volume = cans * 2; What does that 2 mean? — WHICH 2? That 2 is called a “magic number” (so is that one) because it would require magic to know what 2 means. It is not good programming practice to use magic numbers. Use constants. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Constants And it can get even worse … Suppose that the number 2 appears hundreds of times throughout a five-hundred-line program? Now we need to change the BOTTLE_VOLUME to 2.23 (because we are now using a bottle with a different shape) How to change only some of those magic numbers 2’s? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Constants Constants to the rescue! const double BOTTLE_VOLUME = 2.23; const double CAN_VOLUME = 2;... double bottle_volume = bottles * BOTTLE_VOLUME; double can_volume = cans * CAN_VOLUME; (Look, no magic numbers!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Comments Comments are explanations for human readers of your code (other programmers). The compiler ignores comments completely. double can_volume = 0.355; // Liters in a 12-ounce can Comment C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Comments double can_volume = 0.355; // Liters in a 12-ounce can This just in… The number of liters in a twelve ounce can is 355 one hundredths This newsbreak brought to you by Cay’s Cans Corp. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Comments Comments can be written in two styles: Single line: double can_volume = 0.355; // Liters in a 12-ounce can The compiler ignores everything after // to the end of line Multiline for longer comments: /* This program computes the volume (in liters) of a six-pack of soda cans. */ C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

/* This program computes the volume (in liters) of a six-pack of soda cans and the total volume of a six-pack and a two-liter bottle. */ int main() { int cans_per_pack = 6; const double CAN_VOLUME = 0.355; // Liters in a 12-ounce can double total_volume = cans_per_pack * CAN_VOLUME; cout << "A six-pack of 12-ounce cans contains " << total_volume << " liters." << endl; const double BOTTLE_VOLUME = 2; // Two-liter bottle total_volume = total_volume + BOTTLE_VOLUME; cout << "A six-pack and a two-liter bottle contain " << total_volume << " liters." << endl; return 0; } Notice All the Issues Covered So Far ch02/volume1.cpp C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Using Undefined Variables You must define a variable before you use it for the first time. For example, the following sequence of statements would not be legal: double can_volume = 12 * liter_per_ounce; double liter_per_ounce = ; ? ? Statements are compiled in top to bottom order. When the compiler reaches the first statement, it does not know that liter_per_ounce will be defined in the next line, and it reports an error. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Using Uninitialized Variables Initializing a variable is not required, but there is always a value in every variable, even uninitialized ones. Some value will be there, the flotsam left over from some previous calculation or simply the random value there when the transistors in RAM were first turned on. int bottles; // Forgot to initialize int bottle_volume = bottles * 2; What value would be output from the following statement? cout << bottle_volume << endl; // Unpredictable // Result is unpredictable C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Write a Program Write a program to convert ounces to grams. It should input ounces then output the equivalent in grams. There are grams in an ounce. Put this conversion factor in your program as a constant.