Basic Output


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

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; begin   WriteLn('Hello from Delphi!'); end.
image from book

To test the application, simply press F9 on the keyboard. This is what happens when you run an application:

  1. The execution starts at the beginning of the main block.

  2. All statements inside the main block are sequentially executed.

  3. 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

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; begin   WriteLn('Hello from Delphi!');   ReadLn; end.
image from book

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.

image from book
Figure 2-3: The updated console application

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

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; begin   WriteLn('Hello from Delphi!');   WriteLn('Want to learn more?');   ReadLn; end.
image from book

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

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; begin   WriteLn('Hello from Delphi!');   WriteLn('Want to learn more?');   ReadLn end.
image from book

Omitting the semicolon elsewhere will make Delphi present a cute little compiler error, like the one in Figure 2-4.

image from book
Figure 2-4: A compiler error

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

image from book
program Project1; {$APPTYPE CONSOLE} uses   SysUtils; begin   Write('This is ');   Write('the first ');   Write('line.');   WriteLn('This is the second line.');   ReadLn;  end.
image from book

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.

image from book
Figure 2-5: Output of the Write statement

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

image from book
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.
image from book



Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net