Download presentation
Presentation is loading. Please wait.
Published byAvice Flowers Modified over 9 years ago
1
LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES, INPUT, MEMORY المتغيرات, المدخلات, الذاكرة By Mr. Ali Edan
2
Content 2 Variables المتغيرات C++ Built in data types انواع البيانات والمتغيرات C++ variable declaration syntax طريقة تعريف المتغيرات cin statement امر ادخال البيانات Local and global variables المتغير المحلي والمتغير العمومي Scope resolution operator (::) الاداة Variables and RAM المتغيرات والذاكرة Defining constants الثوابت
3
Variable Location on computer’s memory to store data then use and change its value in a program جزء من الذاكرة لخزن نوع محدد من البيانات Each variable has كل متغير لدية 1. Name (identifier) اسم Series of letters, digits, underscores Not a keyword Start with a letter Case sensitive Meaningful 2. Type نوع Programmer-defined احد انواع البيانات 3
4
Determine which of the following variables name is valid or invalid : _under_bar_z2h22 2h67h2his_account_total t587m928134 her_sales3gj7 abc 4
5
C++ Built-in Data Types Called fundamental types or primitives types: numeric, character, logical الانواع الاساسية : ارقام, حرفي, منطقية 5
6
الانواع المنطقية bool Data Type Has two values, true and false قيمتين صح و خطأ Manipulate logical (Boolean) expressions تستخدم في المعادلات المنطقية true and false are called logical values قيم هذا المتغيرات هي true & false bool, true, and false are reserved words الكلمات المحجوزة هي true,false, bool 6
7
نوع char Data Type 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 7
8
Declaring Variables 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 النوع يكتب قبل اسم المتغير Variables of the same type can be declared in Multiple lines One line separated by commas 8 int num1; int num2; int num3; int num1, num2, num3; int num1; int num2; int num3; int num1, num2, num3; dataType varName;
9
اعطاء قيم للمتغيرات Initializing Variable Variables can be initialized once declared 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 9 int first=13, second=10; char ch=' '; double x=12.6, y=123.456; int first=13, second=10; char ch=' '; double x=12.6, y=123.456;
10
Using cin 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 10
11
Using cin (cont.) 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 11
12
12 Enter first integer 45 Enter second integer 72 Sum is 117 1 // Fig. 2.5: fig02_05.cpp 2 // Addition program that display the sum of two numbers. 3 #include // allow program to perform input and output 4 5 // function main begins program execution 6 int main() 7 { 8 // variable declaration 9int 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. Stream manipulator std::endl outputs a newline, then “flushes output buffer.” Concatenating, chaining or cascading stream insertion operations. Calculations can be performed in output statements: alternative for lines 19 and 21: std::cout << "Sum is " << number1 + number2 << std::endl;
13
Variable Scope Portion of the program where the variable can be used Scope can be Local Global 13
14
Local Variables Defined within a module Can be seen and used only by module itself Store temporally in memory Erased when the module terminates 14 int main() { int i; char a; return 0; } int main() { int i; char a; return 0; }
15
Global Variables 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 (::) 15 int i; int main() { char a; return 0; } int i; int main() { char a; return 0; }
16
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 17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.