21
Introduction to Programming Introduction to Programming Lesson 3 Lesson 3

CS201- Introduction to Programming- Lecture 03

Embed Size (px)

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 03

Introduction to ProgrammingIntroduction to Programming

Lesson 3Lesson 3

Page 2: CS201- Introduction to Programming- Lecture 03

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

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

}}

Page 3: CS201- Introduction to Programming- Lecture 03

VariableVariable

VariableVariable X

Page 4: CS201- Introduction to Programming- Lecture 03

VariableVariable Pic of the memoryPic of the memory

2525

1032310323

namenameof the of the variablevariable

Page 5: CS201- Introduction to Programming- Lecture 03

VariableVariableVariable starts withVariable starts with

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

Page 6: CS201- Introduction to Programming- Lecture 03

VariableVariable Small post boxSmall post box

X

Page 7: CS201- Introduction to Programming- Lecture 03

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;

Page 8: CS201- Introduction to Programming- Lecture 03

VariableVariableIn a program a variable In a program a variable

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

Page 9: CS201- Introduction to Programming- Lecture 03

Assignment Assignment OperatorOperator

==x = 2x = 2

X 2

Page 10: CS201- Introduction to Programming- Lecture 03

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

Page 11: CS201- Introduction to Programming- Lecture 03

X = 10 ; X = 10 ;

X = 30 ;X = 30 ;

X 10

X 30

Page 12: CS201- Introduction to Programming- Lecture 03

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

X 10 + 1

=

X

11

Page 13: CS201- Introduction to Programming- Lecture 03

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

Declaration l ineDeclaration l ine

ii

Page 14: CS201- Introduction to Programming- Lecture 03

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

}}

Page 15: CS201- Introduction to Programming- Lecture 03

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

Page 16: CS201- Introduction to Programming- Lecture 03

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

Page 17: CS201- Introduction to Programming- Lecture 03

Arithmetic operatorsArithmetic operatorsPlusPlus ++

MinusMinus --

MultiplyMultiply **

DivideDivide / /

ModulusModulus %%

Page 18: CS201- Introduction to Programming- Lecture 03

Arithmetic operatorsArithmetic operators

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

Page 19: CS201- Introduction to Programming- Lecture 03

% = Remainder% = Remainder

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

Page 20: CS201- Introduction to Programming- Lecture 03

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

Page 21: CS201- Introduction to Programming- Lecture 03

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