The 4 Basic Concepts Programming Languages Share

Veröffentlicht am - Letze Bearbeitung am

“A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs to control the behavior of a machine or to express algorithms.” (source Wikipedia)

Programming languages have been around for a while now, many people consider FORTRAN (first version appeared in 1954) as the first real computer programming language and even today there are systems which still use it. In the 1970’s C appeared, which was designed by Dennis Ritchie, his programming language was primarily adopted in the Unix environment and the beginning of the 21st century was C’s renaissance era, but nowadays its not so widely spread. Instead of C people tend to use C++ (also called C with classes), developed by Bjarne Stroustrup (I had to honor to chat with him about technology, he is an exceptionally bright mind), the first version appeared in 1979. Since its first version the language has changed much and nowadays has modern constructs like lambda expressions and automatic type deduction system.

Type System

Most programming languages have a type system or simply called types for the usage of variables, of course there are exceptions, like Assembly based programming languages, which are untyped languages.

Typed programming languages can be divided into statically typed and dynamically typed languages, for example Java and C are statically typed languages, meanwhile Python and JavaScript are dynamically typed Languages. Below you can find some examples:

String myName = “John Doe”; // String type in Java
char yourName[] = “Jane Doe”; // C’s string type a.k.a character array
var streetName = ‘Sunny’; // JS string
var streetName = “Sunny”; // JS string
cousinName = “Billy Doe” # python’s string

Types can be grouped in three large buckets. The first bucket contains the types which can help to manipulate text (strings). Since most of the developed software applications are designed and used by people, these have to use text when displaying calculation results or when requesting user input. String as a type is the most frequently used in almost every programming language.  As you can see in the example above almost in any programming language strings can be defined using double quotes.

The second bucket contains number based types (or numerical types), those which are used for mathematical or statistical calculations or for implementing algorithms. The examples below demonstrate some mathematical calculations in different programming languages using different number types:

<pre class="brush:java;toolbar:false;">/*
    this code is valid in C, Java, C#, and C++
*/
int lightYears = 2011;
int futureGalaxy = lightYears + (lightYears / 2 – lightYear % 3) + 29;
</pre>
#
#    Same code in python
#
lightYears = 2011
futureGalaxy = lightYears + (lightYears / 2 – lightYears % 3) + 29
giantLeap = futureGalaxy ** 2

Executed in python interpreter:

python interpreter

/*
    Same code in JS
*/
lightYears = 2011;
futureGalaxy = lightYears + (lightYears / 2 – lightYears % 3) + 29;
giantLeap = Math.pow(futureGalaxy, 2);

The third bucket stores all the rest, like Boolean values (true and false) and special values like Undefined in JavaScript or the NoneType in Python.

my_special_value = None # python None
isWinner = True # python bool
var myUndefined = undefined; //JS undefined
bool isMonday = True; //JS
bool isFriday = false; //java

Loops

The for loop (or repeat loop)

In many cases programming languages need to execute the same code many times, for example when an ERP (Enterprise Resource Planning) application calculates the salaries and the taxes of employees which have to deducted. Another example is a banking software, which transfers money from one account to another, it repeats the same operation, only the account numbers, the amount of money and the currency changes but the actual operation, like creating the database level transaction, handling the audit tables, sending out the confirmation messages to the clients and so on are changed for each iteration of the transfer operation.

#
#  Python code for adding a list of integers
#
sum = 0
for number in [1,2,3,4,5,78,99,101,123,1332,321,3322]:
    sum += number
print(sum) # will print 5391 in python
// Java Code for adding an array of integers
int[] numbers = [1,2,3,4,5,78,99,101,123,1332,321,3322];
int sum = 0;
for (int nr : numbers) {
    sum += nr;
}
System.out.print(sum);
// JS Code for adding an array of integers
var numbers = [1,2,3,4,5,78,99,101,123,1332,321,3322];
var sum = 0;
for (var nr in numbers) {
    sum += numbers[nr];
}
console.log(sum);
{ Pascal For loop }
var numbers := array[1..10] of integer;
var sum := 0;
var i := 0;
FOR i := 0 to 10 DO
    sum := sum + i;
WRITELIN(sum);

In the code samples above you can see how the for loop can be used to add up elements of an array (or list), the first code section is in Python, where the for in construct is used, then you have the Java code sample, here the array of elements is declared before the for loop. Then we have the sum variable declared. After that comes the for loop - even more it’s a foreach like loop in Java – which sums up the numbers. The last code part is in JavaScript and does the same thing, first we have the array declared, then the sum variable. The sum variable in JS is of type Number.

The do-while loop

Another option is the do while loop it is also called post-test loop. Meanwhile the for loop does not run at all, if the stop condition specified within the loop is false the do while loop for sure executes at least once. The Pascal programming language is interesting from this point of view because it does not have a do while loop, but it does have a REPEAT … UNTIL construct, which is basically the same. On the code below we can see the same sum calculation algorithm what we have implemented before using the for loop.

