Computer Programming Tips : Nesting If Statements

Aaron Tadeo
Computer programming isn't as complicated as it looks. It really depends on what the purpose of your program is. If you have a simple program that outputs data with a linear presentation, meaning you don't need conditions or encounter lots of situations, it'll be very easy to code. However, if you have lots of conditions within the program let's say you need to do something when a condition is met, here's what should happen. If not, that should happen. And so on.

One of the most common statements or keywords used is the "if" statement. It usually has this syntax: "If = true then (do this) else (do that)" Here's an example so that you can clearly see how it functions.

If C = true then
print A
else
print B
end if

In this program snippet, if C is true then it will print A. If not, it will print B. This is simple and common. However, what if you need to test several values and display different outputs for each. It'll be troublesome especially if you have lots of values to test. A nesting "if" consists of "if" statements within several "if" statements.

You'll have a hard time debugging the program if the output isn't what you intended. It may be difficult to look at where the problem is. But what if someone else reviews your code? They're most likely going to get confused. Just take a look at this one. This is a very simple nesting if.

If C = true then
If D = true then
print A
if G = true then
print A
else
print B
end if
else
print B
end if
else
if E = true then
if F = true then
print A
else
print B
end if
else
print B
end if
end if

As you can see, it's going to be hard to debug since there are lots of conditions to test. This is somewhat easy since the indentions can help you identify which condition belongs to which "if" statement. If I remember correctly, I experienced having 50 or more "If" statements in one of my very old programs around a decade ago.

Probably the best way to work around the nesting "if" is to use a case, select or a similar functioning statement. If you have a similar condition in a lot of "if" statements, using a case statement will be very helpful. Here's a general example to give you an idea but not necessarily the correct syntax.

Case A
1: Print 1
2: Print 2
3: Print 3
4: Print 4
End Case

In previous example, if variable A has a value of 3, it prints 3. You don't have to put in 4 "if" statements just to test A. It'll waste space and doesn't really look good.

One aspect of program coding is to make it as compact but as efficient as possible. It should be optimized so that disk space is not wasted. However, there may be cases wherein there's no choice but to use nesting "ifs". One particular example is when the output is dependent on a previous condition and so on. The best way, as I said before, would most likely be to make use of indentation to identify each "if" statement.

And most importantly, as a programmer, I highly suggest you plan ahead before doing multiple or nesting "if" statements. This way, you won't get confused while coding and you can easily simulate the conditions as if it were running in real-time. This will make it faster and easier for you to code.

Published by Aaron Tadeo

Writing has become one of my hobbies and I really love the feeling when I share my experiences and knowledge as a freelance writer. I'm currently working as a customer service rep. I love computers and been...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.