Skip to content


PP: Conditionals


What we saw in the last blog was a simple tax calculator - perhaps too simple to be useful. We made a lot of assumptions to ensure that we can complete the program, with constructs we know. One major assumption was the flat tax rate. Typical tax rate specification would be something like this:Income upto Rs A : no tax
Income over A but less than B: 30% of income
Income over B but less than C: 30% of B + 50% of (Income - B)
Income over C : 30% of B + 50% of (C-B) + 70% of (Income - C)

The number of slabs may vary, but you see that the computation depends on which income slab your income falls in. So, you want to write your program as:

if income less than A then tax = 0
if income greater than A and less than B then tax = income * 30/100
if income greater than B and less than C then tax = B * 30/100 + (income - B) * 50/100
and so on

This is where conditionals are useful. A construct which allows you to do something only when a specific condition is true, is a necessary part of any programming language. In Java, the conditional construct looks like the informal notation above. More formally, we would write the same in Java as:

if (total_income < 100000) income_tax = 0;
if ((total_income > 100000) &&' (total_income < 200000))
'' income_tax = total_income * 30/100;
and so on.

Despite the remarkable similarity with the English notation, note some important differences.

“then” word is not used to indicate the action to be performed. This is the case in many of the programming languages, though some languages do mandate it to be used. One problem when you omit “then” is the difficulty in identifying the end of the condition and the beginning of the action part. Therefore, it is customary to expect that the entire condition part be enclosed in brackets as shown in the example above.

Secondly instead of English terms like less than, we use symbols for them. The standard set of symbols for common conditions are as follows. This set is also followed quite the same way in most languages.

less than <''''''' less than or equal to' <=
greater than >'' greater than or equal to >=
equal to =='''' not equal to <>

Of particular importance is to note the symbol for “equal to”. Remember the symbol used to assign a value to a variable. We used “=” to denote that. In order to avoid confusion with this, a comparison check between two values is denoted with two consecutive equal signs. Common mistakes in programming is to interchange these two symbols. Some of these may be detected when the compiler compiles the program; but not all such cases can be reliably detected. So be careful with these symbols.

Also note that “>=” and “=<" are not equivalent. While languages differ on what is the symbol for 'greater or equal', they will allow only one of these for this purpose. Often, the other symbol may be used for some other purpose. For example, '=>” is often used as an arrow or as implication operator, and hence has nothing to do with “>=”.

As in the case of comparisons, we use specific symbols for logical operators. && for ‘and’, || for or, ! for not, etc. Again, the English terms are not acceptable. Single & and single | are used for other purposes (we will see later), and hence the use of symbol-pairs here. [order of evaluation of conditional: tbd].

If we look at our daily usage, we use conditionals in two distinct ways. One is to do something only if a condition is true. If it is likely to rain, take an umbrella. If the condition is false, nothing is to be done. The second case is where we use a condition to choose between two alternatives. If your score is less than 45, your status is fail, otherwise your status is pass. There is something to be done irrespective of the condition being true. For this case, it would be useful to have an ‘otherwise’ construct. And most languages provide you an ‘else’ companion to the ‘if’ statement.

if (condition) do-something
else do-something-else

Look at this for a minute. Strictly speaking, you dont need this. You can always write this as:

if (condition) do-something
if (not condition) do-something-else

In some cases, the former is more intuitive. It also means the condition will be evaluated only once. So, when there is a choice, use if-then-else. Otherwise, use if-then.

And you can extend this further. What if we have a choice between three options, as in our tax computation case. We had three or four slabs, and we need a different tax formula for each of them. We can nest the if-else structure.

if (income < A) something
else if (income < B) something-else
else if (income < C) something-different
else error

…..


Posted in PP.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.