Presentation is loading. Please wait.

Presentation is loading. Please wait.

A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.

Similar presentations

Presentation on theme: "A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is."— Presentation transcript:

1 A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is given a value, that value is placed in the memory space occupied by the variable. VARIABLES

2 Rules for Naming Variables The first character must be a letter or underscore_. Names can be as long as you like, but first 32 characters will be recognized. Both Upper and lower case letters can be used, but compiler distinguishes between them. Digits can be used. Spaces cannot be used as a part of variable name. Variables must be defined before using them. Variables can be defined throughout a program, not just at the beginning.

3 The statements Val1 = 10; Val2 = Val1 + 5; Assigns values to the two variables. The equal sign =, causes the value on R.H.S to be assigned to the variable on the L.H.S. In the first line, Val1 is given the value 10. In the second line, the plus sign +, adds the value of Val1 and 5. The result of this addition is then assigned to Val2. ASSIGNMENT STATEMENT

4 INTEGER VARIABLES Integer variables represent integer numbers like 1, 12, 14000 etc, i.e. numbers having no fractional part. In C++, the most commonly used is type int. It requires four bytes of memory space. It holds numbers in the range –2,147,483,648 to 2,147,483,647.

5 //intvars.cpp //demonstrates integer variables #include void main ( ) { int var1, var2;// defines two variables var1 = 20;// assign value var2 = var1 + 10; cout << “var1 + 10 is “; // output text cout << var2;// output value }

6 Another integer variable type is char. This type stores integer in the range of –128 to 127. It occupies one byte of memory. Character variables are mostly used to store ASCII ( American Standard Code for Information Interchange) characters. Character Constants use single quotation marks around a character (unlike double quotes around a string). CHARACTER VARIABLES

7 // characters.cpp # include void main ( ) { char var1 = ‘A’, var2 = ‘B’, var3 = ‘ ’; cout << var1 <<var3 << var2; }

8 # include void main ( ) { int ftemp; cout << “Enter temperature in fahrenheit : “; cin >> ftemp; int ctemp = (ftemp-32) * 5 / 9; cout << “Equivalent in Celsius is : “ << ctemp ; } INPUT with cin

9 The cin statement causes the program to wait for the user to type in a number. The keyword cin is an object in C++ to correspond to the standard input stream. This stream represents data coming from the keyboard. The >> is the extraction or get from operator. It takes the value from the stream object (i.e. cin object) on L.H.S and places it in the variable on its R.H.S. Note: The extraction ( >) operators can be used repeatedly in a statement. INPUT with cin

10 Floating Point Variables A floating point variable is used when a fraction component is required. There are three kinds of floating point variables in C++. type float, type double and type long double.

11 Summary of Variable Types typebytes of memory Range char 1-128 to 127 short2-32,768 to 32,767 int4 - 2,147,483,648 to 2,147,483,647 long4- 2,147,483,648 to 2,147,483,647 float43.4 x E-38 to 3.4 x E+38 double81.7 x E-308 to 1.7x E+308 long double10 3.4x E-4932 to 3.4x E-4932

12 Modifiers A modifier is used to alter the meanings of a base type so that it fits more precisely as required. C++ type modifiers are signed, unsigned, long, and short. The unsigned types are used when the quantities represented are always positive. This allows them to represent numbers twice as big as the signed type, i.e. to start at 0 and include only positive numbers.

13 Unsigned Integer Type unsigned char0 to 2551 unsigned short0 to 65,5352 unsigned int0 to 4,294,967,295 4 unsigned long0 to 4,294,967,295 4

14 In C++, backslash (\) causes an “escape” from the normal way, the characters are interpreted. SymbolMeaningSymbolMeaning \abeep\”Double quotes \rreturn\’Single quote \nnewline\?? \ttab \\\ ESCAPE SEQUENCE or BACKSLASH CHARACTER CONSTANT


16 C++ Character Set Consist of Numeric characters, Alphabetic characters, Special characters, Escape characters and White space characters Numeric Characters Set are (0, 1, 2…..and 9) Alphabetic Character Set are upper and lower case letters (A, B….Z, a, b,….z) Special Character Set are period (.), Comma(,), Semicolon(;), colon(:), plus minus (+, -), Asterisk(*), Slash(/), Backslash(\), Equal Sign(=) and so on

