190 likes | 337 Views
Loops. When you want a computer to perform the same operation many times you use a loop. Looping is one of the keys to computer programming. First we will look at the FOR / DO Loop. Later in the semester we will look at the WHILE loop provided by Pascal. Loops (con’t).
E N D
Loops • When you want a computer to perform the same operation many times you use a loop. • Looping is one of the keys to computer programming. • First we will look at the FOR / DO Loop. • Later in the semester we will look at the WHILE loop provided by Pascal.
Loops (con’t) • Reminder: A compound statement is a group of statements beginning with BEGIN and ending with END. BEGIN writeln (‘I will not pledge allegiance to Bart’); readln (answer); END; {Note: no period - only at the end of the program}
FOR-DO Loop FOR Index:= 1 to 100 DO writeln(‘Nobody likes sunburn slappers ‘); FOR Index:=1 to 10 DO BEGIN write(Index); writeln(‘There was no Roman god named "Farticus”’); END
FOR-DO loops (con’t) • Control variable is the variable which controls the loop. In the previous examples the integer variable Index was used as the control variable. • The lower and upper limits of the loop must be the same type as the control variable. (all three must be an ordinal type - more on this next class)
FOR-DO loops (con’t) • As long as the types match (and are an ordinal type), the loop is legal. This does not mean the loop is executed. • This loop is legal but will not be executed: FOR Index := 3 TO 2 DO write(‘I will not sleep through my education’);
Averaging numbers (example from book) • Suppose you want to write a program that will average grades in general (ie. You may use a different number of grades each time you run the program.) • requirements specification • analysis • design • implementation / coding • testing
Requirements Specification • Since the English description for this problem is straightforward, the requirements specification step is trivial.
Analysis • Inputs: • # of grades to be averaged: integer • the grades to be averaged: integers • Outputs: • the average: real • other variables: • sum of the grades read so far
Design: First version of pseudocode • Read the number of grades to be averaged; • Sum all the grades to be averaged • Compute and print the average
Design: Pseudocode refinement • Read the number of grades to be averaged; • Sum all the grades to be averaged • Set Sum to zero • FOR GradeNum:= 1 TO TotalGrades DO • Read the Grade; • Add the grade to Sum; • Compute the Average • divide the Sun by TotalGrades; • Print the sum and average;
Implementation Show Pascal program
FOR DOWNTO • Suppose you want to write a FOR-DO loop where the control variable is decreased with each repetition of the loop. Pascal provides the reserved word DOWNTO for this situation. (The first limit value should be greater than the second.) FOR Index := 10 DOWNTO 1 DO
FOR DOWNTO (con’t) • Remember that the loop: FOR Index := 4 TO 3 DOwill never be executed • Similarly, the loop: FOR Index := 3 DOWNTO 4 DOwill never be executed
FOR DO and the buffer • The FOR DO loop can be used to read several characters, one at a time, from the buffer.
Nested loops • When one loop is completely contained in another loop, it is called a nested loop. • We call the nested loop the “inner loop” • The loop it is placed in is called the “outer loop” • The inner loop is executed in full for each value of the outer loop.
Homework #5 • Problem # 15 A and B from page 204 in the text (the problem suggests you read the Social Security number as a series of characters) • Explain how you could do this problem if the social security number is read instead as a longint.
Homework 5 (con’t): • For the longint part. you can use the digits in reverse order (b/c it is easier) • Don’t do the program - just explain • If ss# is 987654321 your output could be:9:999999999 OR 1:1 8:88888888 2:22 7:7777777 3:333 6:666666 4:4444 5:55555 5:55555 4:4444 6:666666 3:333 7:7777777 2:22 8:88888888 1:1 9:999999999