What is the programming language processing

Chapter 1: Programming languages

General basics of programming

This book is licensed under a Creative Commons license.

1.1 Structure and components

Information processing tools

If you take a close look at programming languages ​​and compare them with one another, you will quickly see that there are great similarities. Programming languages ​​consist of similar components. Once you have understood why these components exist and what their purpose is, it is no longer a problem to learn new programming languages ​​later. The syntax, i.e. the technical terms, differ from programming language to programming language, but the underlying concepts that matter - with a few exceptions, of course - are not.

Why do you need programming languages? What is the point and purpose of programming languages? Programming languages ​​are needed to develop computer applications. In general, computer applications can be reduced to the lowest common denominator: Computer applications process information. It doesn't matter whether you are dealing with a browser, a word processor, a computer game or a control system for nuclear power plants, in the end it is simply about processing information.

In order to develop computer applications that can process information, appropriate tools are required. Programming languages ​​provide these tools.

Information can only be processed meaningfully if information can also be saved. Computer applications must be able to memorize information, for example in order to be able to access previously stored information later in the course of the program. If, for example, any complicated calculations would lead to an intermediate result, which is followed by other calculations in the program before the intermediate result is used later, this intermediate result must be recorded somewhere in the program later. Without saving information, calculated results would be permanently lost. The tool that all programming languages ​​make available to temporarily store information in a program is called variables.

While variables only allow information to be stored, programming languages ​​must provide even more tools - because computer applications should not just store information, but process it. Each programming language therefore provides a whole range of predefined operators. These operators are more or less cryptic characters that are assigned a very specific meaning in the programming language. Variables can now be linked with these operators. Depending on the operator, this link is carried out one way or another and eventually leads to a new result. The information in the two linked variables is calculated by the operator and can then be stored in a third variable, for example.

You can do a lot with variables and operators and develop real computer applications - for example a mathematical program that adds numbers. However, if you want to write software that asks the user to enter a password and, depending on the password, allows access to protected files, variables and operators are not sufficient to write such a program. The problem is, you have a choice to make in your program. Depending on the password entered, access to the files must be permitted or not - that depends entirely on whether the user knows the correct password. Fortunately, programming languages ​​now also offer a tool that checks conditions and executes different code as a function of these. This tool is called control structures. With control structures, conditions can be checked, for example the input of the user to see whether it matches the actual password. With control structures, branches in the code are possible - code can be skipped or even repeated. Programs no longer run stupidly from top to bottom, but branch out depending on the conditions that are checked in control structures.

Variables, operators and control structures - you don't actually need them anymore to develop computer applications. Unfortunately, software development is quite a complicated story. The larger the programs to be developed, the more difficult it becomes to write error-free code. The complexity in programs that consist of thousands of lines of code can hardly be grasped by the human programmer. Programming languages ​​therefore offer another tool to get the complexity under control. The code that the programmer develops can be broken down into small, neat bits. So instead of writing the entire program in one fell swoop, it is broken down into small modules, which can then be easily surveyed and understood. The tool at issue is called a function in programming languages. Functions structure the code and make it clearer.

There are programming languages ​​that are finished at this point. They offer variables, operators, control structures and functions and thus everything you need to develop full-fledged computer applications. Such programming languages ​​are called procedural programming languages. Procedure is another term for function and refers to the fact that the highest and best design element in these programming languages ​​is precisely the functions. Well-known procedural programming languages ​​are C, Pascal and Basic.

Unfortunately, the problem with modern software development today is that it is so immensely complex that functions as structuring agents are just a drop in the ocean. Applications that consist of millions of lines of code require better structuring options so that the programs can still be developed in a meaningful way. In the past few years, object orientation has prevailed and a tool has been added to the procedural programming languages. Code is no longer clearly structured with the help of functions, but with classes and objects. Object orientation is the holy grail today and is the most powerful tool we currently have for developing software. For example, computer applications such as Microsoft Internet Explorer or the programs from Microsoft Office are all programmed in an object-oriented manner. Well-known object-oriented programming languages ​​are C ++, Java and Smalltalk.

In the following you will get to know the various components of programming languages ​​a little better. Using the Javascript programming language, you will be shown how to actually deal with variables, operators, control structures and functions and what you really have to imagine when programming.

1.2 variables

Information pots

To be able to save information in programs, you need special pots: Variables. As a programmer, you can conjure up as many variables as you want at any time. Each variable represents its own container in which you can store information and retrieve it later in the course of the program. In the Javascript programming language, proceed as follows to create a variable, i.e. to conjure up a pot.

var pot;

