Você está na página 1de 7

CONTROL STRUCTURES

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;

BEGIN Sum := 0; . Count := 0; .

{Initialise Sum entered}

to zero to add the marks .

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}

{Add the mark to the Sum, to get the


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);

END; END; {end of the while loop}

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

The REPEAT Loop

Repeat Series of steps Until (some Boolean expression is true)

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.

Você também pode gostar