+ All Categories
Home > Education > CS201- Introduction to Programming- Lecture 08

CS201- Introduction to Programming- Lecture 08

Date post: 18-Dec-2014
Category:
Upload: bilal-ahmed
View: 21 times
Download: 0 times
Share this document with a friend
Description:
Virtual University Course CS201- Introduction to Programming Lecture No 08 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]
Popular Tags:
30
Introduction to Introduction to Programming Programming Lecture No. 8 Lecture No. 8
Transcript
Page 1: CS201- Introduction to Programming- Lecture 08

Introduction to Introduction to ProgrammingProgramming

Lecture No. 8Lecture No. 8

Page 2: CS201- Introduction to Programming- Lecture 08

LoopsLoops– WhileWhile– Do whileDo while– ForFor

OperatorsOperators– Increment / DecrementIncrement / Decrement– Compound Assignment OperatorsCompound Assignment Operators

In the last In the last lecturelecture

Page 3: CS201- Introduction to Programming- Lecture 08

int sum;int sum;int students ;int students ;int average ;int average ;sum = 0 ;sum = 0 ;students = 0 ;students = 0 ;dodo{{

cin >> grade ;cin >> grade ;sum += grade ;sum += grade ;students ++ ;students ++ ;

}}while (grade >= 0) ; while (grade >= 0) ; average = sum / students ;average = sum / students ;cout << average ; cout << average ;

Example: Program to Example: Program to calculate the average marks calculate the average marks

of class of class

A Flaw in the code

Page 4: CS201- Introduction to Programming- Lecture 08

Multi-way decision Multi-way decision

Page 5: CS201- Introduction to Programming- Lecture 08

if ( grade ==‘A’ )if ( grade ==‘A’ )cout << “ Excellent ” ;cout << “ Excellent ” ;

if ( grade ==‘B’ )if ( grade ==‘B’ )cout << “ Very Good ” ;cout << “ Very Good ” ;

if ( grade ==‘C’ )if ( grade ==‘C’ )cout << “ Good ” ;cout << “ Good ” ;

if ( grade ==‘D’ )if ( grade ==‘D’ )cout << “ Poor ” ;cout << “ Poor ” ;

if ( grade ==‘F’ )if ( grade ==‘F’ )cout << “ Fail ” ;cout << “ Fail ” ;

if Statementsif Statements

Page 6: CS201- Introduction to Programming- Lecture 08

if ( grade ==‘A’ )if ( grade ==‘A’ )cout << “ Excellent ” ;cout << “ Excellent ” ;

else else if ( grade ==‘B’ )if ( grade ==‘B’ )

cout << “ Very cout << “ Very Good ” ;Good ” ;

elseelseif ( grade ==‘C’ )if ( grade ==‘C’ )

cout << “ Good ” ;cout << “ Good ” ;elseelse

if ( grade ==‘D’ )if ( grade ==‘D’ )cout << “ Poor ” ;cout << “ Poor ” ;

if elseif else

Page 7: CS201- Introduction to Programming- Lecture 08

if ( grade == ‘A’ )if ( grade == ‘A’ )

cout << “ Excellent ” ;cout << “ Excellent ” ;

else if ( grade == ‘B’ )else if ( grade == ‘B’ )

……

else if …else if …

……

else …else …

if elseif else

Page 8: CS201- Introduction to Programming- Lecture 08

switch statementswitch statement

Page 9: CS201- Introduction to Programming- Lecture 08

switch statementsswitch statements

switch ( variable name )switch ( variable name ){{

case ‘a’ :case ‘a’ :statements;statements;

case ‘b’ :case ‘b’ :statements;statements;

case ‘c’ :case ‘c’ : statements; statements;

……}}

Page 10: CS201- Introduction to Programming- Lecture 08

switch ( grade)switch ( grade){{

case ‘A’ :case ‘A’ :cout << “ Excellent ” ;cout << “ Excellent ” ;

case ‘B’ :case ‘B’ :cout << “ Very Good ” cout << “ Very Good ”

;;case ‘C’ :case ‘C’ :

…………

}}

switch statements switch statements

Page 11: CS201- Introduction to Programming- Lecture 08

case ‘A’ :case ‘A’ :cout << “ Excellent ” ;cout << “ Excellent ” ;…………

switch statements

Page 12: CS201- Introduction to Programming- Lecture 08

ExampleExample

switch ( grade)switch ( grade){{

case ‘A’ :case ‘A’ :cout << “ Excellent ” ;cout << “ Excellent ” ;

case ‘B’ :case ‘B’ :cout << “ Very Good ” ;cout << “ Very Good ” ;

case ‘C’ :case ‘C’ :cout << “Good ” ; cout << “Good ” ;

case ‘D’ :case ‘D’ :cout << “ Poor ” ;cout << “ Poor ” ;

case ‘F’ :case ‘F’ :cout << “ Fail ” ;cout << “ Fail ” ;

}}

