Pascal programming
Download
1 / 13

Pascal Programming - PowerPoint PPT Presentation


  • 530 Views
  • Updated On :

Pascal Programming Pascal Loops and Debugging Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used. Now, we add the Boolean expression. As long as the expression evaluates as true, the loop continues. False stops the looping.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Pascal Programming' - ostinmannual


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Pascal programming l.jpg

Pascal Programming

Pascal Loops and Debugging


Pascal programming2 l.jpg
Pascal Programming

  • Pascal Loops

  • In our first brush with the while do loops, simple comparisons were used.

  • Now, we add the Boolean expression.

  • As long as the expression evaluates as true, the loop continues. False stops the looping.


Pascal programming3 l.jpg
Pascal Programming

  • The repeat statement, another method of looping.

  • Syntax . . .

    • repeat

      • Statement or statements;

    • until

      • Boolean-expression

    • end


Pascal programming4 l.jpg
Pascal Programming

  • The body of a repeat may contain multiple statements . . .

  • The body of the while statement contains one statement.

  • The difference holds limited significance because the while statement may be compound.


Pascal programming5 l.jpg
Pascal Programming

  • The for loop . . .

  • Syntax . . .

    • For Control_variable := Initial_expression to Final_expression do

    • Body

  • The value increments by one each time through the loop.

  • The body of the for loop can not change the loop-control variable.

  • The loop-control variable is intended for exclusive use of the loop. (a local variable)


Pascal programming6 l.jpg
Pascal Programming

  • The second version of the for loop decrements . . .

  • In the statement downto replaces to

  • Downto is a reserved word.

  • Each time through the loop it decreases by one.

  • Note: Pascal only increments by one or decrements by one. You must design around this limitation.

  • e g. You want 0, 2, 4, 6, 8, 10 . . .

    • For N := 0 to 5 do

    • Write (2*N)


Pascal programming7 l.jpg
Pascal Programming

  • When designing a Pascal loop, design…

  • The Body

  • The initializing statements

  • The conditions for exiting the loop.

  • We have talked about the first two no let’s look at terminating the loop.


Pascal programming8 l.jpg
Pascal Programming

  • Terminating a loop . . .

    • Terminating input

      • List is headed by size (list size is known)

      • Ask before iterating (ask the user)

      • List ended by sentinal value.

      • Input runs out.


Pascal programming9 l.jpg
Pascal Programming

  • Terminating the loop . . .

    • Count the controlled loops.

    • Ask before iterating.

    • Exit on sentinel value.

    • Input runs out.

    • Exit on a flag condition.

  • A flag is a variable that changes value to indicate that an event has occurred.

  • The end of line (eoln) and end of file (eof) are examples. (Eoln does not work well with numeric data. It prefers char type.)


Pascal programming10 l.jpg
Pascal Programming

  • What loop should I use?

  • Numeric data, fixed iterations, equal changes each time . . .use for

  • Generally, the for loop will be best for numeric data.

  • If the loop may not be used at least once, use while. It also works when no data may be entered from a list.

  • If you insist that the loop be used at least once, use repeat.


Pascal programming11 l.jpg
Pascal Programming

  • Assertions

  • Comments about pre- and post- conditions are called assertions.

  • An invariant assertion is true before the loop runs and true after. The variable changes with each iteration but holds true with each iteration. Thus it is invariant. continue . . .


Pascal programming12 l.jpg
Pascal Programming

  • Assertions cont . . .

  • A variant assertion changes after each iteration.

  • Two conditions . . .

    • The value must decrease by a fixed amount (or more) each iteration.

    • When the variant equals or falls below the threshold, the loop must stop.

    • The threshold is the point to be reached or passed.


Pascal programming13 l.jpg
Pascal Programming

  • Debugging Loops

  • For repeat or while loops . . .check the Boolean expression. Is something reversed?

  • Be sure that the error is in the loop.

  • Trace the data through the loop.

  • Check intervals in long loops.


ad