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>