200 likes | 214 Views
Loops. Brent M. Dingle Texas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan). For–To-Do Format. FOR [ctrl var] := [init val] TO [final val] DO [statement] Notice, there is no explicit [expr] in a for-loop.
E N D
Loops Brent M. DingleTexas A&M University Chapter 7 – part C (and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)
For–To-Do Format • FOR [ctrl var] := [init val] TO [final val] DO [statement] • Notice, there is no explicit [expr] in a for-loop. • Basically for-to-loops just count BY 1 from [init val] to [final val]
For-To-Do Example PROGRAM ForCount; VAR Counter : integer; BEGIN FOR counter := 1 TO 10 DO BEGIN writeln(‘count = ‘, counter); END; END.
For-To-Do Example • The above program simply counts from 1 to 10. • Notice that the control variable is “automatically” initialized and incremented by one for you. • Can you create a while loop that will do exactly the same thing? • Try it. • And TEST run the two programs to check!
While-loop, for the lazy • This while loop will count to 10 also: PROGRAM WhileCount; VAR counter : integer; BEGIN counter := 1; while (counter <= 10) do BEGIN writeln(‘count = ‘, counter); counter := counter + 1; END; END.
Observance Check • What do you think would happen if we altered the for line to be:for counter := 10 to 1 do • What would have happened in the while loop if you initialized counter := 10 and said: while (counter <= 1) do • Try it and see if you answered correctly.
PROGRAM ForCount; VAR counter : integer; BEGIN FOR counter := 10 TO 1 DO BEGIN writeln(‘count = ‘, counter); END; END. PROGRAM WhileCount; VAR counter : integer; BEGIN counter := 10; while (counter <= 1) do BEGIN writeln(‘count = ‘, counter); counter := counter + 1; END; END. For the lazy,Here are the two altered progs
Observance Check - Answer • For those of you too lazy to try entering the programs yourself, you would have discovered that ‘nothing’ happens. • More specifically, if you start at 10 can you count to 1 by ADDING 1 (repeatedly) to 10? • Exactly. Since 10 is already >= 1 neither the for-to-do loop nor the while loop would ever execute. • So can you use a for-loop to count backwards? • Yes, see the next example =)
Another ExampleFor-Downto-Do PROGRAM ForCount; VAR Counter : integer; BEGIN FOR counter := 10 DOWNTO 1 DO BEGIN writeln(‘count = ‘, counter); END; END.
For-Downto-Example • Notice the above program counts from 10 down to 1. • Notice the control variable is automatically initialized to 10. • Notice also the control variable is automatically decremented by 1 (i.e. one is subtracted from it).
Challenge • Can you write a program using a for loop to display the capital characters A to Z on the screen? • Could you write it using a while-do loop? • Recall that succ() allowed you see that Pascal “knows” the character after A is B, and after B is C, and so on. • Notice in a for-loop you will NOT need to use the function succ().
PROGRAM ForAlpha; VAR letter : char; BEGIN FOR letter := ‘A’ TO ‘Z’ DO BEGIN write(letter); END; writeln; END. PROGRAM WhileAlpha; VAR letter : char; BEGIN letter := ‘A’; while (letter <= ‘Z’) do BEGIN write(letter); letter := succ(letter); END; writeln; END. Challenge Answer
Challenge two • Can you write a repeat-until loop that does the same as the above two loops? • Or rather, write a repeat-until loop which displays the characters A to Z using the function succ().
Challenge Two Answer • You think you get all the answers for free? • Go try that one on your own. • =)
NEVER in a for-loop • Never increment the control variableof a for-loop inside the loop. • Never write a for-loop like this: FOR count := 1 to 10 DO BEGIN writeln(count); count := count + 1; { BAD LINE !!! } END;
When to use WHILE • If you know there is a condition where the loop should never execute, not even once, a while loop would be a good choice. • While loops also can be used to count by increments other than +1 or –1. • Point some while loops NEVER execute.
When to use REPEAT • If you know that a loop while ALWAYS need to execute at least once then a repeat loop might be a good choice. • Point repeat loops ALWAYS execute AT LEAST ONCE.
When to use FOR • If it is known in advance exactly how many times a loop should be executed (or it is known that there will be a specific number of times entered or calculated) then a for loop is a good choice. • i.e. if you know you will do something 10 times, or N times, a for loop could be a good choice. • Note some for loops NEVER execute.
Suggested Problems (not graded) • page 244, 245 1, 2, 3, 4, 6, 7, 8, 9, 11