1 / 15

Input and Output (1)

Input and Output (1). Output Statement. Output Statement. Display text on the screen You can display a string , and any other variables There are two output statements in Pascal write writeln. write statement. Syntax: write ( <output list> )

astra
Télécharger la présentation

Input and Output (1)

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. Input and Output (1) Output Statement

  2. Output Statement • Display text on the screen • You can display a string, and any other variables • There are two output statements in Pascal • write • writeln

  3. write statement • Syntax: • write ( <output list> ) • <output list> could be a single expression or a list of expressions separated by commas • Each of these expressions may be a constant, variable or a formula (expression)

  4. write statement (cont.) • Some examples • write ( ‘hello’ ); • write ( 1234567 ); • write ( 3 * 6 ); • Output variables • var Num1 : integer; • Num1 := 10; • write ( Num1 );

  5. More than one expression • To output more than one expression in one statement, use commas to separate expressions • Example: • write ( ‘I am ‘, ‘Mr. Wong’); • I am Mr. Wong • write ( ‘2 + 3 = ‘, 2 + 3 ); • 2 + 3 = 5

  6. space Output integer and real • integer variables are output as they are • write ( 10 ); 10 • write ( -10 ); -10 • real variables are output in exponential form • write ( 135.0 ); b1.35000000E+02 • write ( -135.0 ); -1.35000000E+02

  7. Formatted Output • Formatting integer values • write ( <integer expression>:<w> ); • The number w specify the number of characters to display • The integer is displayed right-aligned, and blanks are added in front of it if the length of the integer is shorter than w • If the length of the integer is lager than w , the whole integer is displayed

  8. Formatted Output • Examples on formatted integer output • write ( 1234:7 ); • bbb1234 • write ( 1234:4 ); • 1234 • write ( 1234:2 ); • 1234

  9. Formatted Output • Formatting real values • write ( <real expression>:<w>:<d> ); • The number w specify the total number of characters to display, and d specify the number of decimal places to display • The real number is displayed right-aligned, and blanks are added in front of it if the length of the real value is smaller than w

  10. Formatted Output • Formatting real values (cont.) • If the length of a real value is lager than w , the whole number is displayed • If the number of decimal is larger than d, the value is rounded to d decimal places • If d is not given, the value is displayed in exponential form

  11. Formatted Output • Examples on formatted real values output • write ( 12.45:10:4 ); • bbb12.4500 • write ( 12.45:10:2 ); • bbbbb12.45 • write ( 12.45:10:1 ); • bbbbbb12.5 • write ( 12.45:10 ); • b1.245E+01

  12. Formatted Output • Formatting strings and characters • write ( <string or character>:<w> ); • The number w specify the number of characters to display • The character(s) are displayed right-aligned, and blanks are added in front of it if the length is shorter than w • If the length is lager than w , the whole string / character is displayed

  13. Formatted Output • Examples on formatted string / character output • write ( ‘A’:4 ); • bbbA • write ( ‘ABCDEF4’:3 ); • ABCDEF4

  14. writeln statement • writeln statement is similar to the write statement except that the cursor is advanced to the next line after the output is displayed • An empty writeln statement (no argument given) will curse the cursor to advance to a new line

  15. writeln statement • Example 1 • write ( ‘I am ’ ); • writeln ( ‘Mr. Wong’ ); • Example 2 • writeln ( ‘I am Mr. Wong’ ); • Example 3 • write ( ‘I am ’ ); • write ( ‘Mr. Wong’); • writeln; • All these three examples result in same output

More Related