+ All Categories
Home > Documents > Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout

Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout

Date post: 31-Dec-2015
Category:
Upload: kristin-lang
View: 219 times
Download: 1 times
Share this document with a friend
53
Lecture 4 Lecture 4 Introduction to Programming Introduction to Programming
Transcript

Lecture 4Lecture 4

Introduction to Introduction to ProgrammingProgramming

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

while loopwhile loop

while (condition)while (condition)

{{

statements;statements;::

}}

statements;statements;

While loop executes zeroWhile loop executes zero

or more times. What if weor more times. What if we

want the loop to executewant the loop to execute

at least one time?at least one time?

do-whiledo-while

Do while loop execute Do while loop execute oneone

or more timesor more times

Syntax of do-while Syntax of do-while looploop

dodo{{

statements ;statements ;

}}while ( condition ) ; while ( condition ) ;

Example-Guessing Example-Guessing gamegame

char c ;char c ; int tryNum = 1 ;int tryNum = 1 ; dodo {{

cout << "Please enter your guess by pressing a character key from cout << "Please enter your guess by pressing a character key from a to z “ ;a to z “ ; cin >> c ;cin >> c ; if ( c == 'z‘ )if ( c == 'z‘ )

{{ cout << "Congratulations! you guessed the right answer“ ;cout << "Congratulations! you guessed the right answer“ ; tryNum = 6 ;tryNum = 6 ;

}} elseelse

tryNum = tryNum + 1 ;tryNum = tryNum + 1 ; } while ( tryNum <= 5 ) ;} while ( tryNum <= 5 ) ;

Flow chart for do-while Flow chart for do-while looploop

Do-while

condition

Process

Exit

true

false

Relational Relational OperatorsOperators

char c ;char c ; int tryNum , maxTries ;int tryNum , maxTries ; tryNum = 1 ;tryNum = 1 ; maxTries = 5 ;maxTries = 5 ; cout << "Guess the alphabet between a to z “ ;cout << "Guess the alphabet between a to z “ ; cin >> c ;cin >> c ; while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) )while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) {{ cout << "Guess the alphabet cout << "Guess the alphabet

between a to z “ ;between a to z “ ; cin >> c ;cin >> c ; tryNum = tryNum + 1 ;tryNum = tryNum + 1 ; }}

for Loopfor Loop

For loopFor loop

forfor ( ( initialization condition initialization condition ; ; termination condition termination condition ; ; increment conditionincrement condition ) ) { {

statement ( s ) ;statement ( s ) ;}}

ExampleExampleint counter ;int counter ;

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

cout << counter;cout << counter;

OutputOutput

01234567890123456789

Table for 2Table for 22 x 1 = 22 x 1 = 2

2 x 2 = 42 x 2 = 4

2 x 3 = 62 x 3 = 6

::

::

2 x 10 = 202 x 10 = 20

Example - Calculate Table Example - Calculate Table for 2for 2

#include <iostream.h>#include <iostream.h>

main ( )main ( )

{{

int counter ;int counter ;

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

{{

cout << "2 x " << counter << " = " << 2* counter << "\n“ ;cout << "2 x " << counter << " = " << 2* counter << "\n“ ;

}}

}}

OutputOutput2 x1 = 22 x1 = 2

2 x 2 = 42 x 2 = 4

2 x 3 = 62 x 3 = 6

::

::

2 x 10 = 202 x 10 = 20

Flow chart for the ‘Table’ Flow chart for the ‘Table’ example example

counter=1

While

Print 2*counter

counter <=10?

Stop

Start

No Exit

Counter = counter + 1

yes

Example: Calculate Table- Example: Calculate Table- EnhancedEnhanced

#include <iostream.h>#include <iostream.h>main ( )main ( ){{ int number ;int number ; int maxMultiplier ;int maxMultiplier ; int counter ;int counter ; maxMultiplier = 10 ;maxMultiplier = 10 ; cout << " Please enter the number for which you wish to construct the table “ cout << " Please enter the number for which you wish to construct the table “

;; cin >> number ;cin >> number ; for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 )for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 ) {{ cout << number <<" x " << counter<< " = " << number * counter cout << number <<" x " << counter<< " = " << number * counter

<< "\n“ ;<< "\n“ ; }}}}

Increment Increment operatoroperator

++++

counter ++ ; counter ++ ;

same as same as counter = counter + 1;counter = counter + 1;

Decrement Decrement operatoroperator

----

counter -- ;counter -- ;

same assame as counter = counter - 1counter = counter - 1

+=+= counter += 3 ; counter += 3 ;

same assame as counter = counter + 3 ;counter = counter + 3 ;

-=-= counter -= 5 ; counter -= 5 ;

same assame as counter = counter – 5 ;counter = counter – 5 ;

*=*=x*=2x*=2

x = x * 2x = x * 2

/=/=

x /= 2x /= 2

x = x / 2 x = x / 2

Compound Assignment Compound Assignment OperatorsOperators

operatoroperator==

%=%= x %= 2 ; x %= 2 ;

same as same as x = x % 2 ;x = x % 2 ;

CommentsComments Write comment at the top Write comment at the top

program to show what it doesprogram to show what it does Write comments that mean Write comments that mean

some thingsome thing

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

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

StructuresStructures ArraysArrays Character Character

StringsStrings PointersPointers

Next MilestonesNext Milestones


Recommended