59
Programming Dr Damitha Karunaratna Senior Lecturer University of Colombo School of Computing [email protected] 1

ICT Seminar Flow Charts for 2013 Nov

Embed Size (px)

DESCRIPTION

Flow chart

Citation preview

Slide 1

Programming

Dr Damitha KarunaratnaSenior LecturerUniversity of Colombo School of [email protected]

UCSC Algorithms/Flow Charts What is an algorithm?Different ways of representing algorithms.What is a computer program?Main steps in solving a problem by using computer programs. How can an algorithm converted into a computer program?

2UCSC What is an algorithm? An algorithm is a step-by-step procedure (a finite number of steps) for solving a problem. AssumptionsObjectiveStarting pointProcessTerminal point(s)Algorithm to get ready to come to school.

2UCSCDifferent ways of representing algorithmsFlow charts Graphical representationPseudo code Text representationA great deal of pseudo code is a blend of simple imperative constructs and English

4UCSCSymbols used in Flow chartsProcessDecisionInput/OutputTerminatorConnectorFlowDirection5UCSCIncorrect Flow charts ????Is remainder(X/2) = 0 ?Read a number (X)Start6The number is oddThe number is evenA Flow chart to determine whether a given number is odd or evenUCSCIncorrect Flow chartsY = remainder (x/2)Is y = 0?Read a number (X)Start7StopDisplay The number is oddDisplay The number is evenA Flow chart to determine whether a given number is odd or evenyesNoUCSCHow to encode a flow chart as a program ? Must know the various constructs provided by the language.Reserved wordsFlow control mechanisms SequencingSelectionRepetition (Iteration/Loops)Decompose(disintegrate) a program into a collection of subprograms (procedures)8UCSC Flow Charts - Selection Types of Flow Control StatementsIF ..ThenElse .End IfNest IF Select Case End Select

19

UCSC Flow Charts - Repetition Types of Flow Control StatementsFor..NextDo..While conditionDo..Until condition

Loops110

UCSCIF .. ThenElseCondition?Statement 1Statement 3Statement 2One course-of-actionAnother course-of-actionTrueFalse11

UCSCIF .. ThenElse - ExampleTrueFalseIF a Student obtained at least 50 marks he will pass the Exam , otherwise Fail.Marks>=50?Get Student Marks Print Pass the Exam Print Fail the Exam12UCSCIF .. ThenElse : Example Pseudo CodeGet Student Mark (M) IF (M>=50) Then Display/Print PassELSE Display/Print FailENDIF13UCSCNested IF ExampleIF a Student obtained more than 80 marks he will get a Distinction else If he obtained more than or equal to 50, he will pass else will Fail. 14UCSC14Nested IF - ExampleMarks>80?TrueFalseMarks>=50?TrueFalse Print Distinction Print FailPrint PassGet Marks15UCSCNested IF - ExampleMarks>=50?TrueFalseMarks>80?TrueFalse Print Pass Print FailPrint DistinctionGet Marks16This part will never get executedUCSCIF .. ThenElse : Example Pseudo CodeGet Student Mark (M) IF (M>80) THEN Display DistinctionELSE IF (M>=50) THEN Display Pass ELSE Display Fail ENDIFENDIF17UCSCSelect Case End SelectCase can evaluate only one variable whereas Nest IF can evaluate multiple variables.No special Flow chart symbol

18UCSC18Select Case End Select Example - Pseudo CodeGet Student Mark (M) Select Case ( M)Case 0 To 49 Display FailCase 50 To 80 Display PassCase Else Display Distinction END Select19UCSCIterative Statements Loops20

Condition can be at the end of the loop

While conditionStatement(s)TrueFalseI = 1Do Print I I = I + 1Loop while I < 5UCSC20Iterative Statements Loops21

Or Condition can be at the start of the loop

While conditionStatement(s)TrueFalseI = 1Do while I < 5 Print I I = I + 1Loop UCSC21Iterative Statements LoopsFalse22

Statement (s)Statement(s) (s)Do WhileEnd DoTrueStatement(s) (s)Statement (s)Statement (s)Do UntilEnd DoTrueStatement (s)FalseUCSC22Iterative Statements For NextFor I= 1 To 5 Print I Next II=1I=I+1 I