cs201- introduction to programming- lecture 05

28
Introduction to Programming Introduction to Programming Lecture 5 Lecture 5

Upload: bilal-ahmed

Post on 10-Jul-2015

52 views

Category:

Education


2 download

TRANSCRIPT

Page 1: CS201- Introduction to Programming- Lecture 05

Introduction to ProgrammingIntroduction to Programming

Lecture 5Lecture 5

Page 2: CS201- Introduction to Programming- Lecture 05

In the Previous In the Previous LectureLecture

Basic structure of C programBasic structure of C program Variables and Data typesVariables and Data types OperatorsOperators ‘ ‘cout’ and ‘cin’ for output and inputcout’ and ‘cin’ for output and input BracesBraces

Page 3: CS201- Introduction to Programming- Lecture 05

DecisionDecision

Page 4: CS201- Introduction to Programming- Lecture 05

If StatementIf StatementI f condition is trueIf condition is true

statementsstatements

If Ali ’s height is greater then 6 If Ali ’s height is greater then 6 feetfeet

Then Then Ali can become a member of the Ali can become a member of the Basket Ball teamBasket Ball team

Page 5: CS201- Introduction to Programming- Lecture 05

If Statement in If Statement in CC

If (condit ion)If (condit ion)statement ;statement ;

Page 6: CS201- Introduction to Programming- Lecture 05

If Statement in If Statement in CC

I f ( condit ion )If ( condit ion ){{

statement1 ;statement1 ;statement2 ;statement2 ;

::} }

Page 7: CS201- Introduction to Programming- Lecture 05

I f statement in CIf statement in C

i f (age1 > age2)if (age1 > age2)cout<<“Student 1 is older cout<<“Student 1 is older than student 2” ;than student 2” ;

Page 8: CS201- Introduction to Programming- Lecture 05

Relational Relational OperatorsOperators

< < less thanless than<= <= less than or equal toless than or equal to== equal to== equal to>= greater than or equal to>= greater than or equal to> greater than> greater than!= not equal to!= not equal to

Page 9: CS201- Introduction to Programming- Lecture 05

Relational Relational OperatorsOperators

a != b;a != b;

X = 0;X = 0;X == 0;X == 0;

Page 10: CS201- Introduction to Programming- Lecture 05

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

int AmirAge, AmaraAge;int AmirAge, AmaraAge;AmirAge = 0;AmirAge = 0;AmaraAge = 0;AmaraAge = 0;

cout<<“Please enter Amir’s age”;cout<<“Please enter Amir’s age”;cin >> AmirAge;cin >> AmirAge;cout<<“Please enter Amara’s age”;cout<<“Please enter Amara’s age”;cin >> AmaraAge;cin >> AmaraAge;

if AmirAge > AmaraAge) if AmirAge > AmaraAge) {{

cout << “\n”<< “Amir’s age is greater then Amara’s age” ;cout << “\n”<< “Amir’s age is greater then Amara’s age” ;} }

}}

Page 11: CS201- Introduction to Programming- Lecture 05

Flow Chart SymbolsFlow Chart SymbolsStart or stop

Process

Continuation mark

Decision

Flow line

Page 12: CS201- Introduction to Programming- Lecture 05

Flow Chart for i f statementFlow Chart for i f statement

Condition

Process

IF

Then

Entry point for IF block

Exit point for IF block

Note indentation from left to right

Page 13: CS201- Introduction to Programming- Lecture 05

ExampleExampleIf the student age is greater than 18 If the student age is greater than 18

or his height is greater than five or his height is greater than five feet then put him on the foot balll feet then put him on the foot balll teamteam

ElseElsePut him on the chess teamPut him on the chess team

Page 14: CS201- Introduction to Programming- Lecture 05

Logical OperatorsLogical Operators

ANDAND &&&&OROR ||||

Page 15: CS201- Introduction to Programming- Lecture 05

Logical OperatorsLogical OperatorsIf a is greater than b If a is greater than b

AND c is greater than dAND c is greater than d

In Cif(a > b && c> d)if(age > 18 || height > 5)

Page 16: CS201- Introduction to Programming- Lecture 05

i f-elseif-elseif (condition)if (condition){{

statement ;statement ;----

}}elseelse{{

statement ;statement ;----

}}

Page 17: CS201- Introduction to Programming- Lecture 05

i f-elseif-elseCondition

Process 1

IF

Then

Entry point for IF-Else block

Exit point for IF block

Process 2

Else

Note indentation from left to right

Page 18: CS201- Introduction to Programming- Lecture 05

Code

if (AmirAge > AmaraAge) { cout<< “Amir is older than Amara” ;}

if (AmirAge < AmaraAge) { cout<< “Amir is younger than Amara” ;}

Example Example

Page 19: CS201- Introduction to Programming- Lecture 05

ExampleExampleCodeCode

if AmirAge > AmaraAge) if AmirAge > AmaraAge) {{ cout<< “Amir is older than Amara” ;cout<< “Amir is older than Amara” ;} } else else {{ cout<<“Amir is younger than Amara” ;cout<<“Amir is younger than Amara” ;}}

Page 20: CS201- Introduction to Programming- Lecture 05

Make a small f low chart of thisMake a small f low chart of thisprogram and see the one to program and see the one to

oneonecorrespondence of the chart correspondence of the chart and the codeand the code

Page 21: CS201- Introduction to Programming- Lecture 05

Example Example CodeCode

if AmirAge > AmaraAge) if AmirAge > AmaraAge) {{ cout<< “Amir is older than Amara” ;cout<< “Amir is older than Amara” ;} } else else {{ cout<<“Amir is younger than or of the same age cout<<“Amir is younger than or of the same age

as Amara” ;as Amara” ;}}

Page 22: CS201- Introduction to Programming- Lecture 05

ExampleExampleIf (AmirAge != AmaraAge)If (AmirAge != AmaraAge) cout << “Amir and Amara’s Ages cout << “Amir and Amara’s Ages

are not equal”;are not equal”;

Page 23: CS201- Introduction to Programming- Lecture 05

Unary Not operator Unary Not operator !!

!true = false!true = false !false = true!false = true

Page 24: CS201- Introduction to Programming- Lecture 05

If (!(AmirAge > AmaraAge))If (!(AmirAge > AmaraAge))

??

Page 25: CS201- Introduction to Programming- Lecture 05

ExampleExample

i f (( interMarks > 45) && (testMarks >= passMarks))i f (( interMarks > 45) && (testMarks >= passMarks)){ {

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

Page 26: CS201- Introduction to Programming- Lecture 05

ExampleExample

If(!((interMarks > 45) && (testMarks >= passMarks)))If(!((interMarks > 45) && (testMarks >= passMarks)))

??

Page 27: CS201- Introduction to Programming- Lecture 05

Nested ifNested ifI f (age > 18)If (age > 18){{

If(height > 5)If(height > 5){{

::}}

}}Make a f lowchart of this nested if Make a f lowchart of this nested if

structure…structure…

Page 28: CS201- Introduction to Programming- Lecture 05

In Today’s Lecture In Today’s Lecture DecisionDecision

– IfIf– ElseElse

Flowcharts Flowcharts Nested if Nested if