User Tools

Site Tools


k10:k10.2:start

10.2 Selection - Case discrimination or selection

Often, statements or a sequence of statements in a statement block must be executed under specific conditions. A classic example is the calculation of the solutions of a quadratic equation in the general form a·x²+b·x+c = 0, which is first transformed into the normal form x²+p·x+q = 0, in order to make the sample according to Vieta easy for the two solutions x1 and x2 with x1+x2 = -p and x1·x2 = q. Afterwards, their solution variety is determined by means of the discriminant D with D = (p²/4-q). One arrives at exactly 3 distinguishable cases:

  • 1st case: D > 0 → 2 different real solutions or
  • 2nd case: D = 0 → 2 equal real solutions (double solution) or
  • 3rd case: D < 0 → no real solutions, but 2 conjugates complex solutions.

By using the component gb. complex you can quickly implement the above mentioned approach:

Public Function CalculateRoots(fP As Float, fQ As Float) As Variant[]
  Dim fDiskriminante As Float = 0
  Dim fX1, fX2 As Variant
  Dim fXC1, fXC2 As Complex
 
  fDiskriminante = (fP * fP) / 4 - fQ
 
  Select Sgn(fDiskriminante)
    Case 1 ' D>0
       fX1 = - fP / 2 - Sqr(fDiskriminante)
       fX2 = - fP / 2 + Sqr(fDiskriminante)
       Return [fX1, fX2]
    Case 0 ' D=0
       fX1 = - fP / 2
       fX2 = fX1
       Return [fX1, fX2]
    Case Else ' D<0      
       fXC1 = Complex(- fP / 2, - Sqr(- fDiskriminante))
       fXC2 = fXC1.Conj()
       Return [fXC1, fXC2]
  End Select
 
End

This chapter 10.2 describes the following control structures:

  • 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
  • Multiple selection 3 → Choose

Download

Articles

Download


10.2 Auswahl – Fallunterscheidungen oder Selektion

Oft müssen Anweisungen oder eine Folge von Anweisungen in einem Anweisungsblock unter ganz bestimmten Bedingungen ausgeführt werden. Ein klassisches Beispiel ist die Berechnung der Lösungen einer quadratischen Gleichung in der allgemeinen Form a·x²+b·x+c = 0. Diese wird zuerst in die Normalform x²+p·x+q = 0 transformiert, um die Probe nach Vieta für die beiden Lösungen x1 und x2 mit x1+x2 = -p und x1·x2 = q einfach zu gestalten. Danach bestimmt man deren Lösungsvielfalt mit Hilfe der Diskriminante D mit D = (p²/4-q). Man kommt auf genau 3 unterscheidbare Fälle:

  • 1. Fall: D > 0 → 2 verschiedene reelle Lösungen oder
  • 2. Fall: D = 0 → 2 gleiche reelle Lösungen (Doppellösung) oder
  • 3. Fall: D < 0 → Keine reellen Lösungen, sondern 2 konjugiert komplexe Lösungen.

Mit dem Einsatz der Komponente gb.complex können Sie den o.a. Ansatz schnell realisieren:

Public Function CalculateRoots(fP As Float, fQ As Float) As Variant[]
  Dim fDiskriminante As Float = 0
  Dim fX1, fX2 As Variant
  Dim fXC1, fXC2 As Complex
 
  fDiskriminante = (fP * fP) / 4 - fQ
 
  Select Sgn(fDiskriminante)
    Case 1 ' D>0
       fX1 = - fP / 2 - Sqr(fDiskriminante)
       fX2 = - fP / 2 + Sqr(fDiskriminante)
       Return [fX1, fX2]
    Case 0 ' D=0
       fX1 = - fP / 2
       fX2 = fX1
       Return [fX1, fX2]
    Case Else ' D<0      
       fXC1 = Complex(- fP / 2, - Sqr(- fDiskriminante))
       fXC2 = fXC1.Conj()
       Return [fXC1, fXC2]
  End Select
 
End ' CalculateRoots(fP As Float, fQ As Float) As Variant[]

In diesem Kapitel 10.2 werden die folgenden Kontrollstrukturen beschrieben:

  • 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

Download

Artikel

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/k10.2/start.txt · Last modified: 02.07.2018 (external edit)

Page Tools