Three Way Ifs and Three Way Operators
Advantages and disadvantages of using three way ifs and three way operators.
This statement says
The first version uses a three way operator
? to separate the if from the true and false results. The
() are optional.
x is the variable we&';re setting.
(y==0)? is the condition (a question, thus the question mark). When the condition is true, the result is 5. When the condition is false, the result is 7. The
: separates true from false.
In the second version, the
() are required. True and False equations are enclosed in
If Y=0 Then
Move 5 To X EndMove
Move 7 To X EndMove
As you may have noticed, there are no shortcuts in Hi5ive.
Now, let&';s get to the three-way if. If statements can return one of any number of results. The first time I encountered a three way if was in 1977. I was learning to program RPG. In that language, an if statement could have one, two, or three results. This made it a three way if. In RPG, one result was assigned if the condition was less than 0. One result was assigned when the condition was equal to 0, and one result was when the condition was greater than 0.
Most programming languages have special statements like
case that will handle if statements that test multiple conditions. These can all be written as if statements. And I often do just that.
Let&';s take a look at what&';s happening behind the scenes with an if statement. Your if statement is turned into something that the hardware understands. When you write
case, the hardware uses special processors to make those decisions faster. I suspect that these days, that&';s also true if you write write these as if statements. If you&';re dealing with very large volumes of data or real-time programming, that speed might make a difference. Only once did it make a difference in one of my programs. In that case, I had a statement like
if i='a' or i='b' or i='c' .... I had placed the conditions in alphabetical order (easier to read). I had to switch that statement around, so that the condition which was most likely to happen was processed first.
In the old days, the hardware would test the electrical current to determine true or false. Computers run on electricity, and the electricity was used to make this determination. One volt was true, anything less than that was false. An older programmer once told me how their computers were not calculating things correctly. He finally got the electricity cleaned up, and then things worked fine. With an if statement, there were only ever two conditions for the electricity. True or False. Special hardware or processing was added to handle three conditions. And, as far as I know, that&';s been the solution ever since. (I could be wrong).
There are only two electrical states because computers are binary machines. Deep down inside the computer there are only two values, zero and one. But, what if there were a quadratic machine. This one would have four values, zero thru three. Rather than one volt representing the value one and anything less than that representing zero, one volt could be divided into thirds. Anything near zero would be zero. Electricity around one-third volt would be one. Two-thirds would be two, and one volt would be three. I suspect the voltage in computers isn&';t that high these days, but maybe something similar would work. Also, a quadratic computer would be capable of storing more data in a smaller space.
Now, back to the three way operator that started off this blog. I thought about using it to make my code smaller. However, it only applies to certain if statements. It doesn&';t apply to most of mine. Also, I&';m used to reading if statements the other way. If statements are written as
if ... else ... in most computer languages. And since I program in several, it&';s easier if I use the standard notation.
But, perhaps a three way operator has its place. Maybe it uses something special to make it process faster. If it does, that could also be applied to regular if statements. That could be applied, so that we could leave the code more readable and easier to maintain. If we want to keep the three way operator, why not invent a four way operator or maybe a seven way one?
One last thought, then it&';s your turn to reply. When I solve problems, I use a dozen different methods all at the same time. If you&';re interested in exploring those further, they&';re detailed in my book Solutions Galore. It works for me, why not for a computer? This would require some parallel processing. Computers have had parallel processors for along time now. Before that, there was the night shift. It takes quite a bit of reworking to set up a program to use those parallel processors. It also takes some design time. A computer can take an if statement with many conditions and pass pieces of it to separate processors. However, the if statement, as it is, evaluates conditions in a certain order. When we assigning different values depending on if something is true or false, the order doesn&';t matter. At other times it does. How do we tell the computer when it matters? Some routines can be processed at the same time. Most, need to be processed in the logical order that they&';re in in the program.
Mostly parallel processing only affects three things. One: People working on different computers on the same network, possibly running the same program. Two: Jobs which run side by side, rather than consecutively. Three: Processing large amounts of data. In this case, the data that&';s going to be processed is logically grouped into chunks. Each chunk is run by the process in a different thread. Sometimes this method is helpful. Sometimes is doesn&';t matter. Sometimes it overloads the machine.
OK. I&';ve had my say. What do you think?