User Tools

Site Tools


k10:start

10.0 Control structures

Every task or problem - as a task with a higher level of requirements - whose solution can be described by algorithms, can in principle be solved with the help of the thinking tool computer. This makes the direct correlation between the algorithm and the computer program obvious, which has already been clearly highlighted in chapters 4.5 and 4.6. For the program design, the algorithms for the most important procedures must be available in a suitable form of description - either as text or in another suitable form (natural language - text, pseudo code, structure diagram or also Nassi-Shneiderman diagram) - whereby the representation form (notation) is adapted to the respective task.

When displaying algorithms, you will notice that each algorithm is composed of a few structure elements. These structures are called control structures because they are used to control and control complex (program) sequences. The following control structures are described in this chapter:

  • Linear instruction sequence (sequence)
  • Selection (case discrimination or selection)
  • Repeat (loop or loop)
  • Special control structures

10.0.1 Selection - Case discrimination or selection

  • One-sided selection IF… THEN… ENDIF
  • Two-sided selection 1 IIF (selector, A, B)
  • Two-sided selection 2 IF… THEN… ELSE… ENDIF
  • Multi-selection 1 IF…. THEN…[ ELSE IF.. .. ELSE IF] … ENDIF
  • Multiple selection 2 SELECT CASE… END SELECT
  • Multi-selection 3 Choose (…)

10.0.2 Repeat - Loop

  • FOR… TO … STEP… NEXT
  • FOR EACH… IN… NEXT
  • REPEAT…. UNTIL
  • WHILE… WEND
  • BREAK and CONTINUE
  • DO… LOOP
  • Recursion

10.0.3 Special control structures

  • RETURN
  • STOP
  • QUIT
  • WITH… END WITH
  • GOTO
  • ON GOTO
  • GOSUB
  • ON GOSUB
  • Application.BUSY

10.0.4 Example

The following section introduces an excerpt from the source code of a real project, in which some of the above control structures (For Each..In..Next, For..To..Step..Next, Select.Case, Recursion, If..Then..Else..Endif) the program flow is controlled and controlled in one procedure:

Public Sub ParseNode(Node As XmlNode) 
 
  Dim xmlNode, xmlAttribute As XmlNode 
  Dim hFeed As FeedItem 
  Dim iCount As Integer 
 
  FOR EACH xmlNode IN Node.Children 
    IF xmlNode.Name = "item" THEN 
       hFeed = New FeedItem(ListContainer.Count + 1, ListContainer) 
       FOR iCount = 0 TO xmlNode.Children.Max STEP 1
         xmlAttribute = xmlNode.Children[iCount] 
         SELECT CASE xmlAttribute.Name 
           CASE "title" 
             hFeed.FeedTitle = xmlAttribute.Value 
           CASE "description" 
             hFeed.Description = xmlAttribute.Value 
           CASE "link" 
             hFeed.Link = xmlAttribute.Value 
         END SELECT ' xmlAttribute.Name 
       NEXT ' iCount 
    ELSE 
       IF xmlNode.Children.Count > 0 THEN ParseNode(xmlNode) ' Recursive call!
    ENDIF ' xmlNode.Name = "item" ? 
  NEXT ' Each xmlNode 
 
End ' ParseNode(...) 

Download

10.0 Kontrollstrukturen

Jede Aufgabe oder jedes Problem – als Aufgabe mit erhöhtem Anforderungsniveau – deren Lösung durch Al­gorithmen beschrieben werden können, sind im Prinzip mit Hilfe des Denkwerkzeugs Computer lösbar. Damit wird der unmit­tel­bare Zusammenhang zwischen Algorithmus und Computerprogramm offensichtlich, der bereits in den Kapiteln 4.5 und 4.6 deutlich herausgehoben wurde. Für den Programmentwurf müssen die Algorithmen für die wichtigsten Prozeduren in einer geeigneten Beschreibungsform – entweder als Text oder in einer anderen geeigneten Form (Natürliche Sprache - Text, Pseudo-Code, Struktogramm oder auch Nassi-Shneiderman-Diagramm) – vorliegen, wobei die Darstellungsform (Notation) der jeweiligen Aufgabenstellung angepasst ist.

Bei der Darstellung von Algorithmen werden Sie feststellen, das jeder Algorithmus aus wenigen Strukturelementen aufgebaut ist. Man nennt diese Strukturen Kontrollstrukturen, weil man sie zur Kontrolle und Steuerung komplexer (Programm-)Abläufe verwendet. Folgende Kontrollstrukturen werden in diesem Kapitel beschrieben:

  • Lineare Anweisungsfolge (Sequenz)
  • Auswahl (Fallunterscheidung oder Selektion)
  • Wiederholung (Schleife oder Loop)
  • Spezielle Kontrollstrukturen

10.0.1 Auswahl – Fallunterscheidungen oder Selektion

  • Einseitige Auswahl IF … THEN … ENDIF
  • Zweiseitige Auswahl 1 IIF(Selektor, A, B)
  • Zweiseitige Auswahl 2 IF … THEN … ELSE … ENDIF
  • Mehrfach-Auswahl 1 IF … THEN … [ ELSE IF … .. ELSE IF ] … ENDIF
  • Mehrfach-Auswahl 2 SELECT CASE … END SELECT
  • Mehrfach-Auswahl 3 Choose (…)

10.0.2 Wiederholung – Schleife oder Loop

  • FOR … TO … STEP … NEXT
  • FOR EACH … IN … NEXT
  • REPEAT … UNTIL
  • WHILE … WEND
  • BREAK und CONTINUE
  • DO … LOOP
  • Rekursion

10.0.3 Spezielle Kontrollstrukturen

  • RETURN
  • STOP
  • QUIT
  • WITH … END WITH
  • GOTO
  • ON GOTO
  • GOSUB
  • ON GOSUB
  • Application.BUSY

10.0.4 Beispiel

Im folgenden Abschnitt wird ein Ausschnitt aus dem Quelltext eines realen Projekts vorgestellt, in dem mit einigen der o.a. Kontrollstrukturen (For Each..In..Next, For..To..Step..Next, Select..Case, Rekursion, If..Then..Else..Endif) der Programmablauf in einer Prozedur kontrolliert und gesteuert wird:

Public Sub ParseNode(Node As XmlNode) 
 
  Dim xmlNode, xmlAttribute As XmlNode 
  Dim hFeed As FeedItem 
  Dim iCount As Integer 
 
  FOR EACH xmlNode IN Node.Children 
    IF xmlNode.Name = "item" THEN 
       hFeed = New FeedItem(ListContainer.Count + 1, ListContainer) 
       FOR iCount = 0 TO xmlNode.Children.Max STEP 1
         xmlAttribute = xmlNode.Children[iCount] 
         SELECT CASE xmlAttribute.Name 
           CASE "title" 
             hFeed.FeedTitle = xmlAttribute.Value 
           CASE "description" 
             hFeed.Description = xmlAttribute.Value 
           CASE "link" 
             hFeed.Link = xmlAttribute.Value 
         END SELECT ' xmlAttribute.Name 
       NEXT ' iCount 
    ELSE 
       IF xmlNode.Children.Count > 0 THEN ParseNode(xmlNode) ' Rekursiver Aufruf! 
    ENDIF ' xmlNode.Name = "item" ? 
  NEXT ' Each xmlNode 
 
End ' ParseNode(...) 

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools