[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
PrologSwitch
See also Prolog and C++ syntax comparison.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
PrologSwitch
(Prolog) Switch statement
Using the cut, !, you can make statements similar to a switch statement in other programming languages (e.g. C++).
% If the user enters a number from 1 to an including 3
% the right clauses are called.
% If you would remove the cuts,
% then the input '2' would results in calling both action(2) and action(_)
%
% Note that you can remove 'nondeterm' from the action's predicate,
% but I left it in, so you do can comment out the cuts
PREDICATES
nondeterm action(integer)
CLAUSES
action(1) :-
!,
nl,
write("You typed one").
action(2) :-
!,
nl,
write("You typed two").
action(3) :-
!,
nl,
write("You typed three").
action(_) :-
!,
nl,
write("You typed an unknown number").
GOAL
write("Type a number from 1 to 3: "),
readInt(Num),
action(Num),
nl.
See also Prolog and C++ syntax comparison.
'switch' links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
