CS201- Introduction to Programming- Lecture 03

Preview:

Citation preview

Introduction to ProgrammingIntroduction to Programming

Lesson 3Lesson 3

#include <iostream.h>#include <iostream.h>main ( )main ( ){{

cout << “ Welcome to Virtual University cout << “ Welcome to Virtual University “;“;

}}

VariableVariable

VariableVariable X

VariableVariable Pic of the memoryPic of the memory

2525

1032310323

namenameof the of the variablevariable

VariableVariableVariable starts withVariable starts with

1.1. CharacterCharacter2.2. Underscore _ Underscore _ (Not Recommended)(Not Recommended)

VariableVariable Small post boxSmall post box

X

VariableVariableVariable is the name of a location Variable is the name of a location

ininthe memorythe memory

e.g. x= 2;e.g. x= 2;

VariableVariableIn a program a variable In a program a variable

has:has:1.1. NameName2.2. TypeType3.3. SizeSize4.4. ValueValue

Assignment Assignment OperatorOperator

==x = 2x = 2

X 2

Assignment Assignment Operator Operator

L.H.S = R.H.S.L.H.S = R.H.S.

X+ 3 = y + 4 X+ 3 = y + 4 WrongWrongZ = x +4Z = x +4

x +4 = Z x +4 = Z WrongWrong

X = 10 ; X = 10 ;

X = 30 ;X = 30 ;

X 10

X 30

X = X + 1;X = X + 1;

X 10 + 1

=

X

11

Data typeData type int i ; -> int i ; ->

Declaration l ineDeclaration l ine

ii

#include <iostream.h>#include <iostream.h>main ( )main ( ){{

int x ;int x ;int y ;int y ;int z ;int z ;x = 10 ;x = 10 ;y = 20 ;y = 20 ;z = x + y ;z = x + y ;

cout << " x = " ;cout << " x = " ;cout << x ;cout << x ;

cout << " y = " ;cout << " y = " ;cout << y ;cout << y ;

cout << " z =x + y = " ;cout << " z =x + y = " ;cout << z ;cout << z ;

}}

int x, y, z ;int x, y, z ;int x; int y; int z ;int x; int y; int z ;

Data TypesData Types1.1. intint2.2. shortshort3.3. longlong4.4. f loatf loat5.5. doubledouble6.6. charchar

Arithmetic operatorsArithmetic operatorsPlusPlus ++

MinusMinus --

MultiplyMultiply **

DivideDivide / /

ModulusModulus %%

Arithmetic operatorsArithmetic operators

i + ji + jx * yx * ya / ba / ba % ba % b

% = Remainder% = Remainder

5 % 2 = 15 % 2 = 12 % 2 = 02 % 2 = 0

4 / 2 = 24 / 2 = 25 / 2 = ?5 / 2 = ?

PrecedencePrecedence Highest:Highest: ( )( ) Next:Next: * , / , %* , / , % Lowest:Lowest: + , -+ , -

Recommended