2. Introduction to Programming Language and Boolean Algebra
Programming language is a formal language that contains set of instructions, which produce various kind of output. It can either be imperative or declarative.
Imperative means it has sequence of operations to perform. On the other hand, declarative stands for the specification of desired results, it does not comprise of how to achieve it.
There are thousands of programming languages, many are being created everyday, probably! However, most of them are imperative. They comprise of certain commands or instructions for computers to perform certain tasks, producing various outputs. Let us try to understand this part very clearly.
In natural language, such as in English, we have encountered imperative mood where a verb is used to form a command or request. Consider this example: “do it”, or “Leave” and many more. Mostly it is directed towards a second person, but in some certain contexts, it can involve first ot third persons, such as, “Let’s do it”.
The same way, an imperative programming also comprises of certain commands, set of instructions that focus on how a program should operate. Consider a simple procedural imperative programming example in C programming language.
1 //code 2.1
2 //C
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 void doSomething(void){
7 printf("Do something.");
8 }
9
10 int main(int argc, char** argv) {
11
12 doSomething();
13
14 return (EXIT_SUCCESS);
15 }
In the above code, we have seen the function (method) declaration that gives us a standard output when the function is invoked. In the main() function area, the compiled code will run the code as we invoke the function doSomething(). It gives us the following output.
Quite simple and straightforward.
1 //output of code 2.1
2 Do something.
3 RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms
The above code snippet gives us an idea of how procedural programming works. It is a part of imperative programming paradigm. It is called procedural, because, the program is built on one or more procedures or functions. It is sometimes also called subroutines.
Now question is, where does discrete mathematical conceptions stand here? What is the relationship between a procedural programming and discrete mathematics?
The simple answer is, a program should build either successfully or failed miserably. It cannot go on continuously. So a program must be either true or false, 1 or 0, to be more precise.
It should be discrete. We need to compile the code successfully, that is discrete. We give the instructions, that should be discrete. And, finally, we need the output, that should also be discrete. Now, imperative programming does not end with only procedural programming. There is object-oriented-programming paradigms. A C++ program could be a good example.
Let us see the code first.
1 //code 2.2
2 //C++
3 #include <iostream>
4 #include <string>
5 #include <cmath>
6 #include <cstdlib>
7 #include <sstream>
8 #include <numeric>
9 #include <string>
10 #include <vector>
11 #include <cstddef>
12 #include <limits>
13
14 using namespace std;
15
16 class DoSomething {
17
18 public:
19 string giveInstruction = "Do something";
20
21 void givingInstruction(){
22 cout << giveInstruction << "\n";
23 }
24
25 };
26
27 int main(int argc, char** argv) {
28
29 DoSomething firstObject;
30
31 firstObject.givingInstruction ();
32
33 return 0;
34 }
The above guy does the same thing, giving us the same type of output, only in a different manner. This C++ guy does the same thing discretely. In a distinct way. We cannot let it run continuously.
I hope, now it is clear to you why we need to understand discrete mathematical conceptions; it is needed to understand our code in a better way. Discrete mathematics is a part of mathematical conceptions that are not continuous. We cannot let our code run continuously. So we need to understand discrete mathematical conceptions.
I am not going to elaborate this section anymore. There are several other types of imperative programming. There are several other types of declarative programming languages. This is not the place to discuss them.
It is only imperative to know that our code cannot run continuously. Either it should build successfully or fail miserably; whatever the outcome, it should be discrete.
Logic, Mathematics, and Programming Language
Does logic come before mathematics? As long as humans are concerned, the answer is “Yes”. A child without knowing any mathematical conceptions, knows that what is hot and what is cold. She develops her logical experience by observation and actions. Is it hot or cold? The answer is yes or no. Accordingly, her actions take place.
Latter in life, she applies her sense of logic in understanding mathematical conceptions. Logic comes before mathematics and after that comes algorithm or steps; finally we write code based on that algorithm.
In programming language, logic plays the greatest role; because it leads to the acceptance of one proposition, the conclusion, on the basis of other propositions or premises. Moreover, the conclusion is discrete. That is why, in programming language, the truth table plays an important role. In programming language, if, if-else, or else conditionals lead us to one inference.
In ordinary life we also apply the same logic by observation.
Let us see a very simple program in Java, to see how this logical inference matters.
1 //code 2.3
2 //Java
3 package fun.sanjibsinha;
4
5 import java.util.Scanner;
6
7 public class IfAndElse {
8
9 static int age = 0;
10
11 public static void main(String[] args) {
12 System.out.println("Enter your age: ");
13 Scanner yourAge = new Scanner(System.in);
14 age = yourAge.nextInt();
15
16 if (age >= 1 && age <= 18){
17 System.out.println("Happy birthday!");
18 } else if(age == 21 || age == 50){
19 System.out.println("Important birthday!");
20 } else if(age >= 60){
21 System.out.println("Don't retire. Keep working!");
22 } else {
23 System.out.println("Keep living and help others!");
24 }
25 }
26 }
We have entered three different number and got the following output:
1 //output of code 2.3
2 //output 1
3
4 Enter your age:
5 45
6 Keep living and help others!
7
8 //output 2
9 Enter your age:
10 18
11 Happy birthday!
12
13 //output 3
14 Enter your age:
15 61
16 Don't retire. Keep working!
The above Java code, is based on a few principles that we can call ‘truth table’, based on which we build our algorithm. These logical steps are universal. It is not only true for the Java guy, but also applicable to every single programming paradigm. What is that?
There are three logical operators; they are ‘&&’ symbol that stands for ‘and’; ‘||’ symbol that stands for ‘or’; finally, we have a ‘!’ symbol that just converts true into false and vice versa.
In case of ‘&&’ symbol, if both statements are true, it comes out as TRUE. For the ‘||’ symbol, if any one of the statement is true, it comes out as TRUE. And you have already known about the nature of ‘!’ symbol.
These logical operations are dependent on conditional operators; they also have different symbols, such as ‘==’, ‘!=’, ‘<’, ‘>’, ‘>=’, and ‘<=’; when two conditions are ‘==’, (equal), we perform some operations and so on.
We can conclude that, logic, mathematics, algorithm and code are inter-dependent; and, they should be discrete, as well.
Introduction to Boolean Algebra
George Boole, the founder of Boolean algebra, is considered to be one of the founder of Computer Science also. In 1847, when he wrote his famous book “The Mathematical Analysis of Logic”, he had not thought about PC, mobile or tabs. I don’t want to say that. But in his second book, “An Investigation of the Laws of Thought” that he wrote in 1854, he clearly set the path for future computations. His great idea started a new branch of algebra, Boolean algebra, where the values of the variables are the truth values: true or false. They are usually denoted by 1 and 0 respectively. In elementary algebra the values of variables are numbers. There are several operations we can do on that like addition or multiplication.
In Boolean algebra, there are only three operations we can do: conjunction (and), disjunction (or) and negation (not). Now we are able to the logical operations as we use to do the numerical operations in elementary algebra.
Moreover, it helps programmers to create a formal description of logical operations with the help of conditionals like “if, else if and else”, and in some cases using “switch-cases”.
Now it adds great impetus to every modern programming language.
Not only that, with the help of this ‘truth table’, we can build a very complex decision trees. Let us start with a simple example.
Let us see the code first.
1 //code 2.4
2 //Java
3 package fun.sanjibsinha;
4
5 public class IfAndElseSimple {
6 static boolean isTrue = true;
7 static boolean isFalse;
8 public static void main(String[] args) {
9
10 if(isTrue){
11 System.out.println("It's true.");
12 isTrue = false;
13 if(isTrue && isTrue){
14 System.out.println("It's again true.");
15 } else if(isTrue || isFalse){
16 System.out.println("It's very much true.");
17 } else {
18 System.out.println("True is false, so it's false now.");
19 }
20 } else {
21 System.out.println("It's false.");
22 }
23
24 }
25 }
Watch the output:
1 //output of code 2.4
2 It's true.
3 True is false, so it's false now.
Globally we have made the boolean variable ‘isTrue’ true. Therefore, the first ‘if’ block allows us to enter into the block. Next, we have made the ‘isTrue’ false.
Now according to the boolean algebra and truth table, ‘false and false’ is ‘false’; and, ‘false or false’ is also ‘false.’ For that reason, we get the above output.
Now, we would like to change this code a little bit.
1 //code 2.5
2 //Java
3 package fun.sanjibsinha;
4
5 public class IfAndElseSimple {
6 static boolean isTrue = true;
7 static boolean isFalse;
8
9 public static void main(String[] args) {
10
11 if(isTrue){
12 System.out.println("It's true.");
13 if(isTrue && isTrue){
14 System.out.println("It's again true.");
15 } else if(isTrue || isFalse){
16 System.out.println("It's very much true.");
17 } else {
18 System.out.println("True is false, so it's false now.");
19 }
20 } else {
21 System.out.println("It's false.");
22 }
23
24 }
25 }
Watch the output now:
1 //output of code 2.5
2 It's true.
3 It's again true.
Now, in the second conditional, ‘true and true’ is ‘true’. So, we get the above output.
Let us see more examples and watch the output one after another to comprehend how this truth table works.
1 //code 2.6
2 //Java
3 package fun.sanjibsinha;
4
5 public class IfAndElseSimple {
6 static boolean isTrue = true;
7 static boolean isFalse;
8
9 public static void main(String[] args) {
10
11 if(isTrue){
12 System.out.println("It's true.");
13 if(((isTrue && isTrue) || isFalse) && isFalse){
14 System.out.println("It's not true any more true.");
15 } else if(((isTrue && isTrue) || isFalse) || isFalse){
16 System.out.println("It's very much true because we check between tru\
17 e or false.");
18 } else {
19 System.out.println("True is false, so it's false now.");
20 }
21 } else {
22 System.out.println("It's false.");
23 }
24
25 }
26 }
It’s quite obvious that very first conditional is true, so we enter the block. However, the next conditional comes out as false, because in the truth table ‘true and false’ will come out as false. Therefore, the code will check the next conditional, which is true.
So the output will be as the following:
1 //output of code 2.6
2 It's true.
3 It's very much true because because we check between true or false.
As the above code snippets, we can apply the same truth table on other data types using the logical operators. Here is an example:
1 //code 2.7
2 //Java
3 package fun.sanjibsinha;
4
5 import java.util.Scanner;
6
7 public class IfAndElse {
8
9 static int age = 0;
10
11 public static void main(String[] args) {
12 System.out.println("Enter your age: ");
13 Scanner yourAge = new Scanner(System.in);
14 age = yourAge.nextInt();
15
16 if (age >= 1 && age <= 18){
17 System.out.println("Happy birthday!");
18 } else if(age == 21 || age == 50){
19 System.out.println("Important birthday!");
20 } else if(age >= 60){
21 System.out.println("Don't retire. Keep working!");
22 } else {
23 System.out.println("Keep living and help others!");
24 }
25 }
26 }
Let us give different types of ‘age’ to check how our code works.
1 //output of code 2.7
2 //output 1
3
4 Enter your age:
5 45
6 Keep living and help others!
7
8 //output 2
9 Enter your age:
10 18
11 Happy birthday!
12
13 //output 3
14 Enter your age:
15 61
16 Don't retire. Keep working!
Let us make this example a little bit complex, so we can have an idea about how complicated this combinations might become.
1 //code 2.8
2 //Java
3 package fun.sanjibsinha;
4
5 import java.util.Scanner;
6
7 public class MoreIfAndElse {
8
9 static boolean isCold = false;
10 static boolean isRaining = false;
11 static boolean isTakingCar = false;
12
13 public static void main(String[] args) {
14
15 System.out.println("When asked, enter only true or false.");
16 System.out.println("Is it cold outside?");
17 Scanner cold = new Scanner(System.in);
18 isCold = cold.nextBoolean();
19 System.out.println("Is it raining?");
20 Scanner raining = new Scanner(System.in);
21 isRaining = raining.nextBoolean();
22 System.out.println("Are you taking car?");
23 Scanner takingCar = new Scanner(System.in);
24 isTakingCar = takingCar.nextBoolean();
25
26 if((isCold == true && isRaining == true) || isTakingCar == false){
27 System.out.println("I wear Windcheater jacket with hood.");
28 } else if((isCold == true && isRaining == false) || isTakingCar == true){
29 System.out.println("I wear Windcheater jacket without hood.");
30 } else {
31 System.out.println("I won't wear Windcheater of any kind!");
32 }
33
34 }
35 }
Let us give some different types of input as ‘true’ or ‘false’ and see how our code responds.
1 //output of code 2.8
2
3 //output 1
4
5 When asked, enter only true or false.
6 Is it cold outside?
7 true
8 Is it raining?
9 true
10 Are you taking car?
11 false
12 I wear Windcheater jacket with hood.
13
14 //output 2
15
16 When asked, enter only true or false.
17 Is it cold outside?
18 true
19 Is it raining?
20 true
21 Are you taking car?
22 true
23 I wear Windcheater jacket with hood.
24
25 //output 3
26
27 When asked, enter only true or false.
28 Is it cold outside?
29 true
30 Is it raining?
31 false
32 Are you taking car?
33 true
34 I wear Windcheater jacket without hood.
Like to make the combinations more complex? Well, we can try the following code snippets.
1 //code 2.9
2 //Java
3 package fun.sanjibsinha;
4
5 import java.util.Scanner;
6
7 public class AnotherIfAndElse {
8
9 static int age = 0;
10 static boolean isAllowed = false;
11
12 public static void main(String[] args) {
13
14 System.out.println("When asked, enter only true or false.");
15 System.out.println("Enter your age");
16 Scanner yourAge = new Scanner(System.in);
17 age = yourAge.nextInt();
18 System.out.println("Is allowed? Answer either true or false!");
19 Scanner allowed = new Scanner(System.in);
20 isAllowed = allowed.nextBoolean();
21
22 if(age <= 10 || age >= 70){
23 if(isAllowed == true){
24 System.out.println("You can go free!");
25 } else {
26 System.out.println("You can go free!");
27 }
28 } else {
29 System.out.println("Your entrance fee is 10 Euro.");
30 }
31 }
32 }
As usual, we will give different types of age to test how this combination works by maintaining the truth table rules.
1 //output of code 2.9
2 // output 1
3
4 When asked, enter only true or false.
5 Enter your age
6 80
7 Is allowed? Answer either true or false!
8 true
9 You can go free!
10
11 // output 2
12
13 When asked, enter only true or false.
14 Enter your age
15 56
16 Is allowed? Answer either true or false!
17 true
18 Your entrance fee is 10 Euro.
19
20 // output 3
21
22 When asked, enter only true or false.
23 Enter your age
24 2
25 Is allowed? Answer either true or false!
26 false
27 You can go free!
28
29 // output 4
30
31 When asked, enter only true or false.
32 Enter your age
33 85
34 Is allowed? Answer either true or false!
35 false
36 You can go free!
The above code establishes one thing, by implementing the proper usage of the truth table, we can stop the middle conditional to override the basic rule that has been defined earlier. The above code snippets check the entry to some places. In between there is a boolean value called ‘isAllowed’; you may think this guy as the gatekeeper who can override the entry with a special power. In fact that happens in the real life.
However, through the proper usage of the truth table we have limited his power to override the main conditional that says, for the age range of less than equal to 10 and greater than equal to 70, the entrance fee is zero.
Now if the gatekeeper wants to take price from that age range, he cannot do that.
As we progress, we will see more examples of boolean algebra in the future course of our book. So stay tuned and keep reading.
I write regularly on Algorithm and Data Structure in
QUIZZ and Challenge on on Chapter Two
Question One: In Boolean algebra, how many operations we can do?
Option 1: One
Option 2: Two
Option 3: Three
Answer: Option Three. (In Boolean algebra, there are only three operations we can do: conjunction (and), disjunction (or) and negation (not).)
Question Two: Will ‘true and false’ come out as false in the truth table?
Option 1: Yes.
Option 2: No.
Answer: Option 1.
Question 3: What is the imperative programming language?
Option 1: The programming langauge that deals with Discrete Mathematics.
Option 2: The programming language that comprises of certain commands or instructions for computers to perform certain tasks, producing various outputs.
Option 3: None of the above is true.
Option 4: Both statements are true
Answer: Option 4
=======================
Question 4: What is the relationship between a procedural programming and discrete mathematics?
Option 1: The common relationship is that they both deal with Boolean Algebra.
Option 2: A program should build either successfully or failed miserably.
Option 3: Both cannot go on continuously.
Option 4: All of the above.
Answer: Option 4
=======================
Question 5: A child develops her logical experience by observation and actions. Is that true for Mathematics?
Option 1: Yes
Option 2: No
Answer: Option 1
=======================
Challenge 1: Can you write a program based on the <<Truth Table>>
Solution to Challenge 1: Language used Java
1 //code
2
3 package fun.sanjibsinha;
4
5 import java.util.Scanner;
6
7 public class IfAndElse {
8
9 static int age = 0;
10
11 public static void main(String[] args) {
12 System.out.println("Enter your age: ");
13 Scanner yourAge = new Scanner(System.in);
14 age = yourAge.nextInt();
15
16 if (age >= 1 && age <= 18){
17 System.out.println("Happy birthday!");
18 } else if(age == 21 || age == 50){
19 System.out.println("Important birthday!");
20 } else if(age >= 60){
21 System.out.println("Don't retire. Keep working!");
22 } else {
23 System.out.println("Keep living and help others!");
24 }
25 }
26 }
27
28 //output 1
29
30 Enter your age:
31 45
32 Keep living and help others!
33
34 //output 2
35 Enter your age:
36 18
37 Happy birthday!
38
39 //output 3
40 Enter your age:
41 61
42 Don't retire. Keep working!
Challenge 2: Write an algorithm in natural language that builds a very complex decision trees.
Solution to Challenge 2: Language used Java
1 //code
2
3 package fun.sanjibsinha;
4
5 public class IfAndElseSimple {
6 static boolean isTrue = true;
7 static boolean isFalse;
8 public static void main(String[] args) {
9
10 if(isTrue){
11 System.out.println("It's true.");
12 isTrue = false;
13 if(isTrue && isTrue){
14 System.out.println("It's again true.");
15 } else if(isTrue || isFalse){
16 System.out.println("It's very much true.");
17 } else {
18 System.out.println("True is false, so it's false now.");
19 }
20 } else {
21 System.out.println("It's false.");
22 }
23
24 }
25 }
26
27
28 //output
29
30 It's true.
31 True is false, so it's false now.
Challenge 3: In Boolean algebra, there are only three operations we can do: conjunction (and), disjunction (or) and negation (not). Can you prove this paradigm in a complex “Truth Table” that uses conditional logic.
Solution to Challenge 2: Language used Java
1 //code
2
3
4 package fun.sanjibsinha;
5
6 import java.util.Scanner;
7
8 public class MoreIfAndElse {
9
10 static boolean isCold = false;
11 static boolean isRaining = false;
12 static boolean isTakingCar = false;
13
14 public static void main(String[] args) {
15
16 System.out.println("When asked, enter only true or false.");
17 System.out.println("Is it cold outside?");
18 Scanner cold = new Scanner(System.in);
19 isCold = cold.nextBoolean();
20 System.out.println("Is it raining?");
21 Scanner raining = new Scanner(System.in);
22 isRaining = raining.nextBoolean();
23 System.out.println("Are you taking car?");
24 Scanner takingCar = new Scanner(System.in);
25 isTakingCar = takingCar.nextBoolean();
26
27 if((isCold == true && isRaining == true) || isTakingCar == false){
28 System.out.println("I wear Windcheater jacket with hood.");
29 } else if((isCold == true && isRaining == false) || isTakingCar == true){
30 System.out.println("I wear Windcheater jacket without hood.");
31 } else {
32 System.out.println("I won't wear Windcheater of any kind!");
33 }
34
35 }
36 }
37
38
39 //We have three outputs that prove the above paradigm
40
41 //output 1
42
43 When asked, enter only true or false.
44 Is it cold outside?
45 true
46 Is it raining?
47 true
48 Are you taking car?
49 false
50 I wear Windcheater jacket with hood.
51
52 //output 2
53
54 When asked, enter only true or false.
55 Is it cold outside?
56 true
57 Is it raining?
58 true
59 Are you taking car?
60 true
61 I wear Windcheater jacket with hood.
62
63 //output 3
64
65 When asked, enter only true or false.
66 Is it cold outside?
67 true
68 Is it raining?
69 false
70 Are you taking car?
71 true
72 I wear Windcheater jacket without hood.
Challenge 3: Can you write a program in any langauge that will charge money if the user’s age is between 10 and 70.
Solution to Challenge 2: Language used Java
1 //code
2
3
4 package fun.sanjibsinha;
5
6 import java.util.Scanner;
7
8 public class AnotherIfAndElse {
9
10 static int age = 0;
11 static boolean isAllowed = false;
12
13 public static void main(String[] args) {
14
15 System.out.println("When asked, enter only true or false.");
16 System.out.println("Enter your age");
17 Scanner yourAge = new Scanner(System.in);
18 age = yourAge.nextInt();
19 System.out.println("Is allowed? Answer either true or false!");
20 Scanner allowed = new Scanner(System.in);
21 isAllowed = allowed.nextBoolean();
22
23 if(age <= 10 || age >= 70){
24 if(isAllowed == true){
25 System.out.println("You can go free!");
26 } else {
27 System.out.println("You can go free!");
28 }
29 } else {
30 System.out.println("Your entrance fee is 10 Euro.");
31 }
32 }
33 }
34
35
36 //We can test this program various way and make it impregnable.
37
38 // output 1
39
40 When asked, enter only true or false.
41 Enter your age
42 80
43 Is allowed? Answer either true or false!
44 true
45 You can go free!
46
47 // output 2
48
49 When asked, enter only true or false.
50 Enter your age
51 56
52 Is allowed? Answer either true or false!
53 true
54 Your entrance fee is 10 Euro.
55
56 // output 3
57
58 When asked, enter only true or false.
59 Enter your age
60 2
61 Is allowed? Answer either true or false!
62 false
63 You can go free!
64
65 // output 4
66
67 When asked, enter only true or false.
68 Enter your age
69 85
70 Is allowed? Answer either true or false!
71 false
72 You can go free!