| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 36
- TURBO-LESSON 9: FOR STATEMENT
- OBJECTIVES - In lesson 9, you will learn about:
- 1. Using ":n" to specify field width in Write statements
- 2. FOR statement
- 1. Using ":n" to specify field width in Write statements.
- Spacing of output in previous lessons has been done using spaces
- in string constants, such as, 'I= '. The space after the = will
- be printed as part of the string.
- There is another way to add spaces. When listing the items to
- write in a Write or WriteLn statement, ":n" can be added to
- specify how many characters the item is to occupy in the output.
- For example:
- WriteLn(I:4);
- The value of I would be printed in a space 4 characters wide.
- ##### DO:
- Run Program TEST1 several times with the following statements in
- the main part of the program:
- ReadLn(I);
- WriteLn('[', I:4, ']');
- Look closely at the output. Are the numbers "left-justified" or
- "right-justified"?
- If I has a value of 23, and 23 prints in columns 1 and 2 of the
- 4 column field, it is left-justified. If the 23 appears in
- columns 3 and 4 of the 4 colunm field, it is right-justified.
- What happens if you enter a number that is too large to fit the
- field specified?
- ##### DO:
- Run the program several times using the following values for I:
- 123, 1234, -1234, 12345
- Œ
- TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 37
- 2. FOR statement.
- In the previous lesson, you were reminded of the various ways to
- control the sequencing of program actions. One of the REPETITION
- structures is the FOR statement. The forms of the statement are:
- FOR variable := lowvalue TO highvalue DO
- Statement; {May be a block statement with BEGIN END}
- FOR variable := highvalue DOWNTO lowvalue DO
- Statement;
- ##### DO:
- Run PROG9 several times to see how it works.
- Experiment with both positive and negative numbers.
- What happens if High is smaller than Low?
- ##### DO:
- Run PROG9 using 5 for the Low, 3 for the High?
- What was the result?
- Also try 5 for both values. How many times is the loop repeated
- if Low = High?
- What results would you expect if you entered 3.4 for Low and 5.3
- for High?
- ##### DO:
- Run the program with the values 3.4 and 5.3.
- Were you correct in predicting the outcome?
- Can constants be used instead the variables Low and High?
- ##### DO:
- Edit the FOR statement in PROG9 as follows:
- FOR Index := 1 to 5 DO
- Run the Program.
- Is it possible to use expressions for Low and High?
- ##### DO:
- Change the FOR statement to:
- FOR Index := Low + 1 TO High DO
- Run the Program.
- Œ
|