# Mathematics of if-else statements

## "Bora Eryilmaz" <bora@nope.com>5 Sep 2001 22:11:55 -0400

From comp.compilers

Related articles
Mathematics of if-else statements bora@nope.com (Bora Eryilmaz) (2001-09-05)
Re: Mathematics of if-else statements avbidder@vis.ethz.ch (Adrian 'Dagurashibanipal' von Bidder) (2001-09-10)
Re: Mathematics of if-else statements old_dnepr@yahoo.com (2001-09-10)
Re: Mathematics of if-else statements joachim_d@gmx.de (Joachim Durchholz) (2001-09-11)
Re:Mathematics of if-else statements dsha@tepkom.ru (Dmitry Shaporenkov) (2001-09-11)
Kleene Algebra (was: Mathematics of if-else statements) whopkins@csd.uwm.edu (2001-09-11)
| List of all articles for this month |

 From: "Bora Eryilmaz" Newsgroups: comp.compilers Date: 5 Sep 2001 22:11:55 -0400 Organization: The MathWorks, Inc., Natick, MA 01760 Keywords: optimize, question Posted-Date: 05 Sep 2001 22:11:55 EDT

Are there any mathematical/logical approaches to analyze nested
if-else statements in order to simplify or optimize them, or to
understand the flow of successive tests. For example, if I have

if (a > 10) {
if (a < 20) {
statement1;
} else {
statement2;
}
} else {
statement3;
}
where a is an integer, say.

Now, I can define the logical parameters C1 : (a>10), and C2 = (a<20)
Then I can write the truth table

C1 C2 Result
--- --- -----------
T T statement1
F T statement3
T F statement2
F F statement3

Then I can rearrange the if-else statements noting that the above truth
table can also be written as (!C1 means not(C1))

!C1 !C2 Result
--- --- -----------
F F statement1
T F statement3
F T statement2
T T statement3

so that

if (a < 10) {
statement3;
} else {
if (a > 20) {
statement2;
} else {
statement1;
}
}

My point is that such an analysis tool using math/logic may help
re-organize or optimize code, but I don't claim that the above example
is very useful. So any body familiar with such tools?

Thanks
Bora Eryilmaz

Post a followup to this message