Specificity in If Statements
While skimming over my timeline on Linkedin I stumble across a lot of informitive articles from Free Code Camp. I was reading this article that contained information on how to survive as a software engineer.
On many occasions, I will admit, especially as a junior developer I get intimidated by some of these programming algorithm questions. Of course, as we interview with companies, we dont know what questions will be asked but I found that learning the principles of programming logic helps the most. Just remember that computers are stupid. You must be very specific when speaking to them. In other words, be as detailed and logical as possible.
To better understand programming logic, let’s go house hunting! If you ever been on an apartment/house search, you know how stressful and annoying it may be. You also know that you have wants they no house or apartment will have all of. Nothing is perfect. If you cant find the perfect house, then youll find the next thing that is closest to it, that is also in your budget.
“FizzBuzz” Explained (my way)
In the article, the challange was to print all whole integers from 1–100, and the ones that 3 can go into evenly with “Fizz”, the numbers that 5 can go into evenly with “Buzz”, and for both, “FizzBuzz”.
Sounds easy, and it is. Below, is what I have coded at first, which is incorrect according to what is asked for in the challenge.
I used a for loop with a few if statements. It does run without errors. Some of the conditions were met. Notice for 15, which 3 and 5 can go into evenly gets “Fizz” instead of “FizzBuzz”. That’s because the first condition was already met before checking for the last condition which was the most specific. Look below for one of the few ways to perform this function correctly.
Simply changing the order of conditions by specificity, I was able to get the correct results. Notice how the number 15 now gets “FizzBuzz”.
Computers and humans are different in the making but similar in some ways we process information. We all will like to win $10,000,000 dollars but if were presented with free money of a lower value, we would take it with no doubt. When programming, your computer will see if a variable meets a certain requirement or “if statement” and if it doesn’t, it will check the following condition(s) . Be sure to put your more specifics up top. Hope that helps!