1 / 13

# Pascal Programming - PowerPoint PPT Presentation

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.

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

## PowerPoint Slideshow about 'Pascal Programming' - ostinmannual

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

Pascal Loops and Debugging

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

• The repeat statement, another method of looping.

• Syntax . . .

• repeat

• Statement or statements;

• until

• Boolean-expression

• end

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

• 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)

• 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)

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

• Terminating a loop . . .

• Terminating input

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

• List ended by sentinal value.

• Input runs out.

• Terminating the loop . . .

• Count the controlled loops.

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

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

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

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

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