Loops - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Loops

Description:

Reminder: A compound statement is a group of statements beginning with ... writeln( Nobody likes sunburn slappers ); FOR Index:=1 to 10 DO. BEGIN. write(Index) ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: evank
Learn more at: https://cs.nyu.edu
Category:
Tags: loops | sunburn

less

Transcript and Presenter's Notes

Title: Loops


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 (cont)
  • 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 Index1 to 10
DO BEGIN write(Index) writeln(There
was no Roman god named "Farticus) END
4
FOR-DO loops (cont)
  • 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 (cont)
  • 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 (cont)
  • 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 (cont)
  • For the longint part. you can use the digits in
    reverse order (b/c it is easier)
  • Dont do the program - just explain
  • If ss is 987654321 your output could
    be 9999999999 OR 11 888888888 222 777777
    77 3333 6666666 44444 555555 555555 4
    4444 6666666 3333 77777777 222 888888
    888 11 9999999999
Write a Comment
User Comments (0)
About PowerShow.com