# Re: Mathematics of if-else statements

## old_dnepr@yahoo.com (Oleg T.)10 Sep 2001 23:04:17 -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)
| List of all articles for this month |

 From: old_dnepr@yahoo.com (Oleg T.) Newsgroups: comp.compilers Date: 10 Sep 2001 23:04:17 -0400 Organization: http://groups.google.com/ References: 01-09-020 Keywords: optimize Posted-Date: 10 Sep 2001 23:04:17 EDT

"Bora Eryilmaz" <bora@nope.com> wrote in message news:01-09-020...
> 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.

Yes, the boolean arithmetic rules can be used to minimize the logic
expressions. Also "Carno Card" can help us(not sure if I am writing
this name by true way).

> 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.

This example demonstrates two logically equivalent expressions those
cannot be minimized anymore, just maximized. I would re-write it so:

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

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

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

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

Regards,
Oleg

Post a followup to this message