Updated April 15, 2023
Introduction to Prolog if
“prolog if” is a statement to support conditions of the application’s data and its operations. It is a conditional function to display the required condition of the prolog programming language. It is a function to prove the true and false condition and operation of the given data or values using a programming language. It is a conditional statement to assign the condition of the data and display the required statement of the condition using prolog language. It is a Boolean expression to execute the given condition and operate the block of the code available inside of the “IF” statement. It executes the application’s condition and displays the conditional statement available inside of the “IF” function.
Syntax
This function needs some method, objects, and values in the pl files.
Th syntax is shown below.
numbers:- write('enter the numbers here...'), nl,
read(A), nl, read(B), nl,
condition(A,B).
Explanation:
- The “number” is a method to insert numbers for the condition.
- The “A” and “B” are the attributes.
- The “read” method allows for writing the required value in the console.
- The “condition” method contains attributes for the function.
The syntax is shown below.
condition(A,B):- A>B, write('A value is greater than B value').
Or
condition(A,B):- A>B, write('A value is greater than B value'); A<B, write('A value is less than B value').
Explanation:
- The “condition” method contains attributes for the function.
- The “A” and “B” are the attributes for the conditions.
- It writes the condition of the “A” and the “B” attributes.
- It uses conditions such as a “greater than” and the “less then” with the values.
- The “write” method returns the true condition and its statement.
The syntax in the console is shown below.
|?- numbers.
Explanation:
- The prolog console writes the “pl” file method.
- You can enter the values of the attributes.
How does Statement works in Prolog?
Prolog comment works in the “pl” file.
Create a file with the “pl” extension.
Example: main.pl
Insert data into the prolog file.
Save the object with value into a pl file.
The following commands can be used for the data of the list.
- Initialize object and writes the read method of the value.
- The “condition” method interconnects with the condition and values.
number:- write('enter the numbers here...'), nl,
read(A1), nl, read(B1), nl,
conditions(A1,B1).
- The “condition” method contains “number” methods attributes.
- Write the required condition on the variable.
- Use the “write” method to return the required statement.
- Write conditional statement as an output.
conditions(A1,B1):- A1>B1, write('A value is greater than B value').
Prolog comment works in the console.
- Open prolog console or interpreter.
- Set the directory path of the “pl” file.
- Use the given prolog file.
[main].
- You can use the prolog console directly for programming.
- Use the syntax in the prolog console.
| ?- numbers.
- Write the first value of the function.
65.
- Write the second value of the function.
56.
Combine the working procedure of the function.
- Use the “main.pl” file.
numbers:- write('enter the numbers here...'), nl,
read(A1), nl, read(B1), nl,
conditions(A1, B1).
conditions(A1,B1):- A1 > B1, write('A value is greater than B value').
- Use the given console with the required directory path.
[main].
- Use the syntax in the prolog console.
| ?- numbers.
- Write the first value of the function.
65.
- Write the second value of the function.
56.
- The console returns a conditional statement as an output.
Examples of Prolog if
Given below are the examples mentioned:
Example #1
The basic with true condition example and the output shows below.
Use the “main.pl” file.
numbers:- write('enter the numbers here...'), nl,
read(A1), nl, read(B1), nl,
conditions(A1, B1).
conditions(A1,B1):- A1 > B1, write('A value is greater than B value').
Use the given console with the required directory path.
[main].
Use the console.
| ?- numbers.
Write the first value.
65.
Write the second value.
56.
Output:
Explanation:
- The method allows for writing value as input data.
- It fulfills the condition.
- The output displays the conditional statement.
Example #2
The basic with false condition example and the output shown below.
Use the “main.pl” file.
numbers:- write('enter the numbers here...'), nl,
read(A1), nl, read(B1), nl,
conditions(A1, B1).
conditions(A1,B1):- A1 > B1, write('A value is greater than B value').
Use the given console with the required directory path.
[main].
Use the console.
| ?- numbers.
Write the first value.
56.
Write the second value.
65.
Explanation:
- The method allows for writing value as input data.
- The function does not fulfill the condition.
- The method returns the error as an output.
Example #3
The basic with the “equal to” condition example and output shows below.
Use the “main.pl” file.
numbers:- write('enter the numbers here...'), nl,
read(A1), nl, read(B1), nl,
conditions(A1, B1).
conditions(A1,B1):- A1 == B1, write('A value is equal to B value').
Use the given console with the required directory path.
[main].
Use the prolog console.
| ?- numbers.
Write the first value.
66.
Write the second value.
66.
Output:
Explanation:
- The method allows for writing value as input data.
- It function fulfills the condition.
- The output displays the conditional statement.
Conclusion
This function controls the application data using required conditions. It creates elegant, user-friendly, and operative applications. The conditional statement restricts and controls the database of the user applications. This function manages and operates programming data or values as per requirement.
Recommended Articles
We hope that this EDUCBA information on “Prolog if” was beneficial to you. You can view EDUCBA’s recommended articles for more information.