|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
PP: More on Conditionals (incl Switch)
In the previous post, we saw the basic nature of conditionals in programming languages. Some way to handle conditional is essential in any programming language. And most of the languages represent them as some variant of if-then-else. Some languages abbreviate "else if" as "elif", etc as another convenience. Note that these are just decorations, and does not add any power or flexibility to languages.Compare the sequences (A):
if c1 then a1 if c2 then a2
and (B):
if c1 then a1 else if c2 then a2
These are not equivalent. This is a common mistake most beginners make. The equivalent version of (B), without using "else" is:
if c1 then a1 if not c1 and c2 then a2
Similarly, when there is a series of "else if"s, the last else actually corresponds to "(not c1) and (not c2) and (not c3)...".
A special case of these conditionals often occurs in programming, for which most languages have introduced a special construct: the switch statement.
One of the clauses in the income tax law states: a standard deduction of 10000 Rupees is allowed on the income, for males and 15000 for females. So, after you compute the total income, you need to deduct this amount before computing tax. You can write:
if (sex is male) then sded = 10000 else if (sex is female) then sded = 15000
Let me take a moment"s diversion here. Take a closer look at this statement, and in particular, the second if-condition.
Assuming that sex is either male or female, one does not need the second condition check. The else indicates that sex is not male, and hence it must be female. However, in general, it is useful to put that condition there. One purpose is clarity. It makes the law very explicit. This is very handy, when you update these statements when the rules change next year, where these categories may be revised.
However, when you include this condition, semantically the statement is incomplete. What if both conditions are false? What happens to standard deduction? But, can both conditions become false? In computer programming parlance, it is possible. Someone when entering data, may have entered a wrong value for "sex". It could be a mistake in typing, or filling in some other column value for this column in the form, and so on. So, it is a good idea to address these by extending the statement as:
if (sex is male) then sded = 10000 else if (sex is female) then sded = 15000 else print "illegal sex value"
Such small precautions can save you days of effort in tracking down bugs. Without this, it is possible, you will happily go ahead calculating sded, using an illegal value and get, say, the total tax wrong. And you will wonder what went wrong. Alert messages as given above, get you to the root of the problem much faster and with less pain.
Coming back to the notion of switch statement, this kind of branching depending on the different values of a variable is what a switch statement facilitates. Another example:
print "choose your language: 1 - English, 2-Hindi, 3-Tamil, ..".
You can see such directions in ATM machines, telephone answering services. You need to choose one out of a set of options provided. (We will see later how to read a user's choice - for now, let us assume we know). And you have to perform different actions depending on the choice made. Unlike in the tax computation example, here the choice is dependent on different values of a single variable as in:
if (lang == 'E') then .... else if (lang == 'H') then .... else if (lang == 'T' ) then ....
Or
if (option == 1) then .... else if (option == 2) then .... else if (option == 3) then ...
A switch statement provides a short hand for these kinds of code segments:
switch (option) { case 1: .....; break; case 2: .....; break; case 3: ....; break; default: print "illegal option"; }
Against switch you specify the variable on which you want to branch on, and against 'case', you specify the possible values. In place marked '.....', insert the code corresponding to different values.
Compare this template with the if-then-else template given before, and you can see the mapping. In principle, you can convert any switch statement to if-then structure (of course, not vice versa!).
In the template you see two new entities: break and default. Let us try to understand why they are there and what they do.
The 'default' is like the catch-error else part we had introduced in if-then earlier. A way to specify some action, in the event of the value not matching with any of the listed values. So you can write our sex-tax code as:
switch (sex) { case 'M': sded = 10000; break; case 'F' : sded = 15000; break; default : sded = 0; print "illegal sex value"; }
The value not matching can arise in two cases. One is the error condition, we have sketched here. Another is a scenario like this:
switch (sex) {
case 'F' : sded = 15000; break;
default : sded = 10000;
}
This captures the rule 'for females, the exception is 15000; for everyone else it is 10000''. This is same as the above, if we are sure that the only sex values are 'm' and 'f'. Thus, you can use 'default' to pull together all values that need not be given special attention.
What is the 'break' about? 'Break' is a general construct - not specific to switch - with the meaning 'break free'. We will discuss this in a later post. For now, just make sure to put a 'break' after handling each possible value in a switch.
Practice writing in if-then-else form and switch form, the various conditionals that you can think of. Write them in as many different ways as possible, and see how they differ in readability, efficiency, correctness, etc.
|
|
| | |
|
|
|
|
|
|
|