17 C++ Character Set -contd Escape Character Set Used for formatting purpose, backslash is used in the statement followed by the character, to insert a new line in the output you can write \n in your program code. Escape character change the meaning of the character follow it, any character interpreted with \ is known as escape sequence or escape character White Space Character Any character that produces blank space when printed is white space character, they are space, tabs new lines and comments, they are used to make your program more readable Z=x+y can be z = x + y

18 C++ Reserved Words Reserved for specific purpose and can not used for other purpose. Have special meaning compiler know their meaning, all are in lower case. C++ is case sensitive Can’t be used as user defined names examples are break, case, char, const, continue, default, else Variables A value which may vary during the program execution. Each variable has specific storage location in the memory where the data is stored

19 Constants Unlike variable a constant is a fixed value that does not change during the execution of a program, constant are values and variables are holders of these values, constant may be divided into two main types: Numeric Constant Non-Numeric Constant Numeric Constant : Numbers are referred as numeric constant may contains Numbers digits 0 to 9 Plus(+) or minus(-)sign(if no sign taken as positive) Decimal point

20 Constant contd No commas or blanks are allowed in numeric constants 25 0.0 +100 -300 3.14 Numeric constants are represents in three ways –Integer constant –Floating point constant –Exponential real contant

21 Constant Numeric Constant Integer constant Octal constant Hexadecimal constant Floating point constant Non-Numeric Constant Char constant String constant

22 Numeric Constant -contd Decimal (base10) Octal (base8)Hexadecimal (base 16) 4040x04 80100x08 160200x10 290350x1d

23 Integer Constant Like 25 1993 +125 0 suffix L or U or ul can be used to force constant as unsigned long Invalid Octal and Hex constant –567 must start with 0, should not have 8, (.) is not allowed –0823 –0745.56

24 Illegal (.), start with 0x, illegal character G 0x12.34 0FABC 0XEFG Floating point constant contains decimal part 0.3, -860.7, +98.33 are double by definition to ensure maximum efficiency.

25 Exponential Real Constant Floating point constant can also be represented in E-notation form known as exponential real constant used for scientific engineering application to represent large numbers as well as small numbers –“m” is mantissa, “E” is base 10, “n” is exponent of base 10 –Where “m” can be any int or real value and “n” must be integer 123 = 1.23E+2 ».0333= 3.33E-2

26 Non-Numeric Constant Character Constant enclosed in single quote, single quote are not part of character constant, serve to delimit it –‘z’ ‘3’ ‘*’ ‘.’ All alphabets, numeric and special characters on keyboard are character constant, except backslash \ and single quote marks character ‘\\’ for back slash, ‘\’’ for single quote

27 String Constant any sequence of character between double quotes are string constant or simple string.Special characters, blanks spaces and reserved words may also be used in double quotation marks. Double quotation are not part of string constant but delimit the string e.g., “12345”, “Hello”, “ “, “________”, except the back slash and double quotes all can be string constant.

28 Operators An op is a symbol causes the compiler to take an action, op works on data Arithmetic operators -, +, *, /, and % modulus works on int data, arithmetic assignment op are +=, *=, /=, %= Assignment Operator basic assignment operator = Increment Operators ++, -- to increment or decrement the value of simple variable by adding or subtracting 1

29 Expression Any combination of operators, constant, variables, all expression are statements The expression may consist of a single entity, such as constant, variable e.g., 7 int constant, z variable, 3.2 float point constant An exp may be combination of constant, variable, op x=5+6, x=y, ++z May also represent a logical condition that evaluate to true or false x = =y, x<=y

30 Statements It is a gp of instruction to carry out certain action, end with semicolon ;, types are –Simple statement –Structure statement Exp to be evaluated fol by ; like x=5; z = x+y; cout<<“Hello”; are the example of simple statements.

31 Structure statement Compound Statement: written in pair of braces { pi=3.14; area=pi*radius* radius;}, several indl statement, which may consist of exp statement. Selection Statement: chose among alternates if(marks>33) cout<<“Pass”; else cout<<“Fail”; Iteration Statement: or looping statement are used to perform action repeatedly while some conditions are true count=1; while(count<=5){cout<<“Pakistan”;count++;}

Download ppt "A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is."

Similar presentations

Ads by Google