Introduction to Introduction to ProgrammingProgramming
Lecture No. 8Lecture No. 8
LoopsLoops– WhileWhile– Do whileDo while– ForFor
OperatorsOperators– Increment / DecrementIncrement / Decrement– Compound Assignment OperatorsCompound Assignment Operators
In the last In the last lecturelecture
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
Multi-way decision Multi-way decision
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
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
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
switch statementswitch statement
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;
……}}
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
case ‘A’ :case ‘A’ :cout << “ Excellent ” ;cout << “ Excellent ” ;…………
switch statements
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 ” ;
}}
break;break;
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 ;
}}
default :default :
cout << “ Please Enter Grade cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ;from ‘A’ to ‘D’ or ‘F’ “ ;
default :default :
switch (grade)
Display “Excellent”
case ‘B’ :
case ‘A’ :
Display “Very Good”
Default :
“……..”
Flow Chart of switch Flow Chart of switch statementstatement
…
if ( amount > 2335.09 ) if ( amount > 2335.09 ) statements ;statements ;
Whole NumberWhole Number shortshort intint longlong
case ‘A’ :case ‘A’ :case ‘ 300 ‘ :case ‘ 300 ‘ :case ‘ f ‘ :case ‘ f ‘ :
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 ;
continue ;continue ;
continuecontinuewhile trynum <= 5 ;while trynum <= 5 ;
{{
……..
……..
continue ;continue ;
}}
for ( counter = 0 ;counter <= 10 ; counter ++ )for ( counter = 0 ;counter <= 10 ; counter ++ )
{{
…………..
continue ;continue ;
}}
continue in ‘for’ continue in ‘for’ looploop
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 …
gotogotoUnconditional Branch of ExecutionUnconditional Branch of Execution
SequencesSequences DecisionsDecisions LoopsLoops
Structured Structured ProgrammingProgramming
Minimize the use of breakMinimize the use of break Minimize the use of Minimize the use of
continuecontinue Never use gotoNever use goto
Guide lines for Guide lines for structured structured
programmingprogramming
Modular Modular Single entry - single exitSingle entry - single exit
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
Data StructuresData Structures– ArraysArrays
Character StringsCharacter Strings PointersPointers
Next MilestonesNext Milestones