Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.

Slides:



Advertisements
Similar presentations
Class 2 ssh, memory, data types, variables
Advertisements

Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
Types and Variables. Computer Programming 2 C++ in one page!
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Structure of a C program
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
String Escape Sequences
Basic Elements of C++ Chapter 2.
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
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Input & Output: Console
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
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.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 2 Variables.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
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
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter Topics The Basics of a C++ Program Data Types
Variables Mr. Crone.
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Basic Elements of C++.
Multiple variables can be created in one declaration
Variables and Primative Types
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
C++ Data Types Data Type
Chapter 2: Introduction to C++.
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Chapter 4 Literals, Variables and Constants

#Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value Any numeric literal starting with 0 is an octal value

#Page3 4.1 Literals Character literals - single characters placed between apostrophes ( ‘ ) String literals - one or more characters placed between quotes ( “ ) Usually, treat single character as a character literal

#Page4 4.2 Escape Sequences Escape sequence - exception to the rule that literals are interpreted exactly as they are written Escape sequences start with a backslash ( \ ) followed by a single character Two types of escape sequences Character Numeric

#Page Character Escape Sequences Can be embedded in a string literal or be used as a character literal Null character - a special character used, among other things, to give a character variable an initial value Escape Sequence Character Representation \n Carriage return and line feed (new line) \t Tab (eight characters wide) \"Double quote \'Single quote \\Backslash \0Null character

#Page Character Escape Sequences cout << "This is on one line\n This is on another\n"; cout << "\tHe said, \"Stop!\""; // Output This is on one line This is on another He said, "Stop!" cout << "This is an apostrophe: "; cout << '\''; // Output This is an apostrophe: '

#Page7 4.3 Variable Declarations Variable - a placeholder whose contents can change Everything must be declared before it is used

#Page8 4.3 Variable Declarations A variable declaration has several purposes: informs operating system how much internal memory (RAM) the variable will need identifies the memory address to use for that variable identifies the type of data to be stored in that physical memory location indicates what operations (i.e., +, -, /, etc.) can be performed on the data contained within that variable

#Page9 4.3 Variable Declarations Basic declaration syntax identifier; Data types discussed in the next section Identifier - the variable name int salary; // Notice the semicolon

#Page Variable Declarations Multiple variables can be declared in the same statement int age, iq, shoe_size; Variables can be declared anywhere as long as they are declared before used

#Page Variable’s Initial Value When declared, its initial value is unknown Important to provide an initial value for all variables Initialization - process of giving a variable a value during its declaration - resulting in the variable always being in a known state int sum = 0; int Ralphs_age = RETIREMENT_AGE; int Randys_age = Ralphs_age - 26;

#Page Variable’s Initial Value Can initialize a variable to another variable’s value identifier2 = identifier; int base_salary = 30000; int num_dependents, staff_salary = base_salary;

#Page Variable’s Initial Value Another form of initialization - uses parentheses instead of the assignment operator int base_salary( ); int num_dependents, staff_salary( base_salary );

#Page Initialization Always know the state, or value, of all variables Variables should always be initialized Variables, even characters, are usually initialized to 0

#Page Data Types A data type: Specifies how much memory a variable will take up in memory Indicates operations that can be performed on the variable Primitive data type - data type whose definition is built into the language

#Page Data Types C++ Data Type Description of Data Memory Allocated Range charCharacter1 byte-128 to 127 intIntegerOS Dependent floatFloating point (decimal)4 bytes 3.4E +/- 38 with 7 digits of accuracy double Double precision floating point 8 bytes 1.7E +/- 308 with 15 digits of accuracy boolBoolean data1 bytetrue or false short (or short int) Smaller integer2 bytes–32,768 to 32,767 longLarger integer4 bytes –2,147,483,648 to 2,147,483,647 long doubleLarger double8 bytes 1.7E +/- 308 with 15 digits of accuracy

#Page Data Types Boolean value - either true or false Size of an integer ( int ) - dependent upon the operating system On a 16-bit operation system such as Windows 3.x, an integer is 16 bits, or 2 bytes On a 32-bit operation system (Windows XP), an integer is 32 bits, or 4 bytes

#Page Data Types Size of an integer ( int ) - dependent upon the operating system (continued) On a 64-bit operation system - some versions of Windows Vista - an integer is 64 bits, or 8 bytes