{ Pascal coding in Repeat Until aka Do While }
var numbers := array[1..10] of integer;
var sum := 0;
var i := 0;
REPEAT
    sum := sum + numbers[i];
    i := i + 1;
UNTIL i < 10;
WRITELN(sum);

The C-syntax based languages (C++, Java, C#, JavaScript) have the do while loop.

// JS Code for adding an array of integers
var numbers = [1,2,3,4,5,78,99,101,123,1332,321,3322];
var sum = 0;
var idx = 0;
do {
    sum += numbers[idx++];
} while (idx < numbers.length);
console.log(sum);

Be aware that if the numbers array would not have any element, then this code would raise an error, because the loop’s stop condition is evaluated after the first execution cycle.

In programming languages there are other types of loops too, like the while loop.

Conditional statements

Software by it’s nature has to be able to decide based on some conditions what to do. For example, when we want to calculate the absolute value of a number we need to decide whether the number what we want to use is negative or positive. Another example of conditional statements is for example in recommendation engines, there are a lot of conditions, for example if the target audience in mid-age single women then most probably they will be interested in shopping and some party places.

The if-else statement

The if statement is one of the easiest constructs which helps making decisions within our applications. Let’s take a look at some examples:

#
#    Python’s if statement
#
input_number = raw_input(“Please enter a number!”)
if input_number > 100:
    print(“it cannot be larger than 100, sorry I forgot to mention that :)”)
elif input_number < 500:
    print(“Aww, that’s so cute”)
else:
    print(“Yippee thanks for giving that much $ for charity purposes!”)
//
// C# if statement
//
int input_number = int.Parse(Console.ReadLine(“Please enter a number!”));
if (input_number > 100) 
{
    Console.WriteLine(“it cannot be larger than 100, sorry I forgot to mention that :)”);
}
else 
{
    Console.WriteLine(“Yippee thanks for giving that much $ for charity purposes!”);
}

As you can see in the code there is no major difference between programming languages in case of the If-else statement.

The switch statement

There is one interesting conditional statement in most programing languages, this is the switch statement, it is also called multiway branching. Let’s see the examples below:

//
// Java switch
//
int myAge = 30;
switch (myAge) {

  case 20: {
    System.out.println("You are too young to get married :P ");
    break;
  }

  case 30: {
    System.out.println("You should get married ASAP :P ");
    break;
  }

  default: {
    System.out.println("You should have been married or you are way to young to even think about it :D ");
    break;
  }
}
//
// JS Switch statement which returns the Weekend/Weekstart/Midweek
// words based on day value in the current date
//
switch (new Date().getDay()) {
    case 0:
    case 6:
        day = "Weekend";
        break;
    case 1:
    case 2:
    case 3:
        day = "Weekstart";
        break;

    case 4:
    case 5:
        day = "Midweek";
        break;
}

There are some programming languages, like C and C++ which do not support usage of string in switch statements, this is because in C and C++ there is no real string type, strings are basically an array of characters. For instance, Java, C# and JavaScript do support the usage of the switch statement with string values.

The ternary operator

There are cases in programming when a simple condition has to be used for branching the code execution, of course we could use the traditional if-else construct, but some programming languages has added the ternary operator (for example in Java and C# these the ? and : ). The reason for adding these is that it saves space when using  it and makes the code more readable and understandable.

//
// C# and Java ternary operator use
//
bool isHighlighted = myControl.hasFocus() ? true : false;

You can read the code above as, if myControl has the mouse focus then isHighlighted should be set to true, otherwise to false. The code above can be rewritten as the following:

//
// C# and Java ternary operator as if statement
//
bool isHighlighted = false;
if(myControl.hasFocus()) {
   isHighlighted = true;
}
else {
   isHighlighted = false;
}

Of course the ternary operator should be used only in cases when the logic on both branches (true and false) are simple and mostly contain one command.

User Notification

In every programming language there is a possibility to display messages to the user on a so called console, below you can see some examples of this in different programming languages.

//
// C# console messaging
//
Console.WriteLine(“Message to the console!”);

//
// Java console messaging
//
System.out.println(“This is a message for the Java console”);

//
// JS console message
//
console.log(‘This is a message for the JavaScript console’);

//
// C console message
//
printf(“This is a message from the C console”);

//
// C++ console
//
cout << “Hey, this a message for C++ console.”;

In this article we covered the 4 base concepts (type system, loops, conditional statements and user notification) which appear in every programming language, these can be considered as the building blocks of programming and without these it would be quite hard to build applications in the 21st century.

Gepostet 18 Februar, 2016

Greg Bogdan

Software Engineer, Blogger, Tech Enthusiast

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript. I like technical writing and have good experience in creating tutorials and how to technical articles. I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...

Nächster Beitrag

26 Brilliant Facebook Banners to Get Inspired By