Decidere quale eseguire tra tante sequenze alternative di istruzioni
Se le espressioni E, E1, E2, … sono di tipo semplice si può utilizzare una struttura di controllo che permette un’interpretazione molto fedele al diagramma di flusso
BASIC |
|
Select Case E Case E1: Istr11 Istr12 Case E2: Istr21 Istr22 ... ... Case Else: Istrn1 Istrn2 End Select |
|
C… |
|
switch(E) { case E1: istr11; istr12; break; case E2: istr21; istr22; break; ... ... default: istrn1; istrn2; break; } |
|
Pascal |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
Case(E) Of E1: Begin Istr11; Istr12; End; E2: Begin Istr21; Istr22; End; ... ... Else Begin Istrn1; Istrn2; End; End; |
|
Python |
|
match E: case E1: istr11 istr12 case E2: istr21 istr22 ... ... case other: istrn1 istrn2 |
|
Note
- la struttura dell’istruzione è simile tra i linguaggi
- ma ci sono notevoli differenze tra i valori ammessi per i casi (caratteri, numeri interi, …, stringhe?).
Se più casi richiedono lo stesso trattamento si possono raggruppare
BASIC |
|
Select Case E Case 11 To 15: Istr11 Istr12 Case 2, 5: Istr21 Istr22 Case Else: Istr31 Istr32 End Select |
|
C.. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
switch(E) { case 11: case 12: case 13: case 14: case 15: istr11; istr12; break; case 2: case 5: istr21; istr22; break; default: istr31; istr32; break; } |
|
Pascal |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
Case(E) Of 11..15: Begin Istr11; Istr12; End; 2,5: Begin Istr21; Istr22; End Else Begin Istr31; Istr32; End; End; |
|
Python |
|
match E: case 11 | 12 | 13 | 14 | 15: istr11 istr12 case 2 | 5: istr21 istr22 ... ... case other: istr31 istr32 |
|
Nota
- In Python la sintassi può essere molto complessa.