What is the main logic in programming

Systematically from the start

Introduction to Programming - Part 1: Basics of Programming

It is often difficult for beginners to get started with programming or, even better, with the entire process of software development. Questions about the choice of the right programming language or which development environment is most suitable need to be answered. In a multi-part series, we present a comprehensive introductory course in software development with a focus on the actual programming, i.e. the implementation.

Every beginning is difficult! In fact, this statement also applies to a limited extent to entry into professional software development. In a comprehensive series of articles (see box "Article series") we present an introduction to software development for the discerning reader. We are explicitly aimed at beginners who do not yet have extensive knowledge of programming (implementation) collected from software. Our target group includes, for example, computer science students at the beginning of their studies or related disciplines who would like to acquire practical knowledge in program development. The interested autodidact will also find a compact introduction here. The focus of the papers is clearly on implementation. When considering this technical discipline, it is not neglected that the actual programming is only a sub-step of the entire development cycle (Fig. 1) represents. We will point this out at the appropriate places and name sources for a deeper introduction.

Fig. 1: The software development cycle comprises several phases; our focus is on implementation

Introduction to programming - the series

Part 1: Introduction: program development, languages, development environment
Part 2: Basics: Variables, data types, process structures, algorithms
part 3: Object orientation: classes, properties, methods, events, inheritance
Part 4: User Interface: Design from a technical perspective
Part 5: Architecture: application layers and coupling, model-view-controller pattern
Part 6: Data: database theory and data modeling

programming

The goal of any software development process is to provide programs. In principle, it does not matter which system such a computer program should run on. In addition to the classic programs for the desktop, applications for the so-called mobile devices (smartphones, tablets) are playing an increasingly important role today. A programming language is used to develop computer programs. The starting point is always a problem in the real world. For example, calculations should be carried out or customer data should be managed systematically. Computer games also fall into this category. The problem must be analyzed and thus made accessible to a solution. Algorithms are used for this. An algorithm is defined as follows [1]: "An algorithm is a detailed and explicit rule for solving the problem step by step“.

The following properties apply:

  • The algorithm is executed in individual steps
  • Each step consists of just one simple basic action
  • At all times it must be clear which step is to be carried out next

Then the problem is written in a programming language. One also speaks of the source code (source program, source code). Programming languages ​​are more or less geared towards the needs of the problem and thus allow a problem-oriented formulation of the solution. The technology of the computer is largely abstracted. The computer itself only understands machine language (a sequence of 0 and 1). The translation takes place between the source program and the machine program. This process is done by the compiler. Ultimately, the entire process of programming can also be understood in analogy to the communication process between people. For example, as an adult, one explains a rather complex situation in the real world in a child-friendly manner, only with the aid of the child's vocabulary (Fig. 2).

Fig. 2: Human-machine communication via a programming language [2]

The translator (compiler) thus determines for which target systems (hardware and software) the program is created. He is among other things. matched to the operating system and processor (Fig. 3).

Fig. 3: Principle of the translation of programs [2]

What language?

There is only a limited answer to this question. First of all: There is no ideal - for all purposes - programming language. Basically, the individual languages ​​can be classified according to the concepts they support. Depending on the problem, one or the other language is better suited, it is supported more comprehensively by the selected operating system, or the tools available for it (development environments) are particularly powerful. The historical development of programming languages ​​also plays a major role to this day. Some languages ​​have established themselves universally and show a wide range of concepts, other languages ​​have reacted to the latest developments and have thus deliberately broken with "legacy". A (incomplete) overview of known and at the same time historical programming languages ​​is shown in Table 1.

And which language do we choose now? Anyway ... well not quite! In order to make the entry into program development as easy as possible, you should choose a universal language with good tool support, a multitude of possible uses and sufficient distribution. Modern concepts should also be supported. Once you have understood the basics of a language, it is relatively easy to learn a second or third programming language. There are always many things in common. Over time you will see the differences as an opportunity to choose your favorite for the current problem. Ultimately, however, it also remains a question of taste. We chose C #. The following reasons speak for this:

  • It is a modern language with potential for a wide variety of application types (desktop, app, web)
  • It is based on the powerful .NET framework, which is now the standard for the development of Windows applications
  • The language is relatively easy to learn, the complexity of C and C ++ has been left out
    • It offers the best possible support through the integrated development environment Visual Studio
  • Availability of diverse information (specialist books, internet, community)
  • Compilers and tools to get you started are freely available

Tools of the trade

