1 / 14

FOR DOWNTO

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

hamlin
Download Presentation

FOR DOWNTO

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

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

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

  4. Ordinal types • An ordinal data type is an ordered set in which every element, except the first element, has an immediate predecessor, and every element, except the last element, has an immediate successor. • all types of integers • characters • boolean • enumerated types (Enumerated types are user defined. We will not cover them in this course)

  5. Ordinal types (con’t) • Reals and strings are not ordinal types because they do not have a unique predecessor or successor. • A FOR-DO loop’s control variable must be an ordinal type. • This is why reals cannot be control variables

  6. ASCII Table • ASCII (American Standard Code for Information Exchange) table contains 128 entries relating whole numbers to characters • Turbo Pascal extends this table with graphical characters. The total number of characters in the table for Turbo Pascal is 256 (0 to 255 or 0000 0000 to 1111 1111). • A character’s position in this table is called it’s ordinal position.

  7. Distinction between integer and its character representation • The number 6 and the character ‘6’ are not the same • The explicit representation of the character 6 in a program is ‘6’ and its ordinal position in the ASCII table is 54. • The explicit representation of the integer 6 in a program is 6 and its ordinal position in the integer sequence is also 6.

  8. Ordinal Functions • ord(ordinal_type) This function returns the ordinal position of the value in the parenthesis. • ord (‘A’) returns 65 • ord (123) returns 123 • ord (TRUE) returns 1 • ord (FALSE) returns 0

  9. Ordinal functions (con’t) • chr(byte) • give this function an integer between 0 and 255 and it returns the character whose ordinal value is that integer. • chr(64) returns ‘@’ • chr(52) returns ‘4’ {note: this is a character} • chr(87) returns ‘W’ • chr(129) returns ‘ü’

  10. Ordinal functions (con’t) • succ() is the successor function. It returns the next value of the ordinal type. • succ(‘A’) returns ‘B’ • succ(3) returns 4 • succ(‘3’) returns ‘4’ • succ(FALSE) returns TRUE • succ(TRUE) does not have a value

  11. Ordinal functions (con’t) • pred() is the predecessor function. It returns the previous value of the ordinal type. • pred(‘B’) returns ‘A’ • pred(3) returns 2 • pred(‘3’) returns ‘2’ • pred(FALSE) does not have a value • pred(TRUE) returns FALSE

  12. Converting numbers read as characters to integers • Sometimes you may want to read numerical digits as characters and convert them to numerical values in your program. Number := ord(char) - ord(‘0’);

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

  14. Homework #4 • 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.

More Related