With this one line you conjure up a variable called. You can now access this variable in your program at any time and save information in it. In many programming languages, variables are always created according to the same scheme: First you specify a data type. In this example the data type is. The data type defines what kind of information can be stored in the variable. There are programming languages ​​in which you need a specific pot depending on the type of information you want to store. These programming languages ​​include, for example, C, C ++ and Java. In Javascript - the programming language in which the examples in this book are written - there is only one data type. In Javascript for a variable, it does not matter what kind of information you want to store in it. A JavaScript variable can, for example, store a letter or a number or a word. In another programming language, you might actually need a pot that can store letters for a letter, and another type of pot that can store numbers for a number. The data type expresses nothing else - it defines what kind of information can be stored in a variable.

The data type is followed by a variable name. Variables must be given names so that they can be accessed and identified at any time in a program. In the example above, the variable was simply given the name. Note that Javascript, like many other programming languages ​​such as C, C ++, and Java, is case-sensitive.

The semicolon that is specified at the end of the line is also typical for many programming languages. A statement is ended and concluded with the semicolon. While Javascript does not take the semicolon very carefully and sometimes ignores it if it is missing, semicolons must be set in C, C ++ and Java.

While there is only one data type in the Javascript programming language, as just seen, in C ++, for example, a variable that should be able to store numbers must be given the data type.

int number pot;

However, if you need a variable in C ++ that is to be able to store a word, you have to give the variable the data type.

string word pot;

C ++ is a strictly typed programming language. This means that variables do not have a general data type as in Javascript and can store any type of information, but they have a very specific data type and can therefore only store a very specific type of information.

The advantage of the strictly typed programming languages ​​is that programming errors are prevented. It is not possible in C ++ to accidentally save a word in the variable created above - this does not work due to the data type. The disadvantage, however, is that when you create variables you have to be clear about what kind of information you want to save later. It actually depends on which data type you have to specify.

1.3 operators

Link and process information

You now know how to create variables. You can now create as many variables as you want at any time and save information in them. Storing information is not enough, however. After all, you want your application to process information, such as performing a calculation and adding two numbers. You can use variables to store numbers, but how do they add up?

Information processing operations such as the addition of numbers are possible through operators. Every programming language offers operators - without operators, no information can be processed. Most programming languages ​​have similar operators, each with the same meaning. If you know, for example, how the operators in Javascript work, then you actually already know almost all operators from Java, C and C ++. In the following you can see a few operators of the Javascript programming language in action.

var i = 1, j = 2, k; k = i + j; k = i - j; k = i * j; k = i / j; k + = j; k - = i; i << j; k = i> j; k = (i + j) * 2;

Operators can be structured according to their function and assigned to different groups. For example, there is a group of arithmetic operators whose purpose is to perform basic arithmetic with,, and. Just as arithmetic operators can calculate numbers, logical operators such as, and can calculate truth values. Bitwise operators like,, and work very close to the machine. These operators can be used to process the smallest units of information that the computer has in variables - namely bits. Another group of operators - the so-called comparison operators such as,, and - enable values ​​to be compared.

In addition to these operators, every programming language has a precedence table in which every operator is simply assigned a priority by definition. These precedence tables are critical in that they clearly state the order in which operators are executed when more than one is used in a single code statement. For example, the precedence table of a programming language can specify that the has a higher priority than that. This means nothing other than the rule "point before line" from mathematics: a multiplication is preferred to an addition. It makes sense that programming languages ​​such as Javascript, Java, C and C ++ also provide for this.

The operator with the highest priority is usually the parenthesis. This means that parentheses are always preferred and carried out first. This also means that the parentheses can be used to change the order in which operators are executed, as specified by the precedence table. As in mathematics, an addition can be carried out first by using suitable brackets, before the sum is then multiplied by another value.

1.4 Control structures

Execute different code depending on conditions

With the help of variables and operators, you can now process information such as numbers. To date, however, this processing has only been carried out linearly. The computer works through your instructions in the program from top to bottom, each instruction exactly once. However, if you want to develop password protection, for example, access to the secured resource must either be granted or not, depending on the input of the user. Branches of the program flow are possible with control structures. The following three code examples are also written in the Javascript programming language.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var passwort = "abc"; var eingabe = prompt("Geben Sie ein Passwort ein."); if (eingabe == passwort) { alert("Passwort ok!"); } else { alert("Passwort falsch!"); } </script> </head> <body> </body> </html>

The - statement enables a simple check of a condition. If the condition is true, the statement block, which is defined by the curly braces, is executed after the. If the condition is false, the statement block after the is executed. If the respective statement block has been executed, program execution continues after the - statement.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var passwort = "abc"; var eingabe = prompt("Geben Sie ein Passwort ein."); while (eingabe != passwort) { eingabe = prompt("Das Passwort ist falsch. Bitte nochmal probieren."); } </script> </head> <body> </body> </html>

