In this blog we are going to be discussing an extension to the if statement called the else if statement. Now, the else if is different than the if else…Which is confusing because the names are so similar. Soon I will go through all of the differences so you can have a clear definition of all three.
In a previous blog I gave you a super crazy if statement to explain how to evaluate complex conditionals. To organize this, we used parenthesis. Let’s go over a simplified example.
In this situation, we do not have parenthesis to explain in what order these are going to be evaluated.
Short circuiting is something that will happen when your program is running and it hits a conditional. A short circuit in logic is when you know for sure that an entire complex conditional is either true or false before you evaluate the whole thing.
You honestly do not have to worry a whole lot about short circuiting when coding because this is something that happens when the software is running, not when you are coding. That being said, it is still an important concept to understand when it comes to logic. There is one specific time when this info is very useful.
This blog we are going to be discussing complex conditionals. First, you let’s do a brief review of conditionals. A conditional is something that evaluates to true or false. Inside of a conditional you can have relational operators. Let’s say we have a variable, x, which has the value 5. We can make a conditional like this:
So far in in this series we’ve discussed the very basics of logic. In the very first blog over logic, I briefly introduced a topic called logical operators but I never went into a whole lot of detail. These allow us to make more complex conditionals, and that is what this entire blog is going to be about.
In the previous blog we had this code:
The problem here is that we are calling return 0 twice. This works in this situation, but what if we wanted to continue executing the program. For example, what if we wanted to do something like this:
In this blog we are going to be creating an if statement with relational operators.
We want to create an application that gives the user one try to guess a number between 1 and 5. As we get more advanced, we can do things such as give them multiple tries and see how many times it takes them to get it right. For now, we are just getting the very basics.
In the previous videos we have worked with if statements that work with strictly true or false values. We’ve used both bool variables and we’ve also replaced variables with actual values and learned that any number that is not zero is considered to be true.
What if we want to change the way our if statements work and instead of consider anything not zero to be false, we want to pick our own rules with what is true and false? This is where relational operators come in.
This video is going to be a very short and to the point video that explains some good habits that you should have when you are starting out programming.
Let me tell you, a lot of people hate programming. I believe one of the reasons for this is because they don’t follow what I am going to teach you in this video.
In the last blog I spoke of how if statements work. In this blog we are going to write a very simple if statement with a bool variable! Remember that we have to have #include <stdbool.h> in our code. Are you new here? Start at the beginning, Intro to C!
This works and all and you can see that it adds a layer of functionality to our program, but this really isn’t that cool. You can see that we are selves are assigning true to the bool variable, so we wouldn’t really even need this conditional. We know it’s going to run, so we could just print what we want to print and delete the conditional.