#Page Data Types The amount of memory an integer requires determines the range of values In a 32-bit operating system - since a bit can have one of two values - there will be 2 32 different possibilities

#Page Data Types Most significant bit is used as a sign bit Zero meaning the number is positive One means its negative Therefore, left with 31 bits, or 2 31 different values

#Page Data Types Unsigned prefix for integral data types - the sign bit is used for data instead of the sign Integral data type - only holds whole numbers A char data type is an integral data type Under the hood a char holds an ASCII number representing a character Use smallest data type that will work with the data

#Page The sizeof Operator sizeof operator - determines number of bytes required for a specific data type // Part 1 cout << sizeof( char ) << '\n'; // Part 2 unsigned short age = 21; cout << sizeof( age ) << '\n'; // Output 1 2

#Page Numeric Literal Suffixes Numeric literal suffix - special character used to specify the type of literal Numeric literal with an F suffix specifies a float, while L specifies a long value

#Page Numeric Literal Suffixes Either case will work for suffixes – but use capitals to avoid confusion between lower case l and a numeric 1 float money = F;// Flt pt (4 bytes) numeric float avg = 95.5f; // literals are treated as long flag = 0L; // doubles (8 bytes) // Last character is not a one but a lowercase l long salary = 50000l;

#Page Naming Rules Variable naming rules: Only made up of letters, digits and underscores Can’t start with a digit (must begin with a letter or underscore) Can’t be a reserved word ( if, else, while, etc.) Variable names should be descriptive, aiding in code readability

#Page ASCII Characters ASCII chart - associates characters with a number American Standard Code for Information Interchange (ASCII)

#Page ASCII Characters Allow for the storage of characters in memory Some important ASCII values: 65 = ‘A’ 97 = ‘a’ 32 = ‘ ’ 48 = ‘0’

#Page ASCII Characters To display characters given an ASCII value use numeric escape sequences cout << "Hexadecimal ASCII character: " << "\x4E" << endl; cout << "Octal ASCII character: " << "\77" << endl; cout << "Hexadecimal number: " << 0x4E << endl; cout << "Octal number: " << 077 << endl; //Output Hexadecimal ASCII character: N Octal ASCII character: ? Hexadecimal number: 78 Octal number: 63

#Page Constants Constants - identifiers that have a value that will never change Aid in code readability and maintainability Should have a name that is descriptive of their purpose const int SPEED_LIMIT = 65; const int RETIREMENT_AGE = 67; const double PI = ;

#Page const versus #define To declare constants use the #define preprocessor directive #define SPEED_LIMIT 65 // Notice no = or semicolons #define RETIREMENT_AGE 67 #define PI 3.14 Preprocessor searches through the code replacing the identifier with the value associated with it

#Page const versus #define #define statements can cause compilation errors while looking syntactically correct #define PI = 3.14; // Notice the = and ; int main() { int circumference = 0, radius = 5; circumference = 2 * PI * radius; return 0; }

#Page const versus #define Although the statement looks correct, it causes a compilation error circumference = 2 * PI * radius; Error becomes clearer if we show what was created by the preprocessor circumference = 2 * = 3.14; * radius;

#Page const versus #define Use const versus #define because: const uses a data type and participates in type checking const has scope

#Page Bringing It All Together Useful to picture how variables and constants might be placed in memory Examine the declarations below: short int age; char grade = 'A'; float gpa(0.0); const float PI = 3.14;

#Page Bringing It All Together They may be placed in memory as shown below: ??A age grade gpa PI

#Page36 Remember, pseudocode is a language independent representation of an algorithm Using data types has a tendency to make the solution to closely tied to C++ (or any other language) 4.8 Variable Declarations in Pseudocode

#Page Variable Declarations in Pseudocode Do not put variable declarations in pseudocode #include s are not specified in pseudocode and are considered necessary overhead to the algorithm

4.10 C – The Differences C doesn’t have a Boolean data type (and no true or false ) Doesn’t allow for the use of parentheses to initialize variables or constants In older versions of C, variables must be declared as the first statement in a block of code (after an opening curly brace)

4.10 C – The Differences Current C standard allows a programmer to use const to create constants Legacy C programs written must use the #define to create constants