Variables Mr. Crone.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
// A simple C++ program # include using namespace std; int main () { cout
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Chapter 2. C++ Program Structure C++ program is a collection of subprograms Subprograms in C++ are called FUNCTIONS Each function performs a specific.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Variables in Java x = 3;. What is a variable?  A variable is a placeholder in memory used by programmers to store information for a certain amount of.
VARIABLES AND DATA TYPES Chapter2:part1 1. Objectives: By the end of this section you should: Understand what the variables are and why they are used.
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.
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
LESSON 2 Basic of C++.
A Sample Program #include using namespace std; int main(void) { cout
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.
Constants, Data Types and Variables
Copyright © Texas Education Agency, Computer Programming Variables and Data Types.
Chapter # 2 Part 2 Programs And data
Chapter Topics The Basics of a C++ Program Data Types
LESSON 2 Basic of C++.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Documentation Need to have documentation in all programs
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Revision Lecture
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Variables with Memory Diagram
CMSC 104, Section 4 Richard Chang
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
Programming Funamental slides
Variables in C Topics Naming Variables Declaring Variables
Programming Funamental slides
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
Fundamental Programming
C Programming Pointers
Primitive Types and Expressions
Unit 3: Variables in Java
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Variables and Constants
Programming Fundamental-1
Data in a C++ Program Numeric data and character data
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Variables Mr. Crone

Variables Variables represent a reserved memory location whose value can change during program execution C++ provides numerous variable types that can be used to hold different types of information Variables can be created to hold numbers in the same way that the letter x represents a number in algebra Variables can also be created to hold characters and words

Variable Data Types Type Declared as Bytes Range Integers int 2 -32768 - 32767 long integers long 4 -2.1e9 - 2.1e9 Floating point float 4 1.2e-38 - 3.4e38 Double double 8 2.2e-308 - 1.8e308 Characters char 1 256 character values

Variable Declaration Variables must be declared as a certain data type so C++ can set aside the appropriate amount of memory. Variables must be declared before or as they are being used. Examples: int sum; char gender; long total; double num2;

Declaring Variables Sample Code: int x; // declares variable x of type Integer double y; // declares variable y of type double ***Declarations can be placed anywhere but usually are grouped together immediately after the { in main.

Rules for Declaring Variables Variables Must: 1) begin with a letter or _ and use letters, digits, and _ 2) be less than 41 characters with no spaces, ., #, $ 3) not be reserve words **C++ is case sensitive, TOTAL, Total, and total are names of different locations.

Variable Capitalization Variable Capitalization is important The first letter is not capitalized, but the first letter in every following word IS capitalized Example: int yearlySalary; double numOfSheep;

Variables Names You should use variable names that are meaningful to you and anyone else that reads your program. Example: int age; double salary; int weight;

Variable Initialization A variable is initialized when it is assigned its first value Example) int x; // variable declaration x = 4; // variable initialization // x now has the value of 4

Assignment Statements “=“ is known as the assignment operator “=“ assigns the value on the right to the variable on the left Example) x = 4; // 4 is placed into x // x gets 4

Assignment Statements Each assignment statement replaces the old value of the variable Example) x = 4; x = 5; // x no longer has value of 4 x = 6; // x no longer has value of 5 cout << x << endl; // Prints: 6

Variable Declaration and Initialization Multiple Declarations are allowed: int num1, num2, num3; Variables can be declared and initialized in one step: double num2 = 1.23;

Variables and Math Functions Variables can be used with math functions assuming that they are of the correct type Example) double x = 25; cout << sqrt(x) << endl; //prints: 5

Mixed – Type Operations Mathematical expressions involving variables of different types may produce results that are not expected Use the chart on the next slide to determine what type of variable will be provided during output

Mixed – Type Operations Results of operations of different variable types: int * int ==>int int * double ==> double int / int ==> int int / double or double / int ==> double int % int ==> int 5%3 = 2 % is not defined for doubles in C++

Memory Addresses We can access variable addresses using the “&” symbol Sample Code: int num = 22; cout << “Value = “<< num << endl; // cout << “Address =“ <<&num << endl; // Value = 22 // Address = <some hexadecimal address in memory>