LECTEURE 5 STRUCTURED PROGRAMMING VARIABLES INPUT MEMORY By
LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES , INPUT , MEMORY ﺍﻟﺬﺍﻛﺮﺓ , ﺍﻟﻤﺪﺧﻼﺕ , ﺍﻟﻤﺘﻐﻴﺮﺍﺕ By Mr. Ali Edan
Variable 3 Location on computer’s memory to store data then use and change its value in a program ﺟﺰﺀ ﻣﻦ ﺍﻟﺬﺍﻛﺮﺓ ﻟﺨﺰﻥ ﻧﻮﻉ ﻣﺤﺪﺩ ﻣﻦ ﺍﻟﺒﻴﺎﻧﺎﺕ Each variable has ﻛﻞ ﻣﺘﻐﻴﺮ ﻟﺪﻳﺔ 1. 2. Name (identifier) ﺍﺳﻢ Series of letters, digits, underscores Not a keyword Start with a letter Case sensitive Meaningful Type ﻧﻮﻉ Programmer-defined ﺍﺣﺪ ﺍﻧﻮﺍﻉ ﺍﻟﺒﻴﺎﻧﺎﺕ
Determine which of the following variables name is valid or invalid : 4 _under_bar _ z 2 h 22 2 h 67 h 2 his_account_tot al t 5 87 m 928134 her_sales 3 g j 7 a b c
C++ Built-in Data Types 5 Called fundamental types or primitives types: numeric, character, logical ﻣﻨﻄﻘﻴﺔ , ﺣﺮﻓﻲ , ﺍﺭﻗﺎﻡ : ﺍﻻﻧﻮﺍﻉ ﺍﻻﺳﺎﺳﻴﺔ
6 bool Data Type ﺍﻻﻧﻮﺍﻉ ﺍﻟﻤﻨﻄﻘﻴﺔ Has two values, true and false ﻗﻴﻤﺘﻴﻦ ﺻﺢ ﻭ ﺧﻄﺄ ﺗﺴﺘﺨﺪﻡ ﻓﻲ ﺍﻟﻤﻌﺎﺩﻻﺕ ﺍﻟﻤﻨﻄﻘﻴﺔ true & false ﻗﻴﻢ ﻫﺬﺍ ﺍﻟﻤﺘﻐﻴﺮﺍﺕ ﻫﻲ Manipulate logical (Boolean) expressions true and false are called logical values bool, true, and false are reserved words true , false, bool ﺍﻟﻜﻠﻤﺎﺕ ﺍﻟﻤﺤﺠﻮﺯﺓ ﻫﻲ
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
Declaring Variables 8 All variables must be declared anywhere in program with a name and data type before they used ﻛﻞ ﺍﻧﻮﺍﻉ ﺍﻟﻤﺘﻐﻴﺮﺍﺕ ﻳﺤﺐ ﺍﻥ ﺗﻌﺮﻑ ﻗﺒﻞ ﺍﻥ ﺗﺴﺘﺨﺪﻡ data. Type Syntax rule: begin with a data type then variablevar. Name; name ﺍﻟﻨﻮﻉ ﻳﻜﺘﺐ ﻗﺒﻞ ﺍﺳﻢ ﺍﻟﻤﺘﻐﻴﺮ Variables of the same type can be declared int num 1; in Multiple lines One line separated by commas int num 2; int num 3; int num 1, num 2, num 3;
Initializing Variable ﺍﻋﻄﺎﺀ ﻗﻴﻢ ﻟﻠﻤﺘﻐﻴﺮﺍﺕ 9 Variables can be initialized once 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 10 Namespace std: : Specifies using a name that belongs to “namespace” std Can be removed through use of using statements Standard output stream object std: : cin “Connected” to keyboard Defined in input/output stream header file <iostream>
Using cin (cont. ) 11 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 >> num 1; Inserts the standard input from keyboard into variable num 1 Prints message before cin statement to direct the user to take a specification called prompt cin and cout facilitate interaction between user and program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 // Fig. 2. 5: fig 02_05. cpp // Addition program that display the sum of two numbers. #include <iostream> // allow program to perform input and output // function main begins program execution int main() { Declare integer variables. // variable declaration int number 1; // first integer to add int number 2; // second integer to add Use and stream extraction operator int sum; // sum of number 1 number 2 std: : cout << "Enter std: : cin >> number 1; with standard input stream to usern" input. first obtain integer: ; // prompt user for data // read first integer from user to number 1 Calculations can be performed in output statements: alternative for std: : cout << "Enter second integer: n" ; // prompt user for data lines 19 and 21: std: : cin >> number 2; // read second integer from user to number 2 Stream manipulator std: : cout << "Sum is " << number 1 + number 2 outputs << std: : endl; std: : endl a sum = number 1 + number 2; // add the numbers; stor result in sum newline, then “flushes output std: : cout << "Sum is " << sum << std: : endl; // display buffer. ” sum; end line return 0; // indicate that program ended successfully } // end function main Enter first integer 45 Enter second integer 72 Concatenating, chaining or cascading stream insertion operations. Sum is 117 12
Variable Scope 13 Portion of the program where the variable can be used Scope can be Local Global
Local Variables 14 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; }
Global Variables 15 Defined outside any module Used and seen by all modules Variable name can be duplicated within and outside a modules Differentiate between them by using unary scope resolution operator (: : ) int i; int main() { char a; return 0; }
Exercise - 1 16 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 =A and D=B 5. Write statements to print C and D to screen
17 End
- Slides: 17