
Program % If-Then-Else statementgt(X,Y) :- X >= Y,write('X is greater or equal').gt(X,Y) :- X Y,write('X is greater').gte(X,Y) :- X =:= Y,write('X and Y are same').gte(X,Y) :- X < Y,write('X is smaller'). Following is an example of decision making in Prolog. In some different programming languages, there are If-Else statements, but in Prolog we have to define our statements in some other manner. The basic usage is as follows − If is true, Then, Else So when we try to match some condition, and perform some task, then we use the decision making statements. The decision statements are If-Then-Else statements. compiling D:/TP Prolog/Sample_Codes/ for byte code.D:/TP Prolog/Sample_Codes/ compiled, 14 lines read - 1700 bytes written, 16 msyes| ?- count_down(12,17).5true ? 4true ? 3true ? 2true ? 1true ? 0yes| ?- count_up(5,12).10true ? 11true ? 12true ? 13true ? 14true ? 15true ? 16true ? 17yes| ?- Decision Making count_up(L, H) :- between(L, H, Y), Z is L + Y, write(Z), nl.

Let us see an example program − count_down(L, H) :- between(L, H, Y), Z is H - Y, write(Z), nl. So, we can use the between() to simulate loops. Now create a loop that takes lowest and highest values. compiling D:/TP Prolog/Sample_Codes/ for byte code.D:/TP Prolog/Sample_Codes/ compiled, 4 lines read - 751 bytes written, 16 ms(16 ms) yes| ?- count_to_10(3).345678910true ?yes| ?.

There are no direct loops in some other languages, but we can simulate loops with few different techniques. In general, for, while, do-while are loop constructs in programming languages (like Java, C, C++).Ĭode block is executed multiple times using recursive predicate logic. Loop statements are used to execute the code block multiple times.


In this chapter, we will discuss loops and decision making in Prolog.
