What would life be like without choices?
In the world of object-oriented programming, for the life cycle of a program, it would be a rather difficult one. Creating maintainable systems would be extremely challenging and time consuming and our code would be rather rigid, making application systems very difficult to scale.
Thankfully, in object-oriented-programming (OOP) we can use conditional statements to add flexibility to our code. Conditional statements are like choices, or questions within code. They are functions that are used by the data objects being manipulated or passed around.
There are several types of conditional statements; if, if-else, switch, While, Do-While are the most common. Of those, if and if-else statements are by far the most used. Their simple construct makes them the perfect “question” to ask any object. If and If-else statements present the data with a question to do this or that and then make a decision on how to be manipulated based on the answer.
This or That
To better explain, we can compare the differences between if and if-else statements. First, the If() statement. It’s the most basic, lowest level, and simplest question to ask a data object about how it wants to be treated.
In essence, an if statement asks whether not you want to do THIS and based on the answer either something happens to alter the state of the object, or nothing happens at all. It’s like asking a friend, “Are you hungry?” and if the friend says “I’m about to starve,” or a simple “Yes,” then you go to get something to eat. If your friend says “No,” or “Not right now,” then you don’t do anything.
If statements use a boolean expression to test a condition along with a block of code or statements within the function body. The block of code within the body is executed, or not, based on the result of the boolean expression.
Each programming language uses it’s own syntax, but the basic construct of an if statement is the same for most all programming languages. Here is an example in psuedocode:
if (boolean expression that equates to being true) { //then do something }
This, That, or The Other
An if-else statement is a bit different; if includes an else clause that offers another option to treat the data object(s). So let’s say you asked your friend a slightly different question like, “Where you like to eat a taco, burger, or pizza?” Based on their answer, you would go to Taco Bell, Burger King or Pizza Hut, respectively.
If else statements take the general form like below:
if (boolean expression that equates to being true) { //then do something } else { //then do something }
They can include more than one else clause also, creating an if-else chain. if-else chains are great to use when there are more than two ways to treat the data objects. If-else chains go by different names among the different programming languages, but they behave the same.
if (boolean expression that equates to being true) { //then do something } else (boolean expression that equates to being true) { //then do this } else (boolean expression that equates to being true) { //then do this }
Let’s Get Complex
If and if-else statements can be very simple, as we have discussed so far, and they can also get pretty complex. We can nest statements, and we can also use conditional keywords AND, OR and NOT to add multiple clauses that the statement must check for.
Nesting statements involves using an if or if-else statement as an if or else clause. Nesting is similar to if-else chains, in that multiple clauses exist in one statement, however Nesting allows a set of conditions to be tested based on a super-set of conditions. There isn’t a limit to nesting levels, but you should always keep in mind code maintainability and code brevity when nesting if and if-else statements.
if (boolean expression that equates to being true) { //then test another condition if (boolean expression that equates to being true) { //now you can do something } } ...
AND, OR, and NOT keywords allow us to test multiple conditions within the same clause, so a bit different from nesting. Each of these keywords require all, some, or none of the condition to evaluate to true before the statements are executed. Programming languages use their own syntax; some use the words “AND,” “OR,” and “NOT” others use symbols such as &&, ||, and <>, but the construct and behavior is the same for each keyword for each language.
AND requires ALL of the boolean expressions to evaluate to true.
if(boolean expression 1 = true AND boolean expression 2 = true) { //then do something }
OR requires SOME of the expressions to evaluate to true.
if(boolean expression 1 = true OR boolean expresion 2 = true) { //then do something }
NOT requires NONE of the expressions to evaluate to true.
if(boolean expression 1 = true NOR DOES boolean expression 2 = true) { //then do something }
Summary
If-statements are the simplest forms of controlling the behavior of data objects, using boolean expressions to determine how objects are treated. If-statements brings flexibility to our programming code, allowing applications to be much more enjoyable to use and easier to maintain.