Page 13: CS201- Introduction to Programming- Lecture 08

break;break;

Page 14: CS201- Introduction to Programming- Lecture 08

ExampleExampleswitch ( grade )switch ( grade ){{

case ‘A’ :case ‘A’ :cout << “ Excellent ” ;cout << “ Excellent ” ;break ;break ;

case ‘B’ :case ‘B’ :cout << “ Very Good ” ;cout << “ Very Good ” ; break ; break ;

case ‘C’ :case ‘C’ :cout << “Good ” ; cout << “Good ” ; break ;break ;

case ‘D’ :case ‘D’ :cout << “ Poor ” ;cout << “ Poor ” ; break ; break ;

case ‘F’ :case ‘F’ :cout << “ Fail ” ;cout << “ Fail ” ; break ; break ;

}}

Page 15: CS201- Introduction to Programming- Lecture 08

default :default :

cout << “ Please Enter Grade cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ;from ‘A’ to ‘D’ or ‘F’ “ ;

default :default :

Page 16: CS201- Introduction to Programming- Lecture 08

switch (grade)

Display “Excellent”

case ‘B’ :

case ‘A’ :

Display “Very Good”

Default :

“……..”

Flow Chart of switch Flow Chart of switch statementstatement

Page 17: CS201- Introduction to Programming- Lecture 08

if ( amount > 2335.09 ) if ( amount > 2335.09 ) statements ;statements ;

Page 18: CS201- Introduction to Programming- Lecture 08

Whole NumberWhole Number shortshort intint longlong

Page 19: CS201- Introduction to Programming- Lecture 08

case ‘A’ :case ‘A’ :case ‘ 300 ‘ :case ‘ 300 ‘ :case ‘ f ‘ :case ‘ f ‘ :

Page 20: CS201- Introduction to Programming- Lecture 08

if (c == ‘z’ )if (c == ‘z’ )

{{

cout << “ Great ! You have made the correct guess “ ;cout << “ Great ! You have made the correct guess “ ;break ;break ;

}}

break ;break ;

Page 21: CS201- Introduction to Programming- Lecture 08

continue ;continue ;

Page 22: CS201- Introduction to Programming- Lecture 08

continuecontinuewhile trynum <= 5 ;while trynum <= 5 ;

{{

……..

……..

continue ;continue ;

}}

Page 23: CS201- Introduction to Programming- Lecture 08

for ( counter = 0 ;counter <= 10 ; counter ++ )for ( counter = 0 ;counter <= 10 ; counter ++ )

{{

…………..

continue ;continue ;

}}

continue in ‘for’ continue in ‘for’ looploop

Page 24: CS201- Introduction to Programming- Lecture 08

Sequential StatementsSequential Statements DecisionsDecisions

– if , if else , switchif , if else , switch LoopsLoops

– while , do while , forwhile , do while , for

What have we done till now …What have we done till now …

Page 25: CS201- Introduction to Programming- Lecture 08

gotogotoUnconditional Branch of ExecutionUnconditional Branch of Execution

Page 26: CS201- Introduction to Programming- Lecture 08

SequencesSequences DecisionsDecisions LoopsLoops

Structured Structured ProgrammingProgramming

Page 27: CS201- Introduction to Programming- Lecture 08

Minimize the use of breakMinimize the use of break Minimize the use of Minimize the use of

continuecontinue Never use gotoNever use goto

Page 28: CS201- Introduction to Programming- Lecture 08

Guide lines for Guide lines for structured structured

programmingprogramming

Modular Modular Single entry - single exitSingle entry - single exit

Page 29: CS201- Introduction to Programming- Lecture 08

Rule 1 : Use the simplest flowchartRule 1 : Use the simplest flowchart

Rule 2 : Any rectangle can be replaced Rule 2 : Any rectangle can be replaced by by two rectangles. two rectangles.

Rule 3 : Any rectangle can be replaced Rule 3 : Any rectangle can be replaced with with

structured flowcharting structured flowcharting constructs. constructs.

Rule 4 : It says, rule 2 and rule 3 can beRule 4 : It says, rule 2 and rule 3 can be

repeated as many times as repeated as many times as neededneeded

Rules for Structured Rules for Structured FlowchartFlowchart

Page 30: CS201- Introduction to Programming- Lecture 08

Data StructuresData Structures– ArraysArrays

Character StringsCharacter Strings PointersPointers

Next MilestonesNext Milestones


Recommended