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
Top…
…down
Semplificato
Una sola istruzione
C…
1
2
3
4
5
6
7
8
9
if(E == E1)
{
istr_11;
istr_12;
}
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;
}
1
2
3
4
5
6
7
8
if(E == E1)
istr1;
else if(E == E2)
istr2;
...
...
else
istrn;
Pascal
1
2
3
4
5
6
7
8
9
If E = E1 Then
Begin
Istr_11;
Istr_12;
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;
1
2
3
4
5
6
7
8
If E = E1 Then
Istr1
Else If E = E2 Then
Istr2
...
...
Else
Istrn;
Python
1
2
3
4
5
if E == E1:
Istr_11
Istr_12
else:
# ...
1
2
3
4
5
6
7
8
9
10
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
if E == E1:
istr11
istr12
elif E == E2:
istr21
istr22
...
...
else:
istrn1
istrn2
1
2
3
4
5
6
7
8
if E == E1:
istr11
elif E == E2:
istr21
...
...
else:
istrn1
SMALL BASIC
1
2
3
4
5
6
If E = E1 Then
Istr11
Istr12
Else
' ...
EndIf
1
2
3
4
5
6
7
8
9
10
11
12
If E = E1 Then
Istr11
Istr12
Else
If E = E2 Then
Istr21
Istr22
Else
Istr31
Istr32
EndIf
EndIf
1
2
3
4
5
6
7
8
9
10
11
12
If E = E1 Then
Istr11
Istr12
ElseIf E = E2 Then
Istr21
Istr22
...
...
Else
Istrn1
Istrn2
EndIf
1
2
3
4
5
6
7
8
9
If E = E1 Then
Istr1
ElseIf E = E2 Then
Istr2
...
...
Else
Istrn
EndIf
VISUAL BASIC
1
2
3
4
5
6
If E = E1 Then
Istr_11
Istr_12
Else
' ...
End If
1
2
3
4
5
6
7
8
9
10
11
12
If E = E1 Then
Istr11
Istr12
Else
If E = E2 Then
Istr21
Istr22
Else
Istr31
Istr32
End If
End If
1
2
3
4
5
6
7
8
9
10
11
12
If E = E1 Then
Istr11
Istr12
ElseIf E = E2 Then
Istr21
Istr22
...
...
Else
Istrn1
Istrn2
End If
1
2
3
4
5
6
7
8
9
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