cs201- introduction to programming- lecture 08
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 08 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture No. 8Lecture No. 8
![Page 2: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/4.jpg)
Multi-way decision Multi-way decision
![Page 5: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/8.jpg)
switch statementswitch statement
![Page 9: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/11.jpg)
case ‘A’ :case ‘A’ :cout << “ Excellent ” ;cout << “ Excellent ” ;…………
switch statements
![Page 12: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/13.jpg)
break;break;
![Page 14: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/17.jpg)
if ( amount > 2335.09 ) if ( amount > 2335.09 ) statements ;statements ;
![Page 18: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/18.jpg)
Whole NumberWhole Number shortshort intint longlong
![Page 19: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/19.jpg)
case ‘A’ :case ‘A’ :case ‘ 300 ‘ :case ‘ 300 ‘ :case ‘ f ‘ :case ‘ f ‘ :
![Page 20: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/20.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/21.jpg)
continue ;continue ;
![Page 22: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/22.jpg)
continuecontinuewhile trynum <= 5 ;while trynum <= 5 ;
{{
……..
……..
continue ;continue ;
}}
![Page 23: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/23.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/25.jpg)
gotogotoUnconditional Branch of ExecutionUnconditional Branch of Execution
![Page 26: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/26.jpg)
SequencesSequences DecisionsDecisions LoopsLoops
Structured Structured ProgrammingProgramming
![Page 27: CS201- Introduction to Programming- Lecture 08](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/28.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/29.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022061303/5492e785b4795900548b45fc/html5/thumbnails/30.jpg)
Data StructuresData Structures– ArraysArrays
Character StringsCharacter Strings PointersPointers
Next MilestonesNext Milestones