Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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

3 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

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

5 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

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

7 #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…

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

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

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


Download ppt "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."

Similar presentations


Ads by Google