You are on page 1of 9

University of Salahaddin-Hawler

College of Engineering
Software Engineering

PROLOG
Lecture 3
writing and reading

 Write predicate
write(term) :Writes a single term to the terminal. For
example: write(a), or write(“How are you?”)or
write(“a”,”b”,”c”,2).
 nl: newline.
 Comment : % or /* */ .
 Read Predicates:
readln( Variable) read a whole line .
readchar(Variable) read a single character.
readint(Variable) read integers.
readreal(Variable) read floating point numbers.
readterm(Variable) read any term, including compound objects
2
writing and reading Example

% Special compiler directive; ignore for the moment

predicates
nondeterm run.

clauses
run:-
write("************** Hello World Program
**************"),
nl,
write("Hello World (first)"),
nl,
readint(_).

goal
run. /* This is a comment. */ 3
writing and reading Lab work

define your own version of writeln(write


followed immediately by new line ) as a rule,
then use it in the previous example.

4
Mathematical Operations

 Arithmetic operations.
Visual Prolog can perform all four basic
arithmetic operations (addition, subtraction,
multiplication, and division) between
integral and real values.

Z=X+Y.

5
Mathematical Operations

Relational Operators
Symbol Relation
< less than
<= less than or equal to
= equal
> greater than
>= greater than or equal to
<> or >< not equal

6
Mathematical Operations

Visual Prolog built-in Arithmetic Predicates and


Functions

7
Control Statements

If-else structure If-else structure (in prolog)

test(X):-
If(X%2==0) Print X is even X mod 2=0,
Else Print X is odd write(X,“is even”).

test(X):-
X mod 2<>0,
write(X,“is odd”).

For loop For loop(in prolog)


loop(0). stop condition
For(int i=5;i>0;i--)
loop(X):-
X>0,
Xx=X-1, decrement
Initial value stop condition decrement
loop(Xx).
8
The End

You might also like