Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the...
-
Upload
samson-lester -
Category
Documents
-
view
216 -
download
2
Transcript of Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the...
Repetition
LoopsAllows the same set of
instructions to be used over and over again
Starts with the keyword loop and ends with end loop. This will create an infinite loop and you have to press the stop button in the execution window to exit.
Loopexample1.t% The "ManyCircleAreas" program% Computes the areas of circlesvar radius, area : realconst pi :real := 3.14159loop
put "Enter radius " ..get radiusarea := pi * radius ** 2put "Area is ", area
end loop
Conditional Loops
You can stop a loop by putting a condition in your loop. For example, you can make the first example exit the loop when a negative number is entered for the radius. This signal is called a sentinel.. This is done using the exit when command.
Conditional OperatorsThe operators that can be used with the exit
when command are:> value “Greater than value”< value “Less than value”= value “equal value”>= value “Greater than or equal to value”<= value “Less than or equal to value”not= value “not equal to a value”
Also called BOOLEAN Operator. Returns a value of either True or False.
Loopexample2.t% The "ManyCircleAreas2" program% Compute circle areas until you enter a negative
radiusvar radius, area : realconst pi :real := 3.14159put "Enter a negative radius to stop execution"loop
put "Enter radius " ..get radiusexit when radius < 0area := pi * radius ** 2put "Area is ", area
end loopput "That's all folks"
The condition exit when radius < 0, exits the loop when a negative number is entered in for a radius.
Change the condition so the loop exits when the radius is equal to -1.
Comparing StringsWhen comparing strings, computers use
their ASCII value. for example the character "A" is 65, "B"
is 66, "C" is 67, and so on. Lower case letters have different values: "a" is 97, "b“ is 98, etc. This means that the condition"A" < "B"is true and also that"a" < "B"is false.
Loopexample3.t% The "ComputeAverage" program% Compute the average of a series of marks% Give average to the nearest integervar mark : intvar count, sum : int := 0const sentinel :real := – 1put "Enter a series of marks"put "End with ", sentinelloop
get markexit when mark = sentinelcount := count + 1sum := sum + mark
end loopput "Average mark is ", round (sum / count )
Notice:We initialized the variable sum
and count to 0. Notice the command round() will
round an answer to an operation in the brackets.
Another variable called average should be declared of type real to store the average calculation.
average := round(sum/count)Also… try…..average := sum div count
Loopexample4.t% The "Obey" program% Read in a series of words% until the word "stop" is readvar word : stringput "Enter a series of words, one to a line"put "If you want to stop say so"loop
get wordexit when word = "stop"
end loopput "This is the end"
Counted LoopsWe have looked at conditional and
infinite loops.Counted loops allows you to specify
how many times you want the loop to run.
CODE:for count : startnumber ..
endnumber {Body} end for
Loopexample5.t% The "ComputeAverages" program% Reads marks and computes averagevar mark : intvar sum : int := 0put "Enter marks"for count : 1 .. 5
put countget marksum := sum + mark
end forput "Average is ", round (sum / 5)
Good programming practice
From the previous example, it is good programming practice to use a variable instead of the number “5”.
We could define a constant:◦const numMarks : real := 5
And then change the following lines:◦for count 1 .. numMarks◦put "Average is ", round (sum /
numMarks)
Loopexample6.t
var start, stop : intput "Enter the initial and final values for
the loop: " ..get start, stopfor i : start .. stop
put iend for
Counting by MultiplesLoopexample7.t
%Starts at 2 and goes up by 2 until it reaches 10
for count : 2 .. 10 by 2 put countend for%Starts at 1 and goes up by 5 until it
reaches 10for count : 1 .. 10 by 5 put countend for
QuestionsPg. 138/500 on the IPT.pdfDo questions #1-6
LOOK AT THE EXAMPLES FROM THIS NOTE WHEN CODING YOUR PROGRAMS.
REMEMBER TO COMMENT!!!