if-sætning med flere kommandoer

Tags:    delphi

Jeg er ved at lave en simpel lommeregner. Når man trykker på en operator (f.eks. +), vil jeg gerne have, at mit editfelt (edtTal) ryddes/cleares og værdien i feltet gemmes i variablen T. Såfremt editfeltet er tomt, beder jeg brugeren indtaste et tal vha. ShowMessage.

Mit problem er, at jeg skal udføre to kommandoer i if-sætningen:

<pre>var T : string;
begin
if edtTal.Text <> '' then
T := edtTal.Text;
edtTal.Text := ''
else ShowMessage('Indtast venligst et tal.');
end;
</pre>

Jeg får fejlen END expected but ELSE found i Delphi.

Hvordan angiver jeg flere sætninger/kommandorer, som skal udføres under if-sætningen?

Håber I kan hjælpe.



3 svar postet i denne tråd vises herunder
1 indlæg har modtaget i alt 1 karma
Sorter efter stemmer Sorter efter dato
Prøv med denne her:

Fold kodeboks ind/udKode 


Når du vil lave en if sætning med flere kommandoer skal du gør det sådan her:
if ... then begin

end

Du skal bruger "begin" og "end" imellem kommandoerne!
Håber du kan bruge det!

MVH.
Armen L.A.

[Redigeret d. 02/08-03 19:12:17 af Armen L.A.]



Her er alt hvad Delphi6's hjæpefil har at sige om if-statemtnts:

----
There are two forms of if statement: if...then and the if...then...else. The syntax of an if...then statement is

if expression then statement

where expression returns a Boolean value. If expression is True, then statement is executed; otherwise it is not. For example,

if J <> 0 then Result := I/J;

The syntax of an if...then...else statement is

if expression then statement1 else statement2

where expression returns a Boolean value. If expression is True, then statement1 is executed; otherwise statement2 is executed. For example,

if J = 0 then

Exit
else
Result := I/J;

The then and else clauses contain one statement each, but it can be a structured statement. For example,

if J <> 0 then

begin
Result := I/J;
Count := Count + 1;
end
else if Count = Last then
Done := True
else
Exit;

Notice that there is never a semicolon between the then clause and the word else. You can place a semicolon after an entire if statement to separate it from the next statement in its block, but the then and else clauses require nothing more than a space or carriage return between them. Placing a semicolon immediately before else (in an if statement) is a common programming error.
A special difficulty arises in connection with nested if statements. The problem arises because some if statements have else clauses while others do not, but the syntax for the two kinds of statement is otherwise the same. In a series of nested conditionals where there are fewer else clauses than if statements, it may not seem clear which else clauses are bound to which ifs. Consider a statement of the form

if expression1 then if expression2 then statement1 else statement2;

There would appear to be two ways to parse this:

if expression1 then [ if expression2 then statement1 else statement2 ];
if expression1 then [ if expression2 then statement1 ] else statement2;

The compiler always parses in the first way. That is, in real code, the statement

if ... { expression1 } then

if ... { expression2 } then
... { statement1 }
else
... { statement2 } ;

is equivalent to

if ... { expression1 } then

begin
if ... { expression2 } then
... { statement1 }
else
... { statement2 }
end;

The rule is that nested conditionals are parsed starting from the innermost conditional, with each else bound to the nearest available if on its left. To force the compiler to read our example in the second way, you would have to write it explicitly as

if ... { expression1 } then

begin
if ... { expression2 } then
... { statement1 }
end
else
... { statement2 } ;
----

Mvh,

Thomas Nielsen



Jeg takker for begge indlæg. Fandt imidlertid selv ud af problemet ved at kikke på andre kodeeksempler. Mvh. Henrik Andersen



t