The statement is what is known as a loop. In addition to the loop, programming languages ​​also offer other loops such as the loop or the loop. However, all of these loops work in a similar way: they execute a block of statements repeatedly, as long as a condition to be checked is true.

A programming error called an infinite loop often occurs in connection with loops. An infinite loop is a loop that is repeated an infinite number of times because the condition to be checked always remains true. Endless loops are noticeable in that the program appears to have stopped and no longer reacts to user input.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> var passwort = "abc"; var eingabe = prompt("Geben Sie ein Passwort ein."); while (eingabe != passwort) { eingabe = prompt("Das Passwort ist falsch. Bitte nochmal probieren."); if (eingabe == "ende") { break; } } </script> </head> <body> </body> </html>

Using an additional keyword such as, it is also possible to exit loops prematurely. The loop does not end because the condition to be checked is no longer true, but it ends immediately when the code execution hits.

1.5 functions

Instruction blocks that can be called at any time

You have already heard the term statement block in connection with control structures. An instruction block is limited in the programming languages ​​Javascript, C, C ++ and Java by the curly brackets and. Together with the instructions contained therein, they form an instruction block that can, for example, follow the keyword.

Instruction blocks can also be assigned names. An instruction block can be called using this name. This will execute it. An instruction block that can be called in this way is called a function.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> function hallo() { alert("Hallo!"); } hallo(); </script> </head> <body> </body> </html>

The code example above shows how functions look in Javascript. The name of the function follows the keyword. If necessary, parameters are given in brackets after the name. Otherwise the brackets remain empty. In the function body - i.e. in the instruction block - all instructions are now defined that are to be executed when the function is called.

Parameters are input values ​​that can be passed to a function when it is called and that are processed internally in the function. Not every function requires input values. The function in the example above simply displays a message on the screen so that it is not necessary to pass any parameters to this function.

If you want to access a function defined with, you call it using the function name. To do this, enter the name of the function and, after that, in brackets, any parameters to be transferred. Since the function does not expect any parameters, none is specified between the brackets when called. The brackets are still important and must not be left out in order to clearly identify a function call and not a variable.

<html> <head> <title>Allgemeine Grundlagen der Programmierung</title> <script type="text/javascript"> function add(a, b) { alert(a + b); } var i = 1, j = 2; add(i, j); </script> </head> <body> </body> </html>

In the Javascript code above, a function is now defined that takes two parameters and expects.This means that when the function is called, two parameters must also be passed in brackets. In the example these are the variables and. By calling the function and transferring the two variables, the sum of the transferred values ​​is formed within the function. and ultimately only represent placeholders for the values ​​that are specified when the function is called.

If you look closely at the above example, you will notice that, among other things, a function is called. This function is a function built into Javascript and is automatically available to the programmer in Javascript. With, a small window opens on the screen, which shows what was transferred as a parameter of the function - in the above case, the result of the addition.

1.6 structures

Data blocks

Just as several instructions can be packed together to form functions, it is also possible to combine several variables to form a structure. However, this option does not exist in the Javascript programming language, so the following code example is taken from the C ++ programming language.

struct address {string first name; string surname; string street; int zip code; string place; }; address myaddress;

By creating a structure, it is possible to use data types in your program that do not yet exist. Since C ++ does not automatically provide a data type for storing addresses, you can now create this data type yourself thanks to structures. Once created, you can create a tag from this new data type as usual. In the example above is the name of the variable. And this variable can now store information of the type.

1.7 Other ingredients

Of classes and objects

The components of a programming language that have become known in this chapter can be found in almost every language. In addition, programming languages ​​sometimes offer elements such as classes and objects. These programming languages ​​include, for example, Smalltalk, C ++ and Java. These are object-oriented programming languages. In programming languages ​​of this type, objects are used that enable program development that is closer to the description of the problem. With less abstraction from the problem to the solution program, larger applications in particular can be developed more easily, which would become too confusing due to a lack of class and object support.

Object-oriented programming languages ​​are currently the holy grail of programming languages. They are the most powerful tool with which software can be developed today. Most large and successful applications, such as Microsoft's Internet Explorer or all Microsoft Office programs, are programmed in an object-oriented manner. However, the object orientation has also lost some of its luster. Years ago it was considered the ultimate solution to all problems in software development, but one still has to recognize that it is not a panacea, but can also be improved. Of course, programming languages ​​and management methods in software development are also evolving, so that here too it is certainly not all evening.

1.8 Language Development

From proprietary languages ​​and open standards

For the most part, the development of programming languages ​​is very intertwined. The programming languages ​​C, C ++ and Java, which are very common today, are very similar. C was the first to appear here, and was then supplemented by object-oriented elements for C ++. Years later, the development of the Java programming language was based very closely on C ++. Javascript was developed at the same time as Java and was originally called ActiveScript. Before the first release, the language was adapted to Java - both in terms of its syntax and then of its name.

