1 / 14

VARIABLES vustudents.ning

VARIABLES http://vustudents.ning.com. 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.

pearly
Download Presentation

VARIABLES vustudents.ning

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VARIABLES http://vustudents.ning.com 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.

  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. ASSIGNMENT STATEMENT 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.

  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 <iostream.h> • 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. CHARACTER VARIABLES • 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).

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

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

  9. INPUT with cin • 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 (<<) and insertion (>>) operators can be used repeatedly in a statement.

  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 longdouble.

  11. Summary of Variable Types type bytes of memory Range char 1 -128 to 127 short 2 -32,768 to 32,767 int 4 -2,147,483,648 to 2,147,483,647 long 4 -2,147,483,648 to 2,147,483,647 float 4 3.4 x E-38 to 3.4 x E+38 double 8 1.7 x E-308 to 1.7x E+308 long double 103.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 aresigned, unsigned, long, andshort. • 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 char 0 to 255 1 • unsigned short 0 to 65,535 2 • unsigned int 0 to 4,294,967,295 4 • unsigned long 0 to 4,294,967,295 4

  14. ESCAPE SEQUENCE or BACKSLASH CHARACTER CONSTANT In C++, backslash (\) causes an “escape” from the normal way, the characters are interpreted. Symbol Meaning Symbol Meaning \a beep \” Double quotes \r return \’ Single quote \n newline \? ? \t tab \\ \ http://vustudents.ning.com

More Related