Delphi Programming/Variables and data types

From Wikibooks, open books for an open world
< Delphi Programming
Jump to: navigation, search

Contents

[edit] Strings and chars

You have already worked with strings in the last chapter, such as 'Hello World' or 'Hi there' and so one. A string is a sequence of chars. A char (a character) is something like '#', 'A', '2', ';', '.', '?' and so on. Each char has got a number in a library of chars, the ASCII. Variables are like containers with different types of their contents. You must tell Delphi the type of the container before using it! Here the structure:

 program Project1
 
 {$APPTYPE CONSOLE}
 
 uses
   SysUtils;
 
 var
   var1: type;
   var2: type;
 
 begin
   ...
 end.

This could mean:

 var
   text1: string;
   character1: char;

And then write between begin and end:

 character1 := 'H';
 text1 := character1 + 'ello World!';

With the structure:

 A := B;

A gets the value of B. With '+' you can do two things: add numbers or link texts. Here you link the character 'H' with the text 'ello World!'. The result: 'Hello World!'.

Now you can write:

 WriteLn(text1);
 ReadLn;

With ReadLn the application waits for the user to enter something and then pressing 'Return'. But here you can reach that the application will wait for the user to press Return and thenb will terminate.

Two examples:

 var
   _Input: string;
   _Linkage: string;
 begin
   WriteLn('Hello, what is your name?');
   ReadLn(_Input);
   _Linkage := 'Hello, ' + _Input;
   WriteLn(_Linkage);
 end.

You can also write:

 _Input, _Linkage: string;

ReadLn(_Input) means that the application waits for the user to enter a text and then saves the text in the variable _Input. By the way: Don't forget the semicolon at the end of each command! And: It's not important if you write BEGIN, BeGiN or begin, or if you write _INPUT, _InPuT or _input.

Example two: Get the number of a char in the ASCII.

 var
   number: Integer; // declare number as an integer
   _Input: string;
   _Char1: char;
 begin
   WriteLn('Enter a char!');
   ReadLn(_Input);
   _Char1 := _Input[1];
   number := Ord(_Char1);
   WriteLn('ASCII is: ', number);
 end.

With _Input[1] you get the first char of _Input. And the first char is char 1, not char 0 (like in C++)!

[edit] Integers

Integers are even integers, you write:

 a: Integer;

[edit] Floats

Floats are decimal numbers, you can write:

 c: Real;   // Real is normally the same like Single
 a: Single;
 b: Double;
 d: Extended;

An Extended is more exactly then a Real, but needs more main storage.

[edit] Arrays

Arrays are 'tables' of strings, floats, integers or any data type. But the table can also have a third dimension, or a fourth, are can only have rows (but no columns). You say:

 a: array [from..to] of data_type;
 → a: array [1..3] of string;

for one dimension. For two:

 a: array [fX..tX] of
    array [fY..tY] of data_type;

and so on. A single element of an array is for example:

 a[1] := 'Hello '
 a[2] := 'World'
 a[3] := '!';

And then:

 WriteLn(a[1],
         a[2],
         a[3]);

[edit] Boolean

Boolean is a logical value. It can be 'True' or 'False'. Write:

 var
   a: Boolean;
 begin
   a := True;
   // or
   a := False;

But there is only a sense of Boolean if you know the operators, so jump to the next chapter.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export