A Variable is symbolic name that can be given different values. Variables are stored in particular...

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

Transcript of A Variable is symbolic name that can be given different values. Variables are stored in particular...

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.

VARIABLESVARIABLEShttp://vustudents.ning.com

Rules for Naming VariablesRules 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.

The statementsVal1 = 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 STATEMENTASSIGNMENT STATEMENT

INTEGER VARIABLESINTEGER 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.

//intvars.cpp//demonstrates integer variables#include <iostream.h>

void main ( ){int var1, var2; // defines two variablesvar1 = 20; // assign value var2 = var1 + 10;cout << “var1 + 10 is “; // output textcout << var2; // output value}

• 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 VARIABLESCHARACTER VARIABLES

// characters.cpp# include <iostream.h>

void main ( ){char var1 = ‘A’, var2 = ‘B’, var3 = ‘ ’;cout << var1 <<var3 << var2;}

# 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 ;}

INPUT with cinINPUT 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.

INPUT with cinINPUT with cin

Floating Point VariablesFloating 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.

Summary of Variable TypesSummary of Variable Types

type bytes of memory Range

char 1 -128 to 127short 2 -32,768 to 32,767int 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 10 3.4x E-4932 to 3.4x E-4932

ModifiersModifiers• 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.

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

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 \\ \

ESCAPE SEQUENCE or ESCAPE SEQUENCE or BACKSLASH CHARACTER CONSTANTBACKSLASH CHARACTER CONSTANT

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

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

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

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

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

Constant

Numeric Constant Non-Numeric Constant

Integer constant Floating point constant

Octal constant

Hexadecimal constant

Char constant String constant

Numeric Constant -contd

Decimal (base10)

Octal (base8) Hexadecimal (base 16)

4 04 0x04

8 010 0x08

16 020 0x10

29 035 0x1d

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

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.

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

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

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

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

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

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.

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++;}

http://vustudents.ning.com