100 likes | 110 Views
Understand how to format output in programming, including integers, reals, strings, and Booleans. Learn about field width, precision, and reading methods like Readln and Read. Master the syntax for writing integers, reals, and strings in programming languages.
E N D
Formatting output: integers • Field: a group of columns in which results are printed is called a field • Field width: The number of columns in the field • writeln(IntId:##) • IntId is an integer identifier or value • ## is the rightmost column to print the integer (Compiler will ignore this value if its smaller than number of digits) • can be written as any integer expression • default is one
Formatting output: reals • Writeln (RealId:#1:#2) • RealId is a real identifier or value • #1 is the rightmost column to print the real • #2 is the number of digits you want on the right of the decimal point. • If #2 is omitted, scientific notation is used
Formatting output: strings • Writeln (stringId:##) • stringId can be a string or character identifier or value • ## is the rightmost column for the string to be printed in
Formatting output: Boolean • Similar to strings • note: Although the compiler is not Cap sensitive, it always prints Boolean values in all CAPS.
More on Readln:numerical values • Note: ENTER key counts sends a carriage return and a line feed to the computer • definition: “white space”: space, tab, carriage return, line feed • white space is used to delimit integers and reals
Read vs. Readln • Readln is like Writeln: After the command is complete, the program’s “input or output pointer” moves to the next line • Writeln: next output placed on the next line • Readln: next input comes from the next line (this means the rest of the input on the original line will be ignored)
Read vs. Readln (con’t) • Read is like Write: After the command is complete, the program’s “input or output pointer” stays on the same line • Write: next output placed immediately to the right of previous output • Read: next input comes from the next (on the right) character that was entered by user (note: input is not always entered by a user)
Reading Characters • No delimiters can be used because white space characters are valid characters • We will skip section 4.6
Reading Strings • Use readln for strings - NEVER use read • readln reads the whole line except CR/LF • If the number of characters in the string is less than the length of the string variable, the entire string is assigned to the variable. • If the number of characters in the string is more than the length of the string variable, the computer will assign as many characters as can fit in the variable beginning with the first character
Reading strings and numbers together • If you need to read both strings and numbers in the same line, you should read the numbers first.