1 / 17

Repetition

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 an infinite loop and you have to press the stop button in the execution window to exit. Loopexample1.t.

louvain
Download Presentation

Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Repetition

  2. 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 an infinite loop and you have to press the stop button in the execution window to exit.

  3. Loopexample1.t % The "ManyCircleAreas" program % Computes the areas of circles varradius, area : real const pi :real := 3.14159 loop put "Enter radius " .. get radius area := pi * radius ** 2 put "Area is ", area end loop

  4. 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.

  5. Conditional Operators • The operators that can be used with the exit whencommand 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.

  6. Loopexample2.t % The "ManyCircleAreas2" program % Compute circle areas until you enter a negative radius varradius, area : real const pi :real := 3.14159 put "Enter a negative radius to stop execution" loop put "Enter radius " .. get radius exit when radius < 0 area := pi * radius ** 2 put "Area is ", area end loop put "That's all folks"

  7. 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.

  8. Comparing Strings • When 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.

  9. Loopexample3.t % The "ComputeAverage" program % Compute the average of a series of marks % Give average to the nearest integer varmark : int varcount, sum : int := 0 const sentinel :real := – 1 put "Enter a series of marks" put "End with ", sentinel loop get mark exit when mark = sentinel count := count + 1 sum := sum + mark end loop put "Average mark is ", round (sum / count )

  10. 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

  11. Loopexample4.t % The "Obey" program % Read in a series of words % until the word "stop" is read varword : string put "Enter a series of words, one to a line" put "If you want to stop say so" loop get word exit when word = "stop" end loop put "This is the end"

  12. Counted Loops • We 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

  13. Loopexample5.t % The "ComputeAverages" program % Reads marks and computes average varmark : int varsum : int := 0 put "Enter marks" for count : 1 .. 5 put count get mark sum := sum + mark end for put "Average is ", round (sum / 5)

  14. 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)

  15. Loopexample6.t varstart, stop : int put "Enter the initial and final values for the loop: " .. getstart, stop fori : start .. stop puti end for

  16. Counting by MultiplesLoopexample7.t %Starts at 2 and goes up by 2 until it reaches 10 for count : 2 .. 10 by 2 put count end for %Starts at 1 and goes up by 5 until it reaches 10 for count : 1 .. 10 by 5 put count end for

  17. Questions • Pg. 138/500 on the IPT.pdf • Do questions #1-6 • LOOK AT THE EXAMPLES FROM THIS NOTE WHEN CODING YOUR PROGRAMS. • REMEMBER TO COMMENT!!!

More Related