122 views

Uploaded by api-247871582

- 8.4.3 Use of Control Structure Repetition
- common terms and concepts in programming
- elementary data types
- csec mayjune2013 infotech sr
- Introduction to Object-Oriented Programming
- declaring variables and constants
- csec 2008 june information technology schools report
- csec mayjune2012 informationtechonology sr 1
- 2012 csec it paper 2
- csec 2005 infotech
- csec mayjune2013 infotech sr
- 106819450-MQL-for-Traders.pdf
- MATLAB TUTORIAL BASIC
- C++ Summarized Notes
- 9781530904389(4)
- Excel VBA Steve
- Microsoft_Word___BBA_IV_Semester_970630290.pdf
- GCSE Computer Science for OCR Teachers Resource
- Perl Workshop
- Logix Wp008 en p

You are on page 1of 7

Conditional Branching

Conditional branching involves making a choice between one or two options. Here is the algorithm for a problem to find the average of a set of numbers.

Read the number of marks to be averaged into variable called TotalMarks Set Sum to zero Set Count to zero While Count <> -1 DO BEGIN Read the mark IF (Mark <> -1) {we have NOT come to the end of the marks} THEN BEGIN Add the mark to Sum Add 1 to Count END END {While} Compute the Average (Sum/TotalMarks) Print the average

The IF-THEN statement here will allow the mark to be added to the Sum and also to continue counting how many marks are being entered ONLY IF the mark entered is NOT equal to -1. Remember -1 here is being used as a signal that we have come to the end of the marks that we are adding up. So if we have a set of marks, they could be: 9, 5, 7, 6, 3, 8, 9, 7, -1 These numbers then show that we have eight marks and the 9th number is -1 to indicate that we have no more marks to enter. Here is the Pascal code that includes an IF-THEN statement :

Program IFTHEN; VAR Sum,num, Mark:: Integer; {Assuming the marks are integers, otherwise . they can be of type Real} Average: Real;

While (Mark <> -1) BEGIN Write("Enter a mark: "); Readln(Mark); IF (Mark <> -1)THEN BEGIN Sum := Sum + Mark:

.

{Prompt for the total number of marks to be . averaged} DO {Enter the marks individually}

total}

Count := Count + 1; END; END; {While} Average := Sum/Count; Writeln("The average mark is ",Average); Readln; END.

Once the Mark entered is -1, the average is then calculated and output. However, if the Mark entered is not equal to -1, then the mark is added to the sum of marks, and the count is incremented. Can we improve on this program? Yes we can!

Program IFTHENELSE; VAR Sum, num, Mark: Integer; {Assuming the marks are integers, otherwise . they can be of type Real} Average: Real; BEGIN Sum := 0; {Initialise Sum to zero to add the marks entered} Count := 0; {Prompt for the total number of marks to be averaged} While (Mark <> -1) DO BEGIN Write("Enter a mark: "); {Enter the marks individually} Readln(Mark); IF (Mark <> -1)THEN BEGIN Sum := Sum + Mark: {Add the mark to the Sum, to get . . the total} Count := Count + 1; END

ELSE BEGIN {Else, the Mark entered is -1 } Average := Sum/Count; Writeln("The average mark is ",Average);