The Visual Basic language, which is particularly widespread in the area of ​​Windows operating systems, is derived from Basic. Basic itself, like C, is a rather old language, which was also represented as qbasic under the predecessor of Windows, MS-DOS. VBScript, a competing language to Javascript, is also very closely related to Visual Basic. Nowadays, Basic in all its dialects is primarily a programming language that is strongly supported and further developed by Microsoft and is therefore primarily used on Microsoft operating systems.

The vast majority of languages ​​belong to the general public, to put it somewhat casually. They are being further developed by programmers worldwide, which has not been a problem since the Internet at the latest. The standardization of the further developments is taken over by international committees, in which mostly representatives of leading software companies sit. The prominent founding fathers of the programming languages ​​often also have a few important words to say within the further development and standardization committees.

Probably the best-known programming language that belongs to a company and is not further developed by international bodies is Java. Java is a proprietary language and is owned by Sun Microsystems. If you want to use Java, you have to accept the Java license terms. Independent further development of the programming language is prohibited. In January 2001, a court process lasting several years between Sun and Microsoft ended, in which Sun accused Microsoft of not having adhered to the license terms, but of having changed the Java programming language independently. As of January 2001, Microsoft no longer supports Java.

1.9 Compiler and Interpreter

From source code to program execution

When you write small programs as in the examples above, you as a programmer are always working with the source code. The source code is the human readable program flow. While you and I understand this source code quite well, the computer cannot do anything with it. This is important because you want the computer to run your program. The computer only understands machine code. That means someone has to convert the source code of the program into machine code.

Programs called compilers are used to convert source code into machine code. Depending on the programming language and computer system used, you will need a different compiler. If your source code is written in the C ++ programming language and you want to run the program on a computer with the Intel Pentium chip, for example, you need a compiler that can convert C ++ source code into machine code for the Intel chip. If you want your C ++ source code to run on a computer with a PowerPC chip, you need a compiler that converts C ++ into machine code for the PowerPC. Compilers have to master the language of the source code on the one hand, and the machine code of the target system on the other.

In addition to compilers, there are also so-called interpreters. When you run Javascript samples, you find that you have nowhere to convert your source code to machine code. The PC seems to understand your source code automatically. This only works because the program that executes your source code first starts an interpreter that analyzes your source code. After this analysis, the program then knows what you actually want to do in your source code and executes the corresponding functions. Since a Javascript interpreter is integrated in modern browsers these days, you can usually run all Javascript examples in your browser without any problems.

Source codes that are converted into machine code with compilers only need to carry out this conversion once. Then there is an executable file that you can start immediately every time, for example by double-clicking as in Windows. One also speaks of native applications. You no longer need the source code, as the complete machine code is already in the file. And that's the only thing the pc needs.

Source code such as that in Javascript has to be re-analyzed by the program every time it is to be executed. No machine code is created here, let alone saved for further program execution at a later date. Because a new analysis is necessary every time, programs that are interpreted are slower to execute than programs that are already available as machine code. An analysis is not necessary here, the computer can immediately execute the machine code.

The Java programming language has a special position here. Source code in Java is compiled, but not for a specific target system such as an Intel Pentium chip or a PowerPC chip, but for a virtual machine. This virtual machine is ultimately a computer program that interprets the compiled and virtual machine code. By converting Java source code into virtual machine code and interpreting the virtual machine code, a higher execution speed can be achieved than with purely interpreting languages. The execution speeds of purely compiled programming languages ​​compared to Java are still ten to twenty times faster - roughly as a rule of thumb. However, by compiling the source code for a virtual machine, Java is a language with greater portability. For example, while C ++ programs are compiled either for an Intel Pentium chip or for a PowerPC chip, Java programs run on all computer systems - provided that a program exists for interpreting the virtual machine code. Java programs trade execution speed for portability.

1.10 exercises

Practice creates masters

You can purchase the solutions to all of the exercises in this book as a ZIP file.

  1. Develop a program in Javascript that asks the user to enter a password. If the password is entered incorrectly three times in a row, no further input request should appear.

  2. Develop a program in Javascript that asks the user to enter two numbers. Add the two numbers and display the result on the screen.

  3. Develop a program in Javascript that asks the user to enter two numbers and then one of the four characters +, -, / and *. Depending on the character entered, the two numbers should be added, subtracted, divided or multiplied. The result of the respective type of calculation should be displayed on the screen. Do not develop your own functions to solve the task.

  4. This time, develop the Javascript calculator of the above task using functions. A different function is to be called for each of the four types of calculation, which performs the respective calculation operation and then outputs the result on the screen.

Copyright © 2001-2010 Boris Schäling