Difference between revisions of "Language Reference/Terms/If-then-else"

From wiki.visual-prolog.com
m (Example Template)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Conditionally executes a group of statements.
if-then-else can be used both as a statement and as an expression.
 
==== if-then-else (statement) ====
 
The if-then-eslse statement conditionally executes a group of statements.


<vipbnf><IfThenElseTerm>:
<vipbnf><IfThenElseTerm>:
Line 22: Line 26:
<vip>if Cond then T1 else T2 end if</vip>
<vip>if Cond then T1 else T2 end if</vip>


First <vp>Cond</vp> is evaluated, if it succeeds then <vp>T1</vp> is evaluated otherwise <vp>T2</vp> is evaluated. <vp>Cond</vp> is not allowed to leave backtrack points; that is, it cannot be <vp>multi</vp> or <vp>nondeterm</vp>. <vp>Cond</vp> is a cut-scope (see {{lang2|Terms|Cut_Scopes|Cut Scopes}}).
First <vp>Cond</vp> is evaluated, if it succeeds then <vp>T1</vp> is evaluated otherwise <vp>T2</vp> is evaluated.  
 
<vp>Cond</vp> is followed by an implicit cut, which turns:
* a <vp>nondeterm</vp> condition into a <vp>determ</vp> condition and
* a <vp>multi</vp> condition into a <vp>procedure</vp>.
 
<vp>Cond</vp> is a cut-scope (see {{lang2|Terms|Cut_Scopes|Cut Scopes}}).


{{Example|
{{Example|
<vip>clauses
<vip>clauses
  w(X) :-
    w(X) :-
      if X div 2 = 0 and X > 3 then  
        if X div 2 = 0 and X > 3 then  
          write("X is good")
            write("X is good")
      else
        else
          write("X is bad"),
            write("X is bad"),
          nl
            nl
      end if,
        end if,
      nl.</vip>
        nl.</vip>


There are several things to notice about this example:
There are several things to notice about this example:
Line 51: Line 61:
<vipbnf>if <Cond> then <Term> else succeed end if</vipbnf>
<vipbnf>if <Cond> then <Term> else succeed end if</vipbnf>


<noinclude>[[Category:Language Reference Subarticle]]</noinclude>
==== if-then-else (expression) ====
 
The if-then-else expression conditionally evaluates expressions.
 
Syntactically it is same as the if-then-else statement, but and the terms in the branches must be expressions and the entire if-then-else expression will itself evaluate to a value.
 
The shorthand writings that leave out the else-part does not make sense for the expression.
 
{{Example|
<vip>clauses
    w(X, Y) :-
        Min = if X < Y then X else Y end if,
        writef("The minimum is : %\n", Min).
</vip>
The if-then-else expression above evaluates to <vp>X</vp> if <vp>X</vp> is less than <vp>Y</vp> else it evaluates to <vp>Y</vp>.  <vp>Min</vp> is bound to the resulting value.}}
 
<noinclude>{{LanguageReferenceSubarticle|Terms/If-then-else}}</noinclude>

Latest revision as of 15:39, 28 March 2015

if-then-else can be used both as a statement and as an expression.

if-then-else (statement)

The if-then-eslse statement conditionally executes a group of statements.

IfThenElseTerm:
    if Condition then Term Elseif-list-opt Else-opt end if
 
Elseif:
    elseif Condition then Term
 
Else:
    else Term

The following two terms are equivalents.

if Cond1 then T1 elseif Cond2 then T2 else T3 end if
if Cond1 then T1 else
  if Cond2 then T2 else T3 end if
end if

Consider the schematic if then else term:

if Cond then T1 else T2 end if

First Cond is evaluated, if it succeeds then T1 is evaluated otherwise T2 is evaluated.

Cond is followed by an implicit cut, which turns:

  • a nondeterm condition into a determ condition and
  • a multi condition into a procedure.

Cond is a cut-scope (see Cut Scopes).

Example
clauses
    w(X) :-
        if X div 2 = 0 and X > 3 then 
            write("X is good")
        else
            write("X is bad"),
            nl
        end if,
        nl.

There are several things to notice about this example:

  • You can use "and" and "or" logical operators and other "complex" terms in all three sub-terms.
  • There is no comma before the keywords "then", "elseif", "else", and "end if".

For readability sake, we always recommend using "or" instead of ";". Likewise we also recommend using "and" (instead of ",") when it (as in the condition above) represents a logical "condition" rather than a "sequentation".

Leaving out the else-part is just shorthand for writing that else succeed, i.e.

if Cond then Term end if

is short-hand for

if Cond then Term else succeed end if

if-then-else (expression)

The if-then-else expression conditionally evaluates expressions.

Syntactically it is same as the if-then-else statement, but and the terms in the branches must be expressions and the entire if-then-else expression will itself evaluate to a value.

The shorthand writings that leave out the else-part does not make sense for the expression.

Example
clauses
    w(X, Y) :-
        Min = if X < Y then X else Y end if,
        writef("The minimum is : %\n", Min).
The if-then-else expression above evaluates to X if X is less than Y else it evaluates to Y. Min is bound to the resulting value.