CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables
Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing Variable Using cin Scope of Variables Local Scope Global Scope o Unary Scope Resolution Operator Variable and Memory Constants
Variable 3 Location on computer s memory to store data then use and change its value in a program. Every variable has name, type, size and value: 1. Name (identifier) Series of letters, digits, underscores. Not a keyword ( for example : int, float, double, char, void, return, main). Start with a letter. Case sensitive. Meaningful. 2. Type Programmer-defined. Built-in.
Data Type 4 To store data in a C++ program, we have to tell the compiler which type of data we want to store. The data type will have characteristics such as: 1. The range of values that can be stored. 2. Operations that can be performed on variables of that type.
C++ Built-in Data Types 5 Called fundamental types or primitives types: numeric (integer and floating point), character, logical (Boolean).
bool Data Type 6 Has two values, true and false. Manipulate logical (Boolean) expressions. true and false are called logical values. bool, true, and false are reserved words. Example: bool iseven = false ; bool keyfound = true ;
char Data Type 7 Used for characters letters, digits, and special symbols. Each character is enclosed in single quotes. Examples: 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character and is written ' ' with a space left between the single quotes. Example: char grade ; char star = * ;
int Data Type 8 The integer type is used for storing whole numbers. We can use signed, unsigned or plain integer values as follows: signed int index = 4182; signed int temperature = -32; unsigned int count = 0; int height = 100; int balance = -67;
Floating-Point Types 9 Floating point types can contain decimal numbers. Examples: 1.23, -0.087. There are three sizes: float (single-precision). double (double-precision). long double (extended-precision). Example: float Temp= 37.623; double fahrenheit = 98.415; long double accountbalance = 1897.23;
Declaring Variables 10 All variables must be declared anywhere in program with a name and data type before they used. Syntax rule: begin with a data type then variable name. datatype varname; Variables of the same type can be declared in: Multiple lines One line separated by commas int num1; int num2; int num3; int num1, num2, num3;
Initializing Variable 11 Variables can be initialized when declared. int first=13, second=10; char ch=' '; double x=12.6, y=123.456; first and second are int variables with the values 13 and 10, respectively. ch is a char variable whose value is empty. x and y are double variables with 12.6 and 123.456, respectively.
Using cin 12 Stream extraction operator >> Value to left (left operand) inserted into right operand. Waits for user to input value then press Enter key. Example std::cin >> num1; Inserts the standard input from keyboard into variable num1. Prints message before cin statement to direct the user to take a specification called prompt. cin and cout facilitate interaction between user and program.
Using cin (cont.) 13 Write a program that asks the user to enter 2 numbers (number1) and (number2), then adds the numbers and prints the result.
1 2 // Addition program that display the sum of two numbers. 3 #include <iostream> // allow program to perform input and output 4 5 // function main begins program execution 6 int main() 7 { 8 // variable declaration 9 int number1; // first integer to add 10 int number2; // second integer to add 11 int sum; // sum of number1 and number2 12 13 std::cout << "Enter first integer: \n"; // prompt user for data 14 std::cin >> number1; // read first integer from user to number1 15 16 std::cout << "Enter second integer: \n"; // prompt user for data 17 std::cin >> number2; // read second integer from user to number2 18 19 sum = number1 + number2; // add the numbers; stor result in sum 20 21 std::cout << "Sum is " << sum << std::endl; // display sum; end line 22 23 return 0; // indicate that program ended successfully 24 } // end function main Declare integer variables. Use stream extraction operator with standard input stream to obtain user input. Calculations can be performed in output statements: alternative for lines 19 and 21: std::cout << "Sum is " << number1 + number2 << std::endl; Enter first integer 45 Enter second integer 72 Sum is 117 Stream manipulator std::endl outputs a newline, then flushes output buffer. 14
Variable Scope 15 Portion of the program where the variable can be used. Scope can be: Local Global 1. Local Variables Defined within a module. Can be seen and used only by module itself. Store temporally in memory. Erased when the module terminates. int main() { int i; char a; return 0; }
Variable Scope (cont.) 16 2. Global Variables Defined outside any module. Used and seen by all modules. int i; int main(){ char a; return 0;} NOTES Variable name can be duplicated within and outside a modules. Differentiate between them by using unary scope resolution operator (::). int a; int main(){ int a; return 0;}
Unary Scope Resolution Operator 17 Denoted as ( :: ) Used to declare local and global variables have a same name. To avoid conflicts. Syntax rule ::variable Example: y = ::x + 3 Not needed if names are different.
Unary Scope Resolution Operator (cont.) 18 1 // using the unary scope resolution operator. 2 #include <iostream> 3 using std::cout; 4 using std::endl; 5 int number = 7; // global variable named number 6 int main() 7 { 8 double number = 10.5; // local variable named number 9 // display values of local and global variables 10 cout << "local double value of number = " << number 11 << "\nglobal int value of number = " << ::number << endl; 12 system("pause"); 13 return 0; // indicate successful termination 14 } // end main Local double value of number = 10.5 Global int value of number = 7
Variables and Memory 19 Variables names correspond to location in the computer s memory(ram). Every variable has type, name, size and value, for example: char letter = A ; Type name size value char letter 1byte A Placing new value into variable (memory location), overwrites old value called destructive. Reading value of variable in memory called nondestructive.
Variables and Memory (cont.) 20 std::cin >> number1; Assume user entered 45 number1 45 std::cin >> number2; Assume user entered 72 number1 45 number2 72 sum = number1 + number2; number1 45 number2 72 sum 117
Constants 21 Like variables: data storage locations. Unlike variables: Values never changed during program execution. Any attempt to change a constant creates a compilation error. Declared in two ways and follow identifier naming rules: With const keyword. const char Gender = F ; With #define keyword. #define studentsperclass 15
Exercise - 1 22 1. Write a program that declares two constant A and B. 2. Initialize A =1 and B=2.2 3. Declare an int named C and float named D. 4. Initialize C =1 and D=2.2 5. Write statements to print C and D to screen.