Logic, Mathematics, and Programming Languages

Learn about conditional and logical operators in programming languages.

A programming language is a formal language that contains a set of instructions that produce various kinds of output. It can either be imperative or declarative. Imperative means it has a sequence of operations to perform and declarative involves the specification of desired results. It doesn’t tell us how to achieve them.

There are thousands of programming languages, with new ones being rapidly created. However, most of these programming languages are imperative. They contain specific commands or instructions for computers to perform certain tasks, producing various outputs.

Procedural programming

In natural languages, such as English, we have encountered the imperative mood where a verb is used to form a command or request. Consider these examples: “Do it” or “Leave,” and many more. Primarily, it’s directed toward a second person, but in specific contexts, it can involve the first or third person, such as “Let’s do it.”

In the same way, imperative programming also comprises specific commands, a set of instructions that focus on how a program should operate. Consider a simple procedural imperative programming example in the C programming language.

Press + to interact
//C
#include<stdio.h>
#include<stdlib.h>
void doSomething(void){
printf("Do something.");
}
int main(int argc, char**argv){
doSomething();
return (EXIT_SUCCESS);
}

In the code above, we see that the function (method) declaration gives us a standard output when the function is invoked. In the main() function, the compiled code runs the code as we invoke the doSomething() function. It gives us the following output:

Do something.

The code snippet above gives us an idea of how procedural programming works. It’s a part of the imperative programming paradigm. It’s called procedural because the program is built on one or more procedures, functions, or subroutines.

Now the question is, where do discrete mathematical concepts stand here? What’s the relationship between procedural programming and discrete mathematics? The simple answer is that a program should either build successfully or fail miserably. It cannot go on continuously. So a program must be either 1 or 0—true or false. To be more precise, it should be discrete.

Object-oriented programming

We need to successfully compile the code, which is discrete. We give the instructions that should be discrete. And finally, we need the output, which should also be discrete. Now, imperative programming doesn’t end with only procedural programming. There are object-oriented programming paradigms. A C++ program is a good example. Let’s see the code first.

Press + to interact
//C++
#include<iostream>
#include<string>
using namespace std;
class DoSomething{
public:
string giveInstruction = "Do something";
void givingInstruction(){
cout << giveInstruction << "\n";
}
};
int main(int argc,char**argv){
DoSomething firstObject;
firstObject.givingInstruction ();
return 0;
}

The public: scope modifier in C++ means that these members can be accessed by functions outside the class, such as main() and other functions in the program. In Java, we need to write the public keyword with all the members we want to be accessible from outside the class.

The C++ code above does the same thing, giving us the same output type, but it does it discretely in a distinct way.

We hope now it’s clear why we need to understand discrete mathematical concepts; it’s required to understand our code better. Discrete mathematics is a part of mathematical concepts that are not continuous. We can’t let our code run continuously. It should build successfully or fail miserably; whatever the outcome, it should be discrete. So, we need to understand discrete mathematical concepts.

Does logic come before mathematics?

As far as humans are concerned, the answer is yes. Without knowing any mathematical concepts, a child knows what is hot and what is cold. They develop their logical experience through observation and actions. Is it hot or cold? The answer is yes or no. Later in life, they apply their sense of logic to understand mathematical concepts.

So logic comes before mathematics, and after that comes algorithms or steps. Finally, we write code based on that algorithm.

In a programming language, logic plays the most significant role because it leads to the acceptance of one proposition, the conclusion, based on other offers or premises. Moreover, the decision is discrete. That’s why, in a programming language, the truth table plays an important role. A programming language’s if, if-else, or else conditionals lead us to one inference.

In ordinary life, we also apply the same logic by observation. Let’s look at an elementary program in Java to see how this logical inference matters.

Conditional decisions

Let’s start with our first input example in a Java program. First, we have to provide an input value for age in the box below the code screen. Then, we can run this code. We should execute it multiple times by inputting different values of age, e.g., 18, 45, 50, and 61, and observe the corresponding output.

Click the “Run” button to execute the code given below.

import java.util.Scanner;

//IfAndElse
class Main{
    static int age = 0;

    public static void main(String[] args) {
        System.out.println("Kindly enter the age: ");
        Scanner yourAge = new Scanner(System.in);
        age = yourAge.nextInt();

        if (age >= 1 && age <= 18){
            System.out.println("Happy birthday!");
        } else if(age == 21 || age == 50){
            System.out.println("Important birthday!");
        } else if(age >= 60){
            System.out.println("Ready to retire, or want to keep working?");
        } else {
            System.out.println("Keep being awesome and help others!");
        }
    }
}
Java example of conditional decisions

Upon executing this program three times with distinct input values, we get the following outputs:

  • Output 1:
    Kindly enter the age: 
    45
    Keep being awesome and help others!
    
  • Output 2:
    Kindly enter the age: 
    18
    Happy birthday!
    
  • Output 3:
    Kindly enter the age: 
    61
    Ready to retire, or want to keep working?
    

In the Java code above:

  • Line 9: The input from the user is taken via Scanner(System.in).

  • Lines 12–20: We use if and else if to determine the appropriate response for the specific age.

  • Notice that the input values 1920, 2249, and 5159 for the age variable produce the same result.

The Java code above is based on a few principles that we can call a truth table, based on which we build our algorithm. Its logical steps are universal and it’s valid for the Java programmer and applies to every programming paradigm.

There are three logical operators. The AND operator (represented by the && symbol); the OR operator (represented by the || symbol); and the NOT operator (represented by the ! symbol) that converts true into false and vice versa.

In the case of the && symbol, if both statements are true, it comes out as TRUE. For the || symbol, if any of the statements is true, it comes out as TRUE. And we already know about the nature of the ! symbol.

Truth Table

A

B

A || B

A && B

! A

False

False

False

False

True

True

False

True

False

False

False

True

True

False

True

True

True

True

True

False

These logical operations are dependent on conditional operators. They also have different symbols, such as ==, !=, <, >, >=, and <=. We perform different operations according to the symbol used. For example, when two conditions are equal (==), we perform the specified operations.

Conditional Operators in Java/C++

Mathematical Operators

Java/C++

Description

=

==

Equal to

!=

Not equal to

<

<

Less than

>

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

We can conclude that logic, mathematics, algorithms, and code are all linked.