The primary architecture of all software programs to date. If-then-else is a high-level programming 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"
The Then Is Implied
In many high-level languages, the THEN is implied, and all statements between IF and ELSE are carried out if the condition is true . In the following example, 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");
}
Assembly Language
In assembly language, which is the programming language closer to the computer's machine language, a branch instruction (or jump instruction) follows the compare instruction. High-level languages are translated into low-level assembly language and the latter is converted into machine language, which is the only language a classical computer executes.
The following assembly language example is in pseudocode, which is a native language explanation of assembly instructions. Comprising shorthand and sometimes extremely cryptic commands, assembly languages differ from hardware to hardware. See
assembly language and
classical computer.
Pseudocode Assembly Language (low level language)
compare A,B
branch on equal to print-yes-routine
goto print-no-routine