A high-level programming language statement that compares two sets of data in memory and tests the results. If the results are true, the THEN instructions are taken; if not, the ELSE instructions are taken. The execution of the compare instruction sets an indicator (equal, high, low) that the next instruction can test.
The If is the first part of the statement, which might include multiple compares, in which case all conditions must be met. For example, both compares must be true in "If a equals b and a is greater than d."
Calculate, Compare and Copy
Comparing is one of the three basic functions all computers perform. A computer can look at two sets of data in memory and determine if they are equal in value or if one set is higher or lower in numeric or alphabetic sequence.
A Binary Compare
Because all data (alphabetic letters and numeric digits) are converted to binary when entered into the computer, every comparison is actually made between two binary numbers. See
3 C's,
binary and
bit.
BASIC Language (high-level language)
10 if answer = "y" then print "Yes"
20 else print "No"
In many languages, THEN is implied. All statements between IF and ELSE are carried out if the condition is true. All statements between ELSE and the end of the statement are carried out if not true. The following C/C++ example produces the same results as the BASIC example.
C/C++ Language (high-level language)
if (answer =='y')
{
printf ("Yes\n");
else
printf ("No\n");
}
In a low-level programming language, which is much closer to the computer's machine language, a branch instruction follows the compare instruction. See
assembly language.
Pseudo Assembly Language (low level language)
compare A,B
branch on equal to equal-routine
branch on greater than to high-routine
goto low-routine