The efficiency of program creation is largely defined by the quality of the available development environment. Visual Studio is used for .NET applications and C #. Visual Studio Community - currently in version 2013 - is available for a professional start. The download takes place via the website, the installation does not differ from a conventional application program.

A first program

Let's start with a first test. We create a Windows application. You will see that it is quite simple. It should be motivating that the framework code for a complete application can be created in just a few steps with the help of the integrated development environment Visual Studio. Let's start right away:

  1. Start of Visual Studio 2013.
  2. We call the assistant for a new project via the menu item File | New project on (Fig. 4).
  3. From the templates offered, we select Visual C # | Windows desktop and immediately the first entry Windows Forms Application.
  4. The name of the project and the storage location must be specified in the lower part of the dialog box. In any case, it makes sense to create your own folder.
  5. The project is created by clicking the OK button. After a few moments this process is completed and you are taken directly to the design mode of the only form of the future application (Fig. 5).
  6. There is no longer any obstacle to a first start. To do this, click Start directly on the Visual Studio toolbar. The application will then be executed immediately. Of course, only an empty window is displayed, but it already fulfills all the typical properties of a Windows application: the window can be moved, minimized, maximized or closed. The system menu is also available.

Fig. 4: Selecting a new project in Visual Studio

Fig. 5: Application form - generated automatically by Visual Studio

Now we want to expand our first application. To demonstrate the basic aspects, we will add some text fields to the interface. Numbers can later be entered into these text fields, with which we carry out simple calculations and return the result to another text field. The following steps are necessary:

  1. Drag three from the Toolbar (View | Toolbar) TextBoxControls and a Button on the form.
  2. To label the text fields, we add three additional elements of the label type.
  3. The properties editor (View | Properties window or the F4 key) can be used to adapt the properties of each control element as required. For the control elements placed in the form, we make the setting according to Table 2.
  4. When you click on the button, the numerical values ​​recorded in the text boxes number 1 and number 2 are to be added and the result is to be output in the text box. Code is to be written for this. Below the View menu item, the Code and Designer entries are used to switch between the graphical editor for the form and the source text editor. An event must be assigned to it so that an action is triggered when the button is clicked. To do this, select the button and click on the symbol for the events in the properties editor. You choose the event Clickby double-clicking with the left mouse button next to the word "Click" in the empty field. The source text editor opens automatically.
  5. The code for the calculation must be stored in the so-called event handling method. In our example, the event handling method is called button1_Click (...). To do this, enter the code according to Listing 1. You do not need to understand all of the instructions just yet. Some explanations will be given in the next section of the text, the full connection will only become clear in the course of the further contributions.
  6. If you have entered the source text correctly (no highlighting for error messages), you can then restart the application and try it out. You can now add two whole numbers and the result will be displayed after clicking on Calculate.
Listing 1 using system; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 {public partial class Form1: Form {public Form1 () {InitializeComponent (); } private void button1_Click (object sender, EventArgs e) {int number1; int number2; int result; Number1 = int.Parse (textBoxNumber1.Text); Number2 = int.Parse (textBoxNumber2.Text); Result = number1 + number2; textBox Result.Text = Result.ToString (); }}}

A first analysis

This section provides a brief analysis of the source code from Listing 1:

  • Lines 1 through 9 contain using-Instructions (e.g. using System). This loads libraries that are required for programming. By default, Visual Studio has already made a few useful suggestions. Initially, no further adjustments are necessary here.
  • Each statement must end with a semicolon.
  • Line 11: Each program code must be assigned to an area. These areas are called namespace designated. When the projects become larger, the namespaces are used to structure the source code later. Here, too, Visual Studio makes a suggestion. Such areas are inserted in curly brackets (line 12 and line 32).
  • Other related areas must also be enclosed in curly brackets. Areas are to be nested into one another.
  • Line 13: C # is an object-oriented language; H. all instructions must be given within classes (the class concept is presented in detail in Part 3). Basically, the form is also a class named Form1. All classes belong to a certain type (here of type shape).
  • To the class Form1 belongs inter alia the event handling method button_Click (...) (Line 20 to line 30). Lines 22 to 28 are important now)
  • Line 22: Definition of a variable (used to store values ​​of a certain type) with the name Number1. The guy is int (integer = whole number).
  • Line 23: Definition of a variable with the name Number2. The guy is back int.
  • Line 24: Definition of a variable with the name Result. The guy is back int.
  • Line 25: Assignment of the value from the text box (textBoxNumber1) into the variable Number1. The entered value is in the textProperty of the text box and it is a character string. This string must be converted into a number. This is done using the method int.Parse (...).
  • Line 26: Like line 25 for the variable Number2.
  • Line 27: Execution of the addition and assignment of the result to the variable Result.
  • Line 28: The calculation result from the variable Result is in the text box (textBoxResult) is displayed. To do this, it must be preceded by a number (type int) can now be converted into a character string. The method does this ToString ().

