<p>This is the idiomatic way of writing a (nested) if-then-else in Prolog:</p>

<pre><code>testif(X,Y) :-
    (X &gt;= 6 -&gt;
        writeln('X&gt; 6'),
        (Y&gt;= 3 -&gt;
            writeln('X &gt;6 and Y&gt; 3')
        ;
            writeln('X &gt;6 and Y&lt;3'),
        )
    ;
        writeln('X&lt;6');
    ),
    writeln('Test over').
</code></pre>

<p>As in any other language, indentation counts in Prolog. See <a href="http://arxiv.org/abs/0911.2899" rel="nofollow">Covington <em>et al.</em></a> for a styleguide.</p>
How to use nested if
<p>for example:</p>

<pre><code>testif(X,Y) :-
    X &gt;= 6 -&gt;
        writeln('X&gt; 6'),
        Y&gt;= 3 -&gt;
            writeln('X &gt;6 and Y&gt; 3');
        writeln('X &gt;6 and Y&lt;3'),        

    writeln('X&lt;6');
    writeln('Test over').

test(X,Y):-
    testif(X,Y).


?- test(7, 3).
Yes (0.00s cpu)
X&gt; 6
X &gt;6 and Y&gt; 3
</code></pre>

<p>why don't output 'Test over' ?</p>

<pre><code>?- test(4, 3).
Yes (0.00s cpu, solution 1, maybe more)
X &gt;6 and Y&lt;3    % why output this line?
X&lt;6
</code></pre>

<p>why don't output 'Test over' too?</p>

<p>Thanks very much :)</p>