Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
String Escape Sequences
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Basic Elements of C++ Chapter 2.
Input & Output: Console
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
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 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
CPS120: Introduction to Computer Science Variables and Constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Chapter 2. Variable and Data type
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
A Sample Program #include using namespace std; int main(void) { cout
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
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
Chapter # 2 Part 2 Programs And data
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
Computing Fundamentals
Chapter 2: Problem Solving Using C++
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Data Types, Identifiers, and Expressions
Revision Lecture
Basic Elements of C++ Chapter 2.
Chapter 2 Elementary Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
C++ Data Types Data Type
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ Programming Basics
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Variables A variable is a placeholder for a value. It is a named memory location where that value is stored. Use the name of a variable to access or update the value. View memory as a numbered sequence of bytes, where the number is the address of each byte. Later, we'll see how you can access the value through its address.

Variables The syntax for declaring a variable is: data_type variable_name ; or, for several variables of the same type: data_type variable_name1, variable_name2 ; Variables may be declared at any point in the program, but they must always be declared before they are used. The name of a variable is a sequence of letters, digits and underscores that does not begin with a digit. C++ is case sensitive

Readability Pick meaningful variable names that describe the entity they refer to. Things to avoid: the letter l (el) which can be confused with the digit 1 (one) single-letter variables (there's one exception) variable names starting with one or two underscores (many internal C++ names follow this convention) extremely long names names similar to C++ reserved or predefined words names similar to one another

Basic types int for signed integers (4 bytes*) range: -231..+231 unsigned int for unsigned integers (4 bytes*) range: 0..+232 float and double for single- and double-precision floating point numbers (4 and 8 bytes respectively) range: ± ~10-44.85 to ~1038.53 for floats range: ± ~10-323.3 to ~10308.3 for doubles char for characters (1 byte) range: -128..+127 but ASCII uses only 0..127 bool for boolean values (1 byte) range: 0-1 * for our architecture

Literals integer literal double literal string literals #include<iostream> using namespace std; int main () { cout << 10 << "USD = " << 8.2 << "EUR\n"; return 0; } integer literal double literal string literals

Literals Major disadvantages: Good idea: Look ma, no semicolon! a literal cannot be reused (you have to type it in every time) it is easy to make a typo if you want to change it, you have to make sure you change all of its occurrences inside the program. Good idea: Use #define (C++ preprocessor directive) to define a literal Look ma, no semicolon! #define NUM_DOLLARS 10 #include<iostream> using namespace std; int main () { cout << NUM_DOLLARS; return 0; }

#defined constants Location: with other directives (e.g. #include) Syntax: #define UNIQUE_NAME some_value Before compiling, pre-processor does 'find & replace' on your file: every occurrence of UNIQUE_NAME is replaced by some_value. Convention: UNIQUE_NAME is always in UPPERCASE. Major advantages: One single, central location for fixed values prevents scattered, hard-to-find errors in literals makes it easy to modify the value Works for ANY repeated text, not just numbers string literals, even executable text statements…

named constants Declared like variables BUT preceded with the keyword const initialized at declaration their value CANNOT change afterwards. #include<iostream> using namespace std; int main () { const int num_dollars = 10; cout << num_dollars; // num_dollars = 20; ILLEGAL! return 0; }

Operators Assignment operator: = Arithmetic operators: +, , *, /, % Relational operators: <, >, <=, >=, ==, != Logical operators: &&, ||, !

Operators ( ) ! Unary – * / % higher precedence + – * / % + – < <= >= > = = != && || = higher precedence lower precedence Associativity: left-to-right (except = and unary operators)