-
begin
-
if a = False then
-
WriteLn('a is false')
-
else WriteLn('a is true');
-
end.
Structure:
-
begin
-
if CONDITION then
-
DO_ANYTHING
-
else DO_THIS;
-
end.
-
begin
-
if CONDITION then
-
begin
-
DO_ANYTHING;
-
end
-
else begin
-
DO_THIS;
-
end;
-
end.
-
begin
-
if CONDITION then
-
begin
-
DO_THIS;
-
end;
-
end.
Example:
-
var
-
_Answer: string;
-
begin
-
WriteLn('Do you want to order a pizza?');
-
ReadLn(_Answer);
-
if _Answer = 'Yes' then
-
WriteLn('You decided for yes!')
-
else WriteLn('Don''t want to have a pizza?');
-
end.
'
) or a double quote ("
).
How to write a quote or double quote in a string? It would end the
string in the middle! If you have to write a quote in the text, you can
start and end your string with a double quote or write your quote twice
as it has been done at line 8. Do the same thing for a double quote.The case structure
The Case structure is quite similar to the if structure with the following difference: You can more easily ask for several cases!Structure:
-
case VARIABLE_NAME of
-
VALUE_1:
-
DO_THIS;
-
VALUE_N:
-
DO_THIS
-
else
-
DO_THIS
-
end;
-
end;
Operators
Expanding the condition
You can expand your condition with a few operators:- AND (like
&&
in C): logical 'and':if (a = 1) and (b = 2)
. The value of the expression "(a = 1) and (b = 2)
" is TRUE if a is 1 and b is 2. Else, the value is FALSE and the ELSE-part will be executed (and not the part after THEN). Don't forget the brackets!
- OR (like
||
in C): 'or':if (a = 1) or (b = 1)
. If a is 1, b is 1 or both is 1, the value of the expression is TRUE. - XOR: If only one of the conditions is true:
if (a = 1) xor (b = 2)
. The expression is true if a is 1 or b is 2. If a is 1 AND b is 2, the value will be FALSE! - NOT: The opposite of the expression.
By the way: Every condition returns a boolean value. If it is TRUE, the then-part will be executed. If not, the processor goes to the else-part.
Operators such as 'equals'
Operators such as '=' are:- = - equals
- > - greater than
- < - less than
- <= - less or equals
- >= - greater or equals
- <> - less or greater (but not the same)
Operators for calculating
- You can use ( and ) as brackets.
- / means 'divided by', the result is a float
- div means 'divided by', the result is a rounded integer
- * means 'times'
- + means 'plus'
- - means 'minus'
- string + string : string
- string + char : string
- char + char : string
- char + string : string
- string + number : error
- number + string : error
- number + number : number
- number + char : error
- char + number : error
Loops
Loop means: A block will be executed many times. There are four types of loops:For-to
-
for [var] := [start] to [end] do
-
begin
-
[execute the following code]
-
end;
For-downto
-
for [var] := [start] downto [end] do
-
begin
-
[execute the following code]
-
end;
While-do
-
while [condition] do
-
begin
-
[code]
-
end;
Repeat-until
-
repeat
-
[code]
-
until [condition];
Setting values
The operator for setting values is :=-
a := b;
EXAMPLE:
a equals 1; b equals 3
After executing:
a equals 3; b equals 3
and not:
a equals 1; b equals 1
0 komentar:
Posting Komentar