Selezione multipla

Decidere quale eseguire tra tante sequenze alternative di istruzioni

Selezione tripla

Dopo aver eseguito Istruzione 0 decide quale blocco eseguire in funzione del valore assunto dall’espressione E:

Se E = E1 esegue

  • Istruzione 11
  • Istruzione 12

altrimenti se E = E2 esegue

  • Istruzione 21
  • Istruzione 22

altrimenti esegue

  • Istruzione n1
  • Istruzione n2

e poi continua eseguendo Istruzione n+1.

Il valore assunto dell’unica espressione E decide quale blocco verrà eseguito.
Un solo blocco sarà eseguito in esclusiva.


Non è necessario introdurre un costrutto di controllo a più uscite perché il diagramma di flusso si può trasformare nel successivo (esempio con 3 scelte…) dove cambia l’interpretazione ma non il comportamento dell’algoritmo.

Selezione tripla

Dopo aver eseguito Istruzione 0 decide se eseguire

  • Istruzione 11
  • Istruzione 12

oppure continua e quindi decide se eseguire

  • Istruzione 21
  • Istruzione 21

oppure

  • Istruzione 31
  • Istruzione 32

e quindi continua eseguendo Istruzione 4.

Prima del No finale possono essere inseriti ulteriori blocchi di tipo selezione doppia (con E3, E4, …).

C, C++, Java, …

if(E == E1)
{
    istr11;
    istr12;
}
else 
{
    // ... 
}
if(E == E1)
{
    istr11;
    istr12;
}
else
{
    if(E == E2)
    {
        istr21;
        istr22;
    }
    else
    {
        istr31;
        istr32;
    }
}
if(E == E1)
{
    istr11;
    istr12;
}
else if(E == E2)
{
    istr21;
    istr22;
}
...
...
else
{
    istrn1;
    istrn2;
}
if(E == E1)
    istr1;
else if(E == E2)
    istr2;
...
...
else
    istrn;

Pascal

If E = E1 Then
   Begin
     Istr_11;
     Istr_12;
   End 
Else
   Begin
     (* ... *)
   End;
If E = E1 Then
  Begin
    Istr11;
    Istr12;
  End
Else
  Begin
    If E = E2 Then
      Begin
        Istr21;
        Istr22;
      End
    Else
      Begin
        Istr31;
        Istr32;
      End
  End;
If E = E1 Then
  Begin
    Istr11;
    Istr12;
  End
Else If E = E2 Then
  Begin
    Istr21;
    Istr22;
  End
...
...
Else
  Begin
    Istrn1;
    Istrn2;
  End;
If E = E1 Then
    Istr1
Else If E = E2 Then
    Istr2
...
...
Else
    Istrn;

Python

if E == E1:
    Istr_11
    Istr_12
else:
    pass
if E == E1
    istr11
    istr12
else:
    if E == E2:
        istr21
        istr22
    else:
        istr31
        istr32
if E == E1:
    istr11
    istr12
elif E == E2:
    istr21
    istr22
...
...
else:
    istrn1
    istrn2
if E == E1:
    istr11
elif E == E2:
    istr21
...
...
else:
    istrn1

Small Basic

If E = E1 Then
   Istr11
   Istr12
Else
   ' ...
EndIf
If E = E1 Then
   Istr11
   Istr12
Else
   If E = E2 Then
      Istr21
      Istr22
   Else
      Istr31
      Istr32
   EndIf
EndIf
If E = E1 Then
   Istr11
   Istr12
ElseIf E = E2 Then
   Istr21
   Istr22
...
...
Else
   Istrn1
   Istrn2
EndIf
If E = E1 Then
   Istr1
ElseIf E = E2 Then
   Istr2
...
...
Else
   Istrn
EndIf

Visual Basic

If E = E1 Then
    Istr_11
    Istr_12
Else
    ' ...
End If
If E = E1 Then
    Istr11
    Istr12
Else
    If E = E2 Then
        Istr21
        Istr22
    Else
        Istr31
        Istr32
    End If
End If
If E = E1 Then
    Istr11
    Istr12
ElseIf E = E2 Then
    Istr21
    Istr22
...
...
Else
    Istrn1
    Istrn2
End If
If E = E1 Then
    Istr1
ElseIf E = E2 Then
    Istr2
...
...
Else
    Istrn
End If

Else facoltativo

Se è prevista anche l’eventualità che nessun blocco venga eseguito allora il controllo del valore di E avverrà fino alla fine e non comparirà l’Else finale

C, C++, Java, ..

if(E == E1)
{
    istr_11;
    istr_12;
}
else
{
    if(E == E2)
    {
        istr21;
        istr22;
    } 
}
if(E == E1)
{
    istr11;
    istr12;
}
else if(E == E2)
{
    istr21;
    istr22;
}
...
...
else if(E == En) 
{ 
    istrn1; 
    istrn2; 
}
if(E == E1)
    istr1;
else if(E == E2)
    istr2;
...
...
else if(E == En) 
    istrn;

Pascal

If E = E1 Then
  Begin
    Istr_11;
    Istr_12;
  End
Else
  Begin
    If E = E2 Then
      Begin
        Istr21;
        Istr22;
      End;
  End;
If E = E1 Then
  Begin
    Istr11;
    Istr12;
  End
Else If E = E2 Then
  Begin
    Istr21;
    Istr22;
  End
...
...
Else If E = En Then 
  Begin 
    Istrn1; 
    Istrn2; 
  End;
If E = E1 Then
    Istr1
Else If E = E2 Then
    Istr2
...
...
Else If E = En Then 
    Istrn;

Python

if E == E1:
    istr_11
    istr_12
else:
    if E == E2:
        istr21
        istr22
if E == E1:
    istr11
    istr12
elif E == E2:
    istr21
    istr22
...
...
elif E == En: 
    istrn1 
    istrn2
if E == E1:
    istr1
elif E == E2:
    istr2
...
...
elif E == En: 
    istrn

Small Basic

If E = E1 Then
    Istr_11
    Istr_12
Else
    If E = E2 Then
        Istr_21
        Istr_22
    End If
EndIf
If E = E1 Then
    Istr_11
    Istr_12
ElseIf E = E2 Then
    Istr_21
    Istr_22
EndIf
If E = E1 Then
    Istr_1
ElseIf E = E2 Then
    Istr_2
...
...
ElseIf E = En Then 
    Istr_n
EndIf

Visual Basic

If E = E1 Then
    Istr_11
    Istr_12
Else
    If E = E2 Then
        Istr_21
        Istr_22
    End If
End If
If E = E1 Then
    Istr_11
    Istr_12
ElseIf E = E2 Then
    Istr_21
    Istr_22
End If
If E = E1 Then
    Istr_1
ElseIf E = E2 Then
    Istr_2
...
...
ElseIf E = En Then 
    Istr_n
End If