Decidere quale eseguire tra tante sequenze alternative di istruzioni
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. |
 |
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.
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. |
 |
Codifiche
Si può semplificare il codice, e rimanere più aderenti al primo diagramma di flusso, cambiando l’indentazione delle istruzioni ed eliminando le etichette di blocco superflue.
|
Top… |
…down |
Semplificato |
Una sola istruzione |
C… |
|
if(E == E1) { istr11; istr12; } else { // ... } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
if(E == E1) { istr11; istr12; } else { if(E == E2) { istr21; istr22; } else { istr31; istr32; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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 Istr11; Istr12; End Else Begin (* ... *) End; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
If E = E1 Then Begin Istr11; Istr12; End Else Begin If E = E2 Then Begin Istr21; Istr22; End Else Begin Istr31; Istr32; End End; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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: Istr11 Istr12 else: # ... |
|
|
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 Istr11 Istr12 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 |
|
Note
- Nei linguaggi C… e Python: ==
- Il valore assunto dell’unica espressione E decide quale blocco verrà eseguito
- Un solo blocco sarà eseguito in esclusiva
- Prima dell’Else finale possono essere inseriti ulteriori blocchi di tipo Else If (con E3, E4, …)
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 |
 |
Codifiche
|
…down |
Semplificato |
Una sola istruzione |
C… |
|
if(E == E1) { istr11; istr12; } else { if(E == E2) { istr21; istr22; } } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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 Istr11; Istr12; End Else Begin If E = E2 Then Begin Istr21; Istr22; End; End; |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
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 istr11 istr12 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 Istr11 Istr12 Else If E = E2 Then Istr21 Istr22 EndIf EndIf |
|
|
If E = E1 Then Istr11 Istr12 ElseIf E = E2 Then Istr21 Istr22 EndIf |
|
|
If E = E1 Then Istr1 ElseIf E = E2 Then Istr2 ... ... ElseIf E = En Then Istrn EndIf |
|
VISUAL
BASIC |
|
If E = E1 Then Istr11 Istr12 Else If E = E2 Then Istr21 Istr22 End If End If |
|
|
If E = E1 Then Istr11 Istr12 ElseIf E = E2 Then Istr21 Istr22 End If |
|
|
If E = E1 Then Istr1 ElseIf E = E2 Then Istr2 ... ... ElseIf E = En Then Istrn End If |
|