150 likes | 308 Views
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> )
E N D
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> ) • <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)
write statement (cont.) • Some examples • write ( ‘hello’ ); • write ( 1234567 ); • write ( 3 * 6 ); • Output variables • var Num1 : integer; • Num1 := 10; • write ( Num1 );
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
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
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
Formatted Output • Examples on formatted integer output • write ( 1234:7 ); • bbb1234 • write ( 1234:4 ); • 1234 • write ( 1234:2 ); • 1234
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
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
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
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
Formatted Output • Examples on formatted string / character output • write ( ‘A’:4 ); • bbbA • write ( ‘ABCDEF4’:3 ); • ABCDEF4
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
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