140 likes | 241 Views
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).
E N D
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) • 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
FOR DO and the buffer • The FOR DO loop can be used to read several characters, one at a time, from the buffer.
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)
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
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.
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.
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
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 ‘ü’
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
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
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’);
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.
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.