Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables Mr. Crone.

Similar presentations


Presentation on theme: "Variables Mr. Crone."— Presentation transcript:

1 Variables Mr. Crone

2 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

3 Variable Data Types Type Declared as Bytes Range
Integers int long integers long e e9 Floating point float e e38 Double double e e308 Characters char character values

4 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;

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

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

7 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;

8 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;

9 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

10 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

11 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

12 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;

13 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

14 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

15 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++

16 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>


Download ppt "Variables Mr. Crone."

Similar presentations


Ads by Google