Professional Documents
Culture Documents
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
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
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
7
Control Statements
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”).