Inside Delphi 2006 (Wordware Delphi Developers Library)
Now that we have covered the structure of the console application, we can deal with the more tangible realm of input and output.
The WriteLn Statement
To output a piece of data to the console screen, you can use the WriteLn statement.
Listing 2-2: Basic text output
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin WriteLn('Hello from Delphi!'); end.
To test the application, simply press F9 on the keyboard. This is what happens when you run an application:
-
The execution starts at the beginning of the main block.
-
All statements inside the main block are sequentially executed.
-
When all statements finish executing and the end of the main block is reached, the application terminates.
In this case, the console window appears on screen for a very short amount of time and immediately closes. In that time, it successfully executes the WriteLn statement and displays our message to the screen. But, in order to see that it really works, we have to add one more statement to the application: the ReadLn statement.
Although not its primary job, the ReadLn statement can be used to delay the termination of the console application. When used anywhere in a console application, the ReadLn statement defers further execution until the user presses Enter on the keyboard.
Listing 2-3: The updated console application
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin WriteLn('Hello from Delphi!'); ReadLn; end.
If you run the application now, the WriteLn statement displays the "Hello from Delphi!" message and the ReadLn statement keeps the console window visible until you press the Enter key.
To display more than one line, simply use the WriteLn statement again and add the text enclosed in single quotation marks:
Listing 2-4: Displaying several lines of text
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin WriteLn('Hello from Delphi!'); WriteLn('Want to learn more?'); ReadLn; end.
Note | You have to add a semicolon to the end of every statement. Delphi uses semicolons to separate one statement from another. The only time you don't have to add a semicolon to the end of a statement is when it's the only or the last statement in a block. |
Listing 2-5: Exception to the semicolon rule
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin WriteLn('Hello from Delphi!'); WriteLn('Want to learn more?'); ReadLn end.
Omitting the semicolon elsewhere will make Delphi present a cute little compiler error, like the one in Figure 2-4.
Delphi shows compilation errors in the Messages window, which is at the bottom of the Code Editor. The Messages window displays all errors found in the source code, but the Code Editor highlights only the first one.
The Messages window provides you with all necessary information about the error — the name of the unit where the error is located, the line number, and a short description of the error. You can also get a more detailed description of the error by selecting the error in the Messages window and pressing F1 on the keyboard.
Tip | When trying to fix the error, first examine the source code line specified in the Messages window. If you can't find the error in that line, try looking for the error in one of the preceding lines. |
The Write Statement
In addition to the WriteLn statement, Delphi has the Write statement for displaying data in a console application. The only difference between the two statements is that the WriteLn statement writes data in separate lines and the Write statement writes data in the same line.
Listing 2-6: Write and WriteLn text output
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin Write('This is '); Write('the first '); Write('line.'); WriteLn('This is the second line.'); ReadLn; end.
Don't be fooled by the text inside the Write and WriteLn statements. When you run the above application, everything will be displayed in the same line. The reason is that the Write statement always writes text in the same line and the WriteLn statement first writes the user-defined data and then moves the cursor to a new line.
To correct this situation, we have to move the cursor to a new line before we display the second text message. To do that, use the WriteLn statement without any additional data.
Listing 2-7: Displaying an empty line
program Project1; {$APPTYPE CONSOLE} uses SysUtils; begin Write('This is '); Write('the first '); Write('line.'); { Write an empty line. } WriteLn; WriteLn('This is the second line.'); ReadLn; end.