program control - algorithms
DESCRIPTION
Department of Computer and Information Science, School of Science, IUPUI. CSCI N305. Program Control - Algorithms. Algorithms. Algorithms is the are part of the problem solving process ‘A’ in ST A IR. - PowerPoint PPT PresentationTRANSCRIPT
Dale Roberts1
Program Control
- Algorithms
Department of Computer and Information Science,School of Science, IUPUI
CSCI N305
Dale Roberts2
AlgorithmsAlgorithmsAlgorithms is the are part of the problem solving Algorithms is the are part of the problem solving processprocess
‘‘A’ in STA’ in STAAIR.IR.All computing problems can be solved by executing a All computing problems can be solved by executing a series of actions in a specific order, called an algorithmseries of actions in a specific order, called an algorithm
Algorithm: procedure in terms ofAlgorithm: procedure in terms ofActions to be executed Actions to be executed The order in which these actions are to be executedThe order in which these actions are to be executed
Program control Program control Specify order in which statements are to executedSpecify order in which statements are to executed
Dale Roberts3
Definition of AlgorithmDefinition of AlgorithmDefinition of an AlgorithmDefinition of an Algorithm
consists of consists of unambiguousunambiguous & & computablecomputable operations operationsproduce a produce a resultresulthalthalt in a finite amount of time. (Some scientists add a in a finite amount of time. (Some scientists add a restriction that the algorithm must halt in a restriction that the algorithm must halt in a reasonablereasonable amount of time.)amount of time.)
Dale Roberts4
Control StructuresControl StructuresSequential execution Sequential execution
Statements executed one after the other in the order writtenStatements executed one after the other in the order writtenTransfer of controlTransfer of control
When the next statement executed is not the next one in When the next statement executed is not the next one in sequencesequenceOveruse of Overuse of gotogoto statements led to many problems statements led to many problems
Bohm and JacopiniBohm and JacopiniAll programs written in terms of 3 control structuresAll programs written in terms of 3 control structures
Sequence structures: Built into C. Programs executed sequentially Sequence structures: Built into C. Programs executed sequentially by defaultby defaultSelection structures: C has three types: Selection structures: C has three types: ifif, , ifif//elseelse, and , and switchswitchRepetition structures: C has three types: Repetition structures: C has three types: whilewhile, , dodo//whilewhile and and forfor
Dale Roberts5
PseudocodePseudocodePseudocodePseudocode
Artificial, informal language that helps us develop Artificial, informal language that helps us develop algorithmsalgorithmsSimilar to everyday EnglishSimilar to everyday EnglishNot actually executed on computers Not actually executed on computers Helps us “think out” a program before writing it Helps us “think out” a program before writing it
Easy to convert into a corresponding C programEasy to convert into a corresponding C programConsists only of executable statementsConsists only of executable statements
Dale Roberts6
Control StructuresControl Structures
Flowchart Flowchart Graphical representation of an algorithmGraphical representation of an algorithmDrawn using certain special-purpose symbols Drawn using certain special-purpose symbols connected by arrows called flowlinesconnected by arrows called flowlinesRectangle symbol (action symbol):Rectangle symbol (action symbol):
Indicates any type of actionIndicates any type of action
Oval symbol:Oval symbol:Indicates the beginning or end of a program or a section of Indicates the beginning or end of a program or a section of codecode
Single-entry/single-exit control structures Single-entry/single-exit control structures Connect exit point of one control structure to entry Connect exit point of one control structure to entry point of the next (control-structure stacking)point of the next (control-structure stacking)Makes programs easy to buildMakes programs easy to build
Dale Roberts7
Errors in Errors in Control StructuresControl StructuresSyntax errorsSyntax errors
Caught by compilerCaught by compiler
Logic errors: Logic errors: Have their effect at execution timeHave their effect at execution timeNon-fatal: program runs, but has incorrect outputNon-fatal: program runs, but has incorrect outputFatal: program exits prematurelyFatal: program exits prematurely
Dale Roberts8
The The ifif Selection Structure Selection StructureSelection structure: Selection structure:
Used to choose among alternative courses of actionUsed to choose among alternative courses of actionPseudocode:Pseudocode:
If student’s grade is greater than or equal to 60If student’s grade is greater than or equal to 60Print “Passed”Print “Passed”
If condition If condition truetrue Print statement executed and program goes on to next Print statement executed and program goes on to next statementstatementIf If falsefalse, print statement is ignored and the program goes , print statement is ignored and the program goes onto the next statementonto the next statementIndenting makes programs easier to readIndenting makes programs easier to read
C ignores whitespace charactersC ignores whitespace characters
Dale Roberts9
The The ifif Selection Structure Selection StructureDiamond symbol (decision symbol)Diamond symbol (decision symbol)
Indicates decision is to be madeIndicates decision is to be madeContains an expression that can be Contains an expression that can be truetrue or or falsefalseTest the condition, follow appropriate pathTest the condition, follow appropriate path
ifif structure is a single-entry/single-exit structure is a single-entry/single-exit structurestructure
true
false
grade >= 60
print “Passed”
Dale Roberts10
The The ifif Selection Structure Selection Structureifif structure is a single-entry/single-exit structure is a single-entry/single-exit structurestructure
true
false
grade >= 60
print “Passed”
Dale Roberts11
33.6.6 The The ifif//elseelse Selection Structure Selection Structureifif
Only performs an action if the condition is Only performs an action if the condition is truetrue
ifif//elseelseSpecifies an action to be performed both when the Specifies an action to be performed both when the condition is condition is truetrue and when it is and when it is falsefalse
Pseudocode:Pseudocode:If student’s grade is greater than or equal to 60If student’s grade is greater than or equal to 60
Print “Passed”Print “Passed”elseelse
Print “Failed” Print “Failed”
Note spacing/indentation conventionsNote spacing/indentation conventions
Dale Roberts12
The The ifif//elseelse Selection Structure Selection StructureFlow chart of the Flow chart of the ifif//elseelse selection structure selection structure
Nested Nested ifif//elseelse structures structures Test for multiple cases by placing Test for multiple cases by placing ifif//elseelse selection selection structures inside structures inside ifif//elseelse selection structures selection structuresOnce condition is met, rest of statements skippedOnce condition is met, rest of statements skippedDeep indentation usually not used in practiceDeep indentation usually not used in practice
truefalse
print “Failed” print “Passed”
grade >= 60
Dale Roberts13
The The ifif//elseelse Selection Structure Selection StructurePseudocode for a nested Pseudocode for a nested ifif//elseelse structure structureIf student’s grade is greater than or equal to 90If student’s grade is greater than or equal to 90Print “A”Print “A”else else If student’s grade is greater than or equal to 80If student’s grade is greater than or equal to 80 Print “B” Print “B”else else If student’s grade is greater than or equal to 70 If student’s grade is greater than or equal to 70 Print “C” Print “C” else else If student’s grade is greater than or equal to 60 If student’s grade is greater than or equal to 60 Print “D” Print “D” else else Print “F” Print “F”
Dale Roberts14
33.7.7 The The whilewhile Repetition Structure Repetition StructureRepetition structureRepetition structure
Programmer specifies an action to be repeated while Programmer specifies an action to be repeated while some condition remains some condition remains truetruePseudocode:Pseudocode:
While there are more items on my shopping listWhile there are more items on my shopping list Purchase next item and cross it off my list Purchase next item and cross it off my list
whilewhile loop repeated until condition becomes loop repeated until condition becomes falsefalse
Dale Roberts15
33.7.7 The The whilewhile Repetition Structure Repetition StructureExample: Example:
int product = 2;int product = 2;while ( product <= 1000 )while ( product <= 1000 )
product = 2 * product;product = 2 * product;
product <= 1000 product = 2 * producttrue
false
Dale Roberts16
Summary:Summary: S Structured Programmingtructured Programming
Structured programmingStructured programmingEasier than unstructured programs to understand, test, Easier than unstructured programs to understand, test, debug and, modify programsdebug and, modify programs
Rules for structured programmingRules for structured programmingRules developed by programming communityRules developed by programming communityOnly single-entry/single-exit control structures are usedOnly single-entry/single-exit control structures are usedRules: Rules: Rule #1.: Begin with the “simplest flowchart”Rule #1.: Begin with the “simplest flowchart”Rule #2.: Any rectangle (action) can be replaced by two rectangles Rule #2.: Any rectangle (action) can be replaced by two rectangles
(actions) in sequence(actions) in sequenceRule #3.: Any rectangle (action) can be replaced by any control Rule #3.: Any rectangle (action) can be replaced by any control
structure (sequence, structure (sequence, ifif, , ifif//elseelse, , switchswitch, , whilewhile, , dodo//whilewhile or or forfor))Rules 2 and 3 can be applied in any order and multiple timesRules 2 and 3 can be applied in any order and multiple times
Dale Roberts17
Summary:Summary: Structured-ProgrammingStructured-Programming
Rule 2
Rule 1 :
Begin with the simplest flowchart
Rule 2 :
Any rectangle can be replaced by two rectangles in sequence
Rule 2 ...Rule 2
Rule 3
Rule 3 Rule
3
Rule 3 :
Replace any rectangle with a control structure
All programs can be broken down into 3 All programs can be broken down into 3 controlscontrolsSequence Sequence : handled automatically by compiler: handled automatically by compilerSelectionSelection : : ifif, , ifif//elseelse or or switchswitch RepetitionRepetition : : whilewhile, , dodo//whilewhile or or forfor
Can only be combined in two ways: Nesting (Rule 3) and Stacking (Rule 2)Can only be combined in two ways: Nesting (Rule 3) and Stacking (Rule 2)
Any selection can be rewritten as an Any selection can be rewritten as an ifif statement, and any repetition statement, and any repetition can be rewritten as a can be rewritten as a whilewhile statement statement