Computer Science Canada Algebra of conditionals |
Author: | wtd [ Fri Dec 10, 2021 7:31 pm ] | ||||||||||||||||
Post subject: | Algebra of conditionals | ||||||||||||||||
Let's examine the algebra of conditionals. We'll use Python syntax with symbols because it's pretty. We'll also assume for now that we don't have any statements outside nested conditionals. A sample nested conditional.
How can we flatten this? We know we have to "and" the previous level with any nested conditions. So, as a first step:
But we can go right down to a single level with ease:
Let's look at another example:
When two consecutive branches of a conditional yield the same result, we can "or" them together. If one of the consecutive conditions that leads to the same result is "else", we can just use "else".
But what happens if they're not consecutive. What if other branches lie in between?
We can still "or" those together, but we have to "and" the negation of each intermediate condition.
Or just:
|