Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School...
-
Upload
gavin-carpenter -
Category
Documents
-
view
213 -
download
1
Transcript of Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School...
1Dale Roberts
Program Control
- Algorithms
Department of Computer and Information Science,School of Science, IUPUI
CSCI 230
Dale Roberts, LecturerDale Roberts, Lecturer
IUPUIIUPUI
[email protected]@cs.iupui.edu
2Dale Roberts
AlgorithmsAlgorithms
Algorithms 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
3Dale Roberts
Definition of AlgorithmDefinition of Algorithm
Definition of an AlgorithmDefinition of an Algorithmconsists of consists of unambiguousunambiguous & & computablecomputable operations operations
produce a produce a resultresult
halthalt 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.)
4Dale Roberts
Control StructuresControl Structures
Sequential execution Sequential execution Statements executed one after the other in the order writtenStatements executed one after the other in the order written
Transfer of controlTransfer of controlWhen 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
5Dale Roberts
PseudocodePseudocode
PseudocodePseudocodeArtificial, informal language that helps us develop Artificial, informal language that helps us develop algorithmsalgorithms
Similar to everyday EnglishSimilar to everyday English
Not 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 program
Consists only of executable statementsConsists only of executable statements
6Dale Roberts
Control StructuresControl Structures
Flowchart Flowchart Graphical representation of an algorithmGraphical representation of an algorithm
Drawn using certain special-purpose symbols Drawn using certain special-purpose symbols connected by arrows called flowlinesconnected by arrows called flowlines
Rectangle 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
7Dale Roberts
Errors in Errors in Control StructuresControl Structures
Syntax errorsSyntax errorsCaught by compilerCaught by compiler
Logic errors: Logic errors: Have their effect at execution timeHave their effect at execution time
Non-fatal: program runs, but has incorrect outputNon-fatal: program runs, but has incorrect output
Fatal: program exits prematurelyFatal: program exits prematurely
8Dale Roberts
The The ifif Selection Structure Selection Structure
Selection structure: Selection structure: Used to choose among alternative courses of actionUsed to choose among alternative courses of action
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”
If condition If condition truetrue Print statement executed and program goes on to next Print statement executed and program goes on to next statementstatement
If If falsefalse, print statement is ignored and the program goes , print statement is ignored and the program goes onto the next statementonto the next statement
Indenting makes programs easier to readIndenting makes programs easier to readC ignores whitespace charactersC ignores whitespace characters
9Dale Roberts
The The ifif Selection Structure Selection Structure
Diamond symbol (decision symbol)Diamond symbol (decision symbol)Indicates decision is to be madeIndicates decision is to be made
Contains an expression that can be Contains an expression that can be truetrue or or falsefalse
Test 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”
10Dale Roberts
The The ifif Selection Structure Selection Structure
ifif structure is a single-entry/single-exit structure is a single-entry/single-exit structurestructure
true
false
grade >= 60
print “Passed”
11Dale Roberts
33.6.6 The The ifif//elseelse Selection Structure Selection Structure
ififOnly 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”
elseelsePrint “Failed” Print “Failed”
Note spacing/indentation conventionsNote spacing/indentation conventions
12Dale Roberts
The The ifif//elseelse Selection Structure Selection Structure
Flow 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 structures
Once condition is met, rest of statements skippedOnce condition is met, rest of statements skipped
Deep indentation usually not used in practiceDeep indentation usually not used in practice
truefalse
print “Failed” print “Passed”
grade >= 60
13Dale Roberts
The The ifif//elseelse Selection Structure Selection Structure
Pseudocode for a nested Pseudocode for a nested ifif//elseelse structure structure
If 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”
14Dale Roberts
33.7.7 The The whilewhile Repetition Structure Repetition Structure
Repetition structureRepetition structureProgrammer specifies an action to be repeated while Programmer specifies an action to be repeated while some condition remains some condition remains truetrue
Pseudocode: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
15Dale Roberts
33.7.7 The The whilewhile Repetition Structure Repetition Structure
Example: 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
16Dale Roberts
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
17Dale Roberts
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 controlsAll programs can be broken down into 3 controlsSequence Sequence : handled automatically by compiler: handled automatically by compiler
SelectionSelection : : ifif, , ifif//elseelse or or switchswitch
RepetitionRepetition : : whilewhile, , dodo//whilewhile or or forforCan 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 can be statement, and any repetition can be rewritten as a rewritten as a whilewhile statement statement