J. Michael MooreInput and Output (IO)CSCE 110Drawn from James Tam's materialJ. Michael MooreOutput• Displaying information on screen• Done via the write and writeln statements– Write: displays the output ______________(the cursor remains on the line)– Writeln: displays the output _________________ (the _______ moves to __________)J. Michael MooreOutput• Format (literal string of characters):write ('a message');ORwriteln ('a message');J. Michael MooreOutput Exampleprogram outputExample1 (output);beginwrite('line1');writeln('line2');write('line3');end.Style conventionOr is it?• You can find an copy of this program and the compiled version in the course directory:/pub/courses/csce110/J. Michael MooreOutput the Contents Of Variables And Constants• Format:write(<name of variable> or <constant>);or writeln (<name of variable> or <constant>);J. Michael MooreOutput Variables And Constants Exampleprogram outputExample2 (output);constACONSTANT = 888;beginvar num : integer;num := 7;writeln(ACONSTANT);writeln(num);end.• You can find an copy of this program and the compiled version in the course directory:/pub/courses/csce110/J. Michael MooreMixed Output• It's possible to display literal strings of characters and the contents of variables and constants with a single write or writeln statement.• Format:write('message', <name of variable>, 'message'…);ORwriteln('message', <name of variable>, 'message'…);J. Michael MooreMixed Output Exampleprogram outputExample3 (output);constACONSTANT = 888;beginvar num : integer;num := 7;writeln('ACONSTANT: ', ACONSTANT);writeln('num=', num);end.• You can find an copy of this program and the compiled version in the course directory:/pub/courses/csce110/J. Michael MooreAutomatic Output Formatting•Automatic formatting of output–Field width: The computer will insert enough spaces to ______________________________ _______________.–Decimal places: For ____ numbers the data will be displayed in _____________________form.1 These values can be set to any non-negative integer (zero or greater).J. Michael MooreManual Output Formatting•Format:write or writeln (<data>:<Field width for data1>:<Number decimal places for real data1>);•Examples:var num : real;num := 12.34;writeln(num);writeln(num:5:2);1 These values can be set to any non-negative integer (zero or greater).J. Michael MooreFormatting Output•If the field width doesn't match the actual size of the field–Field width too small – extra spaces will be added for ___________variables but not for _______________________________.–Examples:var num : integer;num := 123456;writeln(num:3);writeln('123456':3);J. Michael MooreFormatting Output•If the field width doesn't match the actual size of the field–Field width too large – the data will be _______justified (extra spaces will be put ___________the data).–Examples:var num : integer;num := 123;writeln(num:6);writeln('123':6);J. Michael MooreFormatting Output• If the number of decimal places doesn't match the actual number of decimal places.– Set the ______________________ less than the _____________________________– the number will be ______________________.– Example One:var num : real;num := 123.4567;writeln (num:6:2);J. Michael MooreFormatting Output• If the number of decimal places doesn't match the actual number of decimal places.– Set the ____________________ greater than the _____________________________– the number _____________________________. – Example Two:var num : real;num := 123.4567;writeln(num:12:6);J. Michael MooreInput• The computer program getting information from the user• Done via the read and readln statements• Format:read (<name of variable to store the input>);ORreadln (<name of variable to store the input>);J. Michael MooreInput Exampleprogram inputExampleOne (input, output);beginvar num : integer;write('Enter an integer: ');readln (num); end.Normally Style…J. Michael MooreRead & Readln• Reads each ______ entered and matches it to the corresponding ____________. – e.g., read (num) – If num is an integer then the read statement will try to read an integer value from the user's keyboard input.J. Michael MooreRead Vs. Readln• Read – If the user ______________________ before hitting enter, the _______________________ ____________________.• Readln– Any _______________ before (and including) the enter key ________________________.J. Michael MooreRead: Effect On The Keyboard Bufferprogram getInput (input, output);beginvar num : integer;write('Enter an integer: ');read(num);end.Pascal programKeyboard: user types in 27 and hits enterJ. Michael MooreRead: Effect On The Keyboard Bufferprogram getInput (input, output);beginvar num : integer;write('Enter an integer: ');read(num);end.Pascal program<EOL>172Keyboard controller: determines which keys were pressed and stores the values in the keyboard buffer 1 When the user presses the enter key it is stored as the EOL (end-of-line) marker. The EOL marker signals to the Pascal program that the information has been typed in and it will be processed.Y YNNote: after the read statement has executed the pointer remains at the EOL marker.27RAMnumJ. Michael MooreReadln: Effect On The Keyboard Bufferprogram getInput (input, output);beginvar num : integer;write('Enter an integer: ');readln(num);end.Pascal programKeyboard: user types in 27 and hits enterJ. Michael MooreReadln: Effect On The Keyboard Bufferprogram getInput (input, output);beginvar num : integer;write('Enter an integer: ');readln(num);end.Pascal program<EOL>172Keyboard controller: determines which keys were pressed and stores the values in the keyboard buffer 1 When the user presses the enter key it is stored as the EOL (end-of-line) marker. The EOL marker signals to the Pascal program that the information has been typed in and it will be processed.Y YNNote: Unlike read, the readln will move the pointer past the EOL marker (input buffer is emptied and ready for new input).27RAMnumJ. Michael MooreReadln: Effect On The Keyboard Bufferprogram getInput (input, output);beginvar num : integer;write('Enter an integer: ');readln(num);end.Pascal program<EOL>172Keyboard controller: determines which keys were pressed and stores the values in the keyboard buffer 1 When the user presses the enter key it is stored as the EOL (end-of-line) marker. The EOL marker signals to the Pascal program that the information has been typed in and it will be processed.Note: Unlike read, the readln
View Full Document