Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 5. Outline Some Exercises in C++ Names Variables Scope Lifetime.

Similar presentations


Presentation on theme: "Chapter 5. Outline Some Exercises in C++ Names Variables Scope Lifetime."— Presentation transcript:

1 Chapter 5

2 Outline Some Exercises in C++ Names Variables Scope Lifetime

3 Some Exercises in C++ اكتب برنامج يستطيع حساب مساحة المستطيل حيث أن الطول و العرض يقوم المستخدم بادخاله، علما بان مساحة المستطيل = الطول * العرض. اكتب برنامج يقوم بحساب معدل الصرف الاسبوعي لشخص ما؟

4 Names – Names are associated with variable,lables,sub- programs, formal parameters and other program constructs such as class. – Design issues= Are the names case-sensitive ?

5 Names Ok ? what is a name in a programming language ? – It’s a string of characters used to identify some entities in a program.

6 Names Fortran : allows space in names sum of salaries sum of salaries. C++ : case –sensitive Rose ≠ ROSE ≠ rose This will take us back to How can we evaluate any language by writbility and readability.

7 7 اﻟﻜﻠﻤﺎت اﻟﻤﺤﺠﻮزة (Reserved words ) unsignedstructshortintfloatdoubleconstauto voidswitchsignedlongforelsecontinuebreak volatiletypedefsizeofregistergotoenumdefaultcase whileunionstaticreturnifexterndochar Names

8 How to name in C++: 1. names must begin with a letter of the alphabet or an underscore( _ ) 2.After the first initial letter, variable names can also contain letters and numbers. No spaces or special characters, however, are allowed. 3.Uppercase characters are distinct from lowercase characters. Using all uppercase letters is used primarily to identify constant variables. 4.You cannot use a C++ keyword (reserved word) as a variable name

9 Variable It is a storage location and an associated symbolic name (an identifier) which contains some known or unknown quantity or information, a value Attributes 1.Names: same conditions previously. 2.Type = int, char, float, double, ….. 3.Address : the place in the memory. 4.Value : content of the memory.

10 Scope Every variable has a scope : So the scope of a variable is the range of statements in which the variable is visible. The scope of a variable is from its declaration to the end of the method.

11 Lifetime The lifetime of a variable is the period of time beginning when the method is entered and ending when execution of the method terminates.

12 #include using namespace std; int main() { int x = 10 ; int grade; Sum(); cout << “Grade” << grade ; for (int i =0; i<5 ; i++) { cout << “ Test “ ; } } void Sum() { int x = 100 ; cout <<“ X = “ <<x ; }


Download ppt "Chapter 5. Outline Some Exercises in C++ Names Variables Scope Lifetime."

Similar presentations


Ads by Google