Struggles with Logic and Conditionals
Struggles With Logic and Conditionals
A beginning is a very delicate time. Know then that it is the year 2018, and understanding conditionals is one of the most important aspects of learning to code.
It really does seem easy when someone gives you a real-world example of a conditional. Like, "If the milk is green, don't drink it. Otherwise, drink up!". That makes sense. But somehow in the early days of understanding the logic and the flow of coding, conditionals can get really, really confusing.
One of the toughest parts of understanding conditionals was all the 'opposite ' conditionals. Like, in the milk example, it might make more sense in code speak to say something like, "If the color of the milk equals green, do whatever the inverse of drinking is. Something like !drink would be how it might look. So it can be quite common to have to do all these little bits of logical acrobatics in order to understand why something would evaluate one way or another.
Another example, more in line with something you might actually see deep within the folds of code , would be something like this:
if a <= b && b <= c
puts "do this"
puts "wait, do this"
In this statement, the code is making two evaluations, and if they are both true, the code in the first block will execute. Otherwise, the code in the else block will execute. So, if a is equal to or less than b , AND b is equal to or less than c, then you are free to pass safely within the mines of Moria (random LOTR reference).
For the most part conditionals are that simple. Of course, with all things coding, it can get really complicated, really quickly. Nested conditionals can add extra layers of confusion upon first glance, but usually if you stop and slow down, you'll get the logic of it all.
There are also many ways to write a conditional. I suppose most coders develop habits over time and start writing the conditional statements which are most comfortable to them, and upon seeing new ones can get freaked out. I know, because it's happened to me! Just take your time, go slow, and make sure to google everything you don't understand!