[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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



last edited (February 7, 2007) by bilderbikkel, Number of views: 1026, Current Rev: 3 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.