Dale Stubbart

The Deep Path Blog

Come with me on the Deep Path, the one which has substance. Join me in Deep Listening, Deep Consulting, Deep Solutions, and Deep Conversations. Let&';s go deep where the answers abound. Let&';s get answers for a lifetime.

We want to listen to the entire question. Then we want to respond with the entire answer. These answers will not only solve problems in the here and now, they will solve them for many generations. Let&';s have fun while we&';re at it.

Let&';s make this as easy as it wants to be. , and at . Let&';s find the solution!, even if you&';re stuck.





Hi there, Aloha, Namaste, Salaam, Shalom, Om Shanti, Avexeni, jicMaylc. I&';m a Consultant specializing in breakthrough problem solving. I&';m the Author of 100+ books, and an Electric Car Concierge. I combine deep listening with gentle power.
I like helping people. I prefer to help people who are helping others and the earth. Contact me. Let&';s see if we can work together to bring about a better world. Often that better world starts with you.

The Deep Path Blog Directory

Three Way Ifs and Three Way Operators

Advantages and disadvantages of using three way ifs and three way operators.


I noticed some Javascript code that I didn&';t recognize the other day.
x=(y==0)?5:7

This statement says if y is 0 then x is 5; else x is 7. In Javascript, this could also be written as
if(y==0){x=5}else{x=7}

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 statements are available in most programming languages (all but one that I know of). And, they looks very similar to the second form. However, in many programming languages an if statement is much more readable than in Javascript. Hi5ive is the simplistic programming language, because I designed it. Too bad it only exists in a book. In Hi5ive, the if statement is
If Y=0 Then
Move 5 To X EndMove
Else
Move 7 To X EndMove
EndIf

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 switch or 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 switch or 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?




Please Share


Categories
Programming

Programming
Subscribe to my Blog
(Requires a Feed Reader)
▼ ▼ ▼ ▼ ▼