In c language i have something like :
if(cond1)
{}
else if(cond2)
{}
else
{}
how is this possible in Prolog?
“prolog if” is a statement to support conditions of the application's data and its operations. It is a conditional function to display the required condition of the prolog programming language. It is a function to prove the true and false condition and operation of the given data or values using a programming language.
% If-Then-Else statement gt(X,Y) :- X >= Y,write('X is greater or equal'). gt(X,Y) :- X < Y,write('X is smaller'). % If-Elif-Else statement gte(X,Y) :- X > Y,write('X is greater'). gte(X,Y) :- X =:= Y,write('X and Y are same').
The arrow in Prolog does not correspond to material implication in first-order logic. It's a ternary "if-then-else" operator with an optional alternative. Because of the way it's implemented in Prolog syntax, (true -> false) ; true.
Performing an "or" in Prolog can also be done with the "disjunct" operator or semi-colon: registered(X, Y) :- X = ct101; X = ct102; X = ct103. Save this answer.
( If1 -> Then1
; If2 -> Then2
; ...
; otherwise
).
Note that if-then-else is only necessary if you cannot express the different conditions by pattern matching in different clauses. Everything that can be expressed by pattern matching should be expressed by pattern matching, as this typically leads to more general and also more efficient code.
(cond1 ->
consequent1
; cond2 ->
consequent2
;
alternative
)
For the record, this is called a conditional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With