CS201- Introduction to Programming- Lecture 05

Post on 10-Jul-2015

44 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

Transcript

Introduction to ProgrammingIntroduction to Programming

Lecture 5Lecture 5

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

DecisionDecision

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

If Statement in If Statement in CC

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

If Statement in If Statement in CC

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

statement1 ;statement1 ;statement2 ;statement2 ;

::} }

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

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

Relational Relational OperatorsOperators

a != b;a != b;

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

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

}}

Flow Chart SymbolsFlow Chart SymbolsStart or stop

Process

Continuation mark

Decision

Flow line

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

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

Logical OperatorsLogical Operators

ANDAND &&&&OROR ||||

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)

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

statement ;statement ;----

}}elseelse{{

statement ;statement ;----

}}

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

Code

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

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

Example Example

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

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

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

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

are not equal”;are not equal”;

Unary Not operator Unary Not operator !!

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

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

??

ExampleExample

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

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

ExampleExample

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

??

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…

In Today’s Lecture In Today’s Lecture DecisionDecision

– IfIf– ElseElse

Flowcharts Flowcharts Nested if Nested if

top related