repetition. loops allows the same set of instructions to be used over and over again starts with the...
TRANSCRIPT
![Page 1: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/1.jpg)
Repetition
![Page 2: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/2.jpg)
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.
![Page 3: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/3.jpg)
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
![Page 4: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/4.jpg)
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.
![Page 5: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/5.jpg)
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.
![Page 6: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/6.jpg)
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"
![Page 7: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/7.jpg)
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.
![Page 8: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/8.jpg)
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.
![Page 9: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/9.jpg)
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 )
![Page 10: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/10.jpg)
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
![Page 11: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/11.jpg)
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"
![Page 12: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/12.jpg)
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
![Page 13: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/13.jpg)
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)
![Page 14: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/14.jpg)
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)
![Page 15: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/15.jpg)
Loopexample6.t
var start, stop : intput "Enter the initial and final values for
the loop: " ..get start, stopfor i : start .. stop
put iend for
![Page 16: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/16.jpg)
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
![Page 17: Repetition. Loops Allows 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](https://reader035.vdocuments.us/reader035/viewer/2022062519/5697bfc21a28abf838ca517f/html5/thumbnails/17.jpg)
QuestionsPg. 138/500 on the IPT.pdfDo questions #1-6
LOOK AT THE EXAMPLES FROM THIS NOTE WHEN CODING YOUR PROGRAMS.
REMEMBER TO COMMENT!!!