Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)

Similar presentations


Presentation on theme: "Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)"— Presentation transcript:

1 Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1

2 VARIABLES A quantity whose value may change during program execution is called variable. It is represented by a symbol or a name. A variable represents a storage or memory location in the computer memory. Data is stored into the memory location. The name of the memory location i.e. the variable name remains fixed during execution of the program but the data stored in that location may change from time to time. 2

3 Conti…. A variable is also known as object in c++. In c++ a variable name consists of alphabets and digits. 3

4 Rules for writing variable name The following are the rules for writing a variable name in a program in c++. The first character of variable name must be an alphabetic character. underscore can be used as first character of variable name. blank space are not allowed in a variable name. 4

5 Conti…. Special characters such as arithmetic operators,#,^, cannot be used in a variable name. Reserved words cannot be used as variable names. The maximum length of a variable name depends upon the compiler of c++. A variable name declared for one data type cannot be used to declare another data type. 5

6 Conti….. C++ is a case sensitive language. Thus variable names with same spelling but different case are treated as different variable name. For example variable ‘PAY’ and ‘pay’ are two different variable. Following are some example of the valid and invalid variables names. 6

7 Variable nameValid/invalidremarks NadeemValid PerformValid doubleInvalidC++ reserved word Foxprovalid switchInvalidC++ reserved word MarriamValid IntInvalidC++ reserved word 3taqInvalidStarts with a number UnsignedinvalidC++ reserved word Taq ahdInvalidSpace is not allowed X-yInvalidSpecial character is not allowed 7

8 Declaration of variables Assigning the name and data type that a variable can hold is called declaration of the variable. All variables that are used in a program are declared using variable declaration statement. The syntax to declare a variable in c ++ is : type list of variable 8

9 Conti…. Type,specify data type of variable. For example, it may be int, float,etc. list of variables specify a list of variables separated by comma. In a single statement more than one variables, separated by commas, of same data type can be declared. For example, integer type. int a,b,c,d; float abc; char name; 9

10 Initialization of variable When a variable is declared, a memory location is assigned to it. The value in that memory location is also assigned to the variable. The value is assigned to the variable at the time of its declaration is called initializing of the variable. For example, to declare variable a,b,and c of integer type and assigning value a=10,b=20,c=30, the statement is written as: int a=10,b=20,c=30; 10

11 Write a program to assign values to the different variables at the time of declaration. print that values. # include main() { int a =4,b=5; float c=3.5; char name[15]=“marriam ahmed”; cout<<name<<endl; cout<<a<<b<<c; getch(); } 11

12 Data types in c ++ The variable types specify the type of data that can be stored in it. Each variable is declared by its type. C++ has five basic data types. These data types are: 1.intinteger 2.floatfloating point 3.doubledouble precision 4.char characters 5.boolboolean 12

13 TypeSize in bytes int2 bytes short int2 bytes long int4 bytes unsigned int2 bytes unsigned long int4 bytes float4 bytes long float8 bytes double8 bytes long double10 bytes char1 byte 13

14 CONSTANTS A quantity that cannot change its values during execution of program is called constants. There are four types of constants in c++. these are: 1.integer constants 2.floating constants 3.character constants 4.string constants 14

15 Integer constants A numerical value without a decimal part is called integer constant. The plus(+) and minus (-) sign can also be used with an integer constant. Integer constants are used in expression for calculations. To print an integer constant,it is given in the output statement without quotation marks. For example,to print integer constant 520 the output statement is cout<<520; 15

16 Floating –point constants Numeric values that have an integer as well as a decimal part are called floating –point values. For example 12.345. CHARACTER CONSTANTS A single character enclosed in a single quotation marks is called character constants. For example ‘a’,’\’,’+’, represent character constants. 16

17 String constants A sequence of character consisting of alphabets, digits or special character enclosed in double quotation marks is called string constants For example “mihe Kabul", "Afghanistan” 17

18 The constant qualifier The data item that follows the keyword “const” cannot change its value during execution of the program. A value is assigned to a data item at the time of its declaration. If numeric type value is assigned then the data item can be used in expression for calculation. 18

19 PROGRAM # include main () { int r; const float p =3.14 ; float peri; r = 2; peri = 2*p*r; cout <<“result is =“<<peri; } 19

20 The “define” directive it is a preprocessor directives. It is used to define a constant quantity. It is used at the beginning of the program. # define identifier constant identifier : it specifies the character to which the constant value is to be assigned constant : it specifies the constant values that is to be assigned to the identifier. 20

21 The value assigned to the identifier remains the same throughout the execution of program. A variable of the name of the identifier cannot be declared in the program. Also, the identifier does not have any data type but any data can be assigned to it. 21

22 Program: ‘p’ has been assigned a constant value of 3.14 in the beginning of the program using the define directive. # include # define p 3.14 main () { int r; float peri; r=2; peri= 2*p*r; cout<<“result is =“<<peri; getch(); } 22

23 Arithmetic expression. An arithmetic expression is a combination of variables, constants and arithmetic operator. It is used to calculate the value of an arithmetic formula. It returns a single value after evaluating the expression. the value of the expression is assigned to a variable on the left side of “=“ operator. This variable is known as the receiving variable. 23

24 Conti……. The ‘=‘ operator is called assignment operator. It is used to assign the calculated value of the expression to the receiving variable. The receiving variable must be of numeric type. For example if m= 10,x=5 the expression may be written as m * x + 100. It is an arithmetic expression. This expression return a single value. This value is assigned to a single variable using the assignment operator. Thus, if “res” is the receiving variable the assignment statement is written as res=m * x + 100; 24

25 Relational Expressions A relational expression consists of constants, variables or arithmetic expression that are combined by a relational operator. A relational expression is written to find a relation between two expressions. It returns only one value, which is either true or false. For example 10>6 is a relational expression. It indicates a relation between two constants. Since the constants value is greater than 6 this relational expression returns a true value. 25

26 The operator ‘>’ used between the two values is called the relational operator. It specifies relation between two arithmetic expression or values. 26


Download ppt "Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)"

Similar presentations


Ads by Google