LOOPS Design: Pseudocode refinement The FOR Loop Read the number of marks to be averaged Set Sum to zero FOR GradeNum = 1 TO TotalMarks DO BEGIN Read the Mark Add the mark to Sum END {FOR} Compute the Average Print the average Note that when you use the FOR loop you must know how many times you want to perform an action. This causes problems if you didnt know how many times an action was to be performed Here is the Pascal code that uses a FOR loop: Program FORLOOP; VAR Sum,num, Mark,TotalMarks: Integer; they can be of type Real} Average: Real; BEGIN Sum := 0; . {Initialise Sum to zero to add the marks . . entered} Write("Enter how many marks you will be averaging" "); . Readln(TotalMarks); FOR num := 1 to TotalMarks DO BEGIN Write("Enter a mark: "); Readln(Mark); Sum := Sum + Mark; END; Average := Sum/TotalMarks; Writeln("The average mark is ",Average); END. {Add the mark to the Sum, to get the total} {Enter the marks individually} {Prompt for the total number of marks to be averaged} . {Assuming the marks are integers, otherwise

The WHILE loop WHILE (some Boolean expression is true) DO some particular action Here is the algorithm for the same problem to find the average of a set of numbers Read the number of marks to be averaged into variable called TotalMarks Set Sum to zero Set Count to zero While Count < TotalMarks DO BEGIN Read the mark Add the mark to Sum Add 1 to Count END {While} Compute the Average (Sum/TotalMarks) Print the average

Here is the Pascal code for the while loop: Program WHILELOOP; VAR Sum,num, Mark,TotalMarks: Integer; {Assuming the marks are integers, otherwise they can be of type Real} Average: Real; BEGIN Sum := 0; entered} Count := 0; be averaged} Write("Enter how many marks you will be averaging" "); Readln(TotalMarks); While Count < TotalMarks DO BEGIN Write("Enter a mark: "); {Enter the marks individually} Readln(Mark); Sum := Sum + Mark: {Add the mark to the Sum, to get the total} Count := Count + 1; END; Average := Sum/TotalMarks; Writeln("The average mark is ",Average); END.

{Initialise Sum to zero to add the marks {Prompt for the total number of marks to

Here is the algorithm for the same problem to find the average of a set of numbers Read the number of marks to be averaged into variable called TotalMarks Set Sum to zero Set Count to zero Repeat BEGIN Read the mark Add the mark to Sum Add 1 to Count END {Repeat} Until (Count < TotalMarks) Compute the Average (Sum/TotalMarks) Print the average

Program WHILELOOP; VAR Sum,num, Mark,TotalMarks: Integer; {Assuming the marks are integers, otherwise they can be of type Real} Average: Real; BEGIN Sum := 0; {Initialise Sum to zero to add the marks entered} Count := 0; {Prompt for the total number of marks to be averaged} Write("Enter how many marks you will be averaging" "); Readln(TotalMarks); While Count < TotalMarks DO BEGIN Write("Enter a mark: "); {Enter the marks individually} Readln(Mark); Sum := Sum + Mark: {Add the mark to the Sum, to get the total} Count := Count + 1; END; Average := Sum/TotalMarks; Writeln("The average mark is ",Average); Readln; END.

Notes on While and Repeat Loops With these types of loops, at least one of the statements in the loop must change the value of the Boolean expression so it will eventually become false, and move on to the other statements in the program. Otherwise, the loop will execute forever (this is called an infinite loop). The variables in the Boolean expression that controls the WHILE loop must all be defined. If they are not defined in an earlier part of the program, you must initialize them before the loop begins.

Suppose you want to jump out of the loop The above examples illustrated the calculation of a specific number of marks that you want to find the average of. Sometimes you may not know how many marks are to be entered.This is where we use a value that when we enter it, it forces us to stop executing the statements in the loop, and jump out of the loop, so to speak. So, we use what is sometimes called a sentinel A sentinel is a piece of data that indicates that you want to stop executing the loop, or that you have reached the end of the data that you have been entering. Values such as 999 or -1 are used as sentinels to indicate that you have reached the end of the data. For example, if we write a program that a teacher uses to enter the marks for a test, the value AFTER the last test mark can be indicated by a -1.

- 8.4.3 Use of Control Structure RepetitionUploaded byLily Mardyana
- common terms and concepts in programmingUploaded byapi-247871582
- elementary data typesUploaded byapi-247871582
- csec mayjune2013 infotech srUploaded byapi-247871582
- Introduction to Object-Oriented ProgrammingUploaded byTendai Mare
- declaring variables and constantsUploaded byapi-247871582
- csec 2008 june information technology schools reportUploaded byapi-247871582
- csec mayjune2012 informationtechonology sr 1Uploaded byapi-247871582
- 2012 csec it paper 2Uploaded byapi-247871582
- csec 2005 infotechUploaded byapi-247871582
- csec mayjune2013 infotech srUploaded byapi-247871582
- 106819450-MQL-for-Traders.pdfUploaded bygorkemkebir
- MATLAB TUTORIAL BASICUploaded byhalimsha
- C++ Summarized NotesUploaded bychandni1972
- 9781530904389(4)Uploaded byEdy Setiawan
- Excel VBA SteveUploaded byMohan Kumar
- Microsoft_Word___BBA_IV_Semester_970630290.pdfUploaded byAkram Mohammad
- GCSE Computer Science for OCR Teachers ResourceUploaded byranpande
- Perl WorkshopUploaded byVenkat Kiran Sribhashyam
- Logix Wp008 en pUploaded byAnonymous YhFsrjunV0
- SAPABAPFAQ1Uploaded byRajiv Kumar
- 11th Physics Assessmentuiytyu SchemueUploaded byCH M Ahmed
- Test Data Generation for Database ApplicationsUploaded byJexia
- Introduction to MatlabUploaded byHassaan Shah
- Open FoamUploaded byVarghese Mathew
- Java programUploaded byAnupam Mani Tripathi
- CreateSpace.java.Mar.2016.ISBN.1530669170Uploaded byBenjo Fausto
- Introduction to CUploaded byagbo_36
- A Recursion Removal Theorem -- Proof and ApplicationsUploaded bymartinward
- SWITCH Control Structure n Loop FOR-WHILE - DO.pdfUploaded byikram2901

- send and retrieve information via the internetUploaded byapi-247871582
- august 5 events planning activityUploaded byapi-247871582
- thhepm0072b-integrate technology into the event management processUploaded byapi-247871582
- call center script 4Uploaded byapi-247871582
- call center script 3Uploaded byapi-247871582
- call center script 2Uploaded byapi-247871582
- call center script 1Uploaded byapi-247871582
- thhepm0042b-select install and operate audio for an outdoor eventUploaded byapi-247871582
- select install and operate audio for an outdoor eventUploaded byapi-247871582
- events panning end of cluster test topicsUploaded byapi-247871582
- deliveringbusinesspresentations-150629191413-lva1-app6892Uploaded byapi-247871582
- eavesdroppingUploaded byapi-247871582
- 3Uploaded byapi-247871582
- gr 8 terms computer ethicsUploaded byapi-247871582
- heart evening practice test questions 2 1Uploaded byapi-247871582
- Excel 2007Uploaded bysanchita22
- computer operations 2Uploaded byapi-247871582
- organize control recordsUploaded byapi-247871582
- how to scan a document using windows 7Uploaded byapi-247871582
- gr 11 it mte 2018 final 1Uploaded byapi-247871582
- excelnotes 1 1Uploaded byapi-247871582
- ms excel activity 1Uploaded byapi-247871582
- the operating system notesUploaded byapi-247871582
- ppt2 - communicate in the workplace-revisedUploaded byapi-247871582
- network admin end of cluster assessment 2017csUploaded byapi-247871582
- operate database application basicUploaded byapi-247871582
- operate a presentation package basicUploaded byapi-247871582
- perform data entry operationsUploaded byapi-247871582

- Intraday Trading Using Camarilla and Advanced CamarillaUploaded bywierdooo
- Marketing Test 2Uploaded byLauren
- Anthony Timeline Events and PeopleUploaded byMyra Mains
- Cross Price ElasticityUploaded byzaraazahid
- Creative Machine Embroidery - July-August 2015Uploaded bytroxo
- Infosys 110 deliverable 2 cliu197Uploaded byTony Liu
- Hotel Management System 2.docxUploaded byAhmed Mohamed
- FBSCORE2CBLM and Assessment Tool - CopyUploaded byAlvin S. seletaria
- MB0039– Business Communication1Uploaded byNishanth Kumar Vaishnav
- SWOT matrix.docxUploaded byDonnaGreenidge-Grazette
- SOBC Booklet FinalUploaded byArsalan Idrees
- Push Service with ASP.NET SignalRUploaded byAtef Zizou
- topic 3 soilUploaded byapi-253170211
- Playing For the Commandant by Suzy Zail Chapter SamplerUploaded byCandlewick Press
- ASP Interview QuestionUploaded bysuhas_kognole1575
- BIOL1230Uploaded bylpalo004
- Central Bank Education LoanUploaded bymoneymaya
- VSL News 1996 2 ArtUploaded byrleszka
- Gartner Five Trends Internal AuditUploaded byShirley Liew
- Merchandising ManagerUploaded byapi-78854015
- Product PEQ C100 SonyUploaded byAnonymous ADJGjz6Vt
- The Moodle Book ModuleUploaded byEdTechSystems
- Beacham-Motion-for-Reconsideration-Supreme-Court-GA-Portnoy-Strawser-2014Uploaded byDeb Beacham
- Solid State on Load Tap Changer for Transformer Using ArduinoUploaded byIRJET Journal
- Accenture Placement Paper 2011Uploaded byAnurag Bhavsar
- OrcaUploaded byAlina Andreea
- Drum Brake CalculationUploaded byKarthigaiselvan Shanmuganathan
- 102012_steelwise.pdfUploaded byrohalestat
- Vehicle Kit SpecsUploaded byfgrefe
- CarbuncleUploaded bykonarkarora