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

From wiki.visual-prolog.com
m (Categorized)
m (Example Template)
Line 24: Line 24:
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 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}}).


'''Example'''
{{Example|
 
<vip>clauses
<vip>clauses
   w(X) :-
   w(X) :-
Line 40: Line 39:
*You can use "<vp>and</vp>" and "<vp>or</vp>" logical operators and other "complex" terms in all three sub-terms.
*You can use "<vp>and</vp>" and "<vp>or</vp>" logical operators and other "complex" terms in all three sub-terms.
*There is no comma before the keywords "<vp>then</vp>", "<vp>elseif</vp>", "<vp>else</vp>", and "<vp>end if</vp>".
*There is no comma before the keywords "<vp>then</vp>", "<vp>elseif</vp>", "<vp>else</vp>", and "<vp>end if</vp>".
}}


For readability sake, we always recommend using "<vp>or</vp>" instead of "<vp>;</vp>". Likewise we also recommend using "<vp>and</vp>" (instead of "<vp>,</vp>") when it (as in the condition above) represents a logical "condition" rather than a "sequentation".
For readability sake, we always recommend using "<vp>or</vp>" instead of "<vp>;</vp>". Likewise we also recommend using "<vp>and</vp>" (instead of "<vp>,</vp>") when it (as in the condition above) represents a logical "condition" rather than a "sequentation".
Line 45: Line 45:
Leaving out the <vp>else</vp>-part is just shorthand for writing that <vp>else</vp> succeed, i.e.
Leaving out the <vp>else</vp>-part is just shorthand for writing that <vp>else</vp> succeed, i.e.


<vip>if Cond then T1 end if</vip>
<vipbnf>if <Cond> then <Term> end if</vipbnf>


is short-hand for
is short-hand for


<vip>if Cond then T1 else succeed end if</vip>
<vipbnf>if <Cond> then <Term> else succeed end if</vipbnf>


<noinclude>[[Category:Language Reference Subarticle]]</noinclude>
<noinclude>[[Category:Language Reference Subarticle]]</noinclude>

Revision as of 10:48, 5 November 2008

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 not allowed to leave backtrack points; that is, it cannot be multi or nondeterm. 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