Comprehensive explanations of classes, their components and data types will be given in the coming articles of the article series.

Our basis: .NET

Windows applications are mostly developed on the basis of the so-called .NET framework. The general process of program creation described above (source code => compile => machine code) is changed slightly when using a .NET programming language. During compilation, the program code is translated into an intermediate language, the so-called MSIL code (Microsoft Intermediate Language Code). This intermediate code is the same for all programming languages ​​used. Only when the program is executed is the MSIL code translated into machine code using the just-in-time (JIT) compiler (Fig. 6).

Fig. 6: Basic concept of the JIT compiler [3]

In order for a .NET application to run on the computer, it is necessary that the .NET runtime environment (in the current version) is available on the target computer. This is automatically the case with all current Windows versions. The .NET framework is a design and runtime environment. The main components are [3]:
  • Common Language Specification (CLS): .NET applications can be developed in different languages ​​(Visual Basic .Net, C #, F #,…). In order to generate a uniform intermediate code (MSIL) from these different approaches, guidelines and standards must be defined that a .NET language must adhere to. This ensures that several languages ​​can be used within a project.
  • Common Language Runtime (CLR): The runtime environment - it is the basis for .NET programs to be executed on the target computer.
  • .NET class library: A large number of classes (with each version the number of classes has increased) are made available that can be used in the programs. For almost every application there are prepared classes that support effective program creation.
  • Compiler for the various programming languages, e.g. B. for C #, VB.Net, F # etc.

You will quickly become familiar with the details of the .NET class library (functionality, structure) in the course of programming.

Fig. 7: Hand sketch of the UI for the sample task

Conclusion, homework and outlook

You learned about programming, programming languages ​​and program development in general. We have also created an initial test program - to a certain extent for motivation. In the next part of the series of articles we will deal with the core elements of program development. We look at the most important elements of an algorithm. Starting with data types and variables, we will then deal with the flow structures of a computer program. Many of these things have been virtually unchanged since the beginning of programming. But there are also new technical developments. Until then, we would like to encourage you to browse curiously and to deal with the content of this first introductory part with interest. You will find a small exercise in the text box ("Homework") so that you can check your level of knowledge. The solution is available online.

Homework
Design a small program with the following input fields (Fig. 7):
  • Text box for entering the article name
  • Text box for entering the quantity ordered
  • Text box for entering the net price per piece
  • Text box for displaying the total net price (quantity x price)
  • Text box for displaying the total gross price (total net price x 19% VAT)
  • Text box for displaying the VAT amount
  • A button ("Calculate") to trigger the calculation process
  • All text boxes must be supplemented with a label.

Proceed as follows:

  1. Start with a new project (project type: Windows Forms application).
  2. Design the user interface form. Design this a little too.
  3. Implement the code in the button's Click event. Note: use the data type float or double for the prices.
  4. Test the application.
  5. What happens if you make a mistake (e.g. enter text instead of a number)?
  6. Familiarize yourself with other controls on the toolbar. In particular, experiment with the elements from the General Controls area. Drag the individual elements onto the form of the dialog box, arrange the elements according to your requirements and try them out by setting different properties using the properties editor (View | Properties window or the F4 key). You can restart the application at any time. You don't need to write any additional code.

Links & Literature:

[1] Gumm, H.-P. and Sommer, M .: "Introduction to Computer Science", Oldenbourg-Verlag, 2013
[2] Balzert, H .: "Textbook - Basics of Computer Science", Spektrum Akademischer Verlag, 2005
[3] Profitus, T. and Doberenz, W .: "The Visual C # Programmer", Carl Hanser Verlag, Munich, 2009

Developer magazine

This article was published in the developer magazine.

Of course, you can also read the developer magazine digitally in the browser or on your Android and iOS devices via the developer.kiosk. The Developer Magazine is also available in our shop by subscription or as a single issue.

Lead image: Rows of butterfly cocoons via Shutterstock / Copyright: Ksenia Ragozina

Our editorial team recommends: