Truth table in Dart and Flutter

We will learn three very important concepts in Dart and Flutter. Firstly, what is truth table. Secondly, how to use truth table. And, finally, why we need truth table.

First thing first.

What is truth table?

Any type of programming language has a close relationship with Discrete Mathematics.

The truth table is a discrete mathematical concept.

Therefore, we need to dig deeper to learn about truth table.

We know that boolean is a built-in data type in Dart. We use boolean data type quite extensively in Flutter also.

The boolean data type has two values – true and false. We know that Boolean algebra is the branch of algebra in which the values of the variables are the truth values true and false.

We usually denote them as 1 and 0.

In 1874, George Boole introduced Boolean algebra in his first book The Mathematical Analysis of Logic.

Augustus De Morgan was a contemporary mathematician of George Boole. Although he did not create the laws using his name, yet it is credited to him, since he was the creator.

De Morgan’s laws are based on Boolean algebra, and in every programming language, it is widely applied and equally true.

What the rule states, we can write this way, where ‘a’ and ‘b’ are two boolean values (true or false):

1. not (a and b) is the same as (not a) or (not b)
2. not (a or b) is the same as (not a) and (not b)

At a glance, it looks confusing. However, if we know the truth table, it is fairly simple.

According to the truth table, true and false becomes always false. However, true or false becomes always true.

So we can rewrite the above statement as folloews.

1. true is the same as false or true
2. false is the same as false and true

Consequently, the above statement makes a sense now.

Consider the first statement. Here a represents true and b represents false. When a and b, that means true and false it becomes false.

Therefore not(a and b) stands for true. It is same as (not a) or (not b). The (not a) is false. And the (not b) is true.

Subsequently, the statement means true is the same as (false or true).

Therefore, we can rewrite the above statement in a simple way.

1. true or false is true
2. true and false is false

How to use truth table?

For many reasons, the truth table is important. Therefore, let us learn through a simple Dart program.

In the previous section, we have learned what is enum in Dart and Flutter.

Suppose we have two types of Human. Good and Bad.

Then we can write the code as follows.

enum HumanType {
  good,
  bad,
}

In a Human class we can use that Human type as a instance variable.

class Human {
  
  HumanType? humanType;
  
  Human({this.humanType});  
  
}

The rest part is quite easy. In the top-level main() function, we can prove the De Morgan’s laws on the truth table.

void main() {  
  
  Human good = Human(humanType: HumanType.good);
  Human bad = Human(humanType: HumanType.bad);
  
  if(good.humanType == HumanType.good || bad.humanType == HumanType.bad) {
    print('Good or bad? Always Good.');
  } else {
    print('Good or bad? Always Bad.');
  } // Good or bad? Always Good. 
  
  if(good.humanType == HumanType.good && bad.humanType == HumanType.bad) {
    print('Good and bad? Always Bad.');
  } else {
    print('Good and bad? Always Good.');
  } // Good and bad? Always Bad.   
  
}

Now, we do not have to memorise the truth table. On the contrary, we can remember it in a very simple way.

When we choose between a good human or bad human, we opt for good. Right?

However, when there is no choice. A good human and bad human mix up. It is bad. We have to accept it.

Certainly, here good stands for true. And bad stands for false.

Why we need truth table?

In many cases, we need to combine boolean expressions using the logical operators.

In a single control flow statement, we can manage it by one boolean expression.

void main() {
  int x = 1;
  if(x == 1) {
    print('True');
  } // true
}

However, that was not true in the previous code.

In Flutter, we have seen how we can change color of two containers. When one is active, the other becomes inactive.

However, we can make the whole code looks much shorter with conditional or ternary operators.

For an example, we can change the above code as follows.

void main() {  
  
  Human good = Human(humanType: HumanType.good);
  Human bad = Human(humanType: HumanType.bad);  
  
  // conditional or ternary operator
  
  (good.humanType == HumanType.good || bad.humanType == HumanType.bad) ?
    print('Good or bad? Always Good.') :
  print('Good or bad? Always Bad.'); // // Good or bad? Always Good. 
  
  (good.humanType == HumanType.good && bad.humanType == HumanType.bad) ?
    print('Good and bad? Always Bad.') :
  print('Good and bad? Always Good.'); // // Good and bad? Always Bad.  
  
}

In the next section we will see ternary operator. We will implement in Flutter.

So stay tuned.

What Next?

Books at Leanpub

Books in Apress

My books at Amazon

Courses at Educative

GitHub repository

Technical blog

Twitter

Comments

One response to “Truth table in Dart and Flutter”

  1. […] the previous section, we have seen how the ternary operator works. In this section we will learn how to use it in […]

Leave a Reply