1 / 18

Understanding Loops in Computer Programming

Learn about loops in computer programming, including the FOR/DO loop and WHILE loop in Pascal. Explore control variables, nested loops, and example programs.

jjaime
Download Presentation

Understanding Loops in Computer Programming

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

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

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

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

  5. 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’);

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

  7. Requirements Specification • Since the English description for this problem is straightforward, the requirements specification step is trivial.

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

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

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

  11. Implementation Show Pascal program

  12. testing

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

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

  15. FOR DO and the buffer • The FOR DO loop can be used to read several characters, one at a time, from the buffer.

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

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

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

More Related