Selezioni annidate

Le condizioni perché dei blocchi siano eseguiti a scapito di altri possono essere complicate…

Selezione 4

Procedi in modo top-down utilizzando sempre le etichette di inizio e fine blocco

C, C++, Java, …

if(A > 0)
{
    // ...
}
else
{
    // ...
}
if(A > 0)
{
    if(B > 0)
    {
        // ...
    }
    else
    {
        // ...
    }
}
else
{
    if(C > 0)
    {
        // ...
    }
    else
    {
        // ...
    }
}
if(A > 0)
{
    if(B > 0)
    {
        istr11;
        istr12;
    }
    else
    {
        istr21;
        istr22;
    }
}
else
{
    if(C > 0)
    {
        istr31;
        istr32;
    }
    else
    {
        istr41;
        istr42;
    }
}

Pascal

If A > 0 Then
  Begin
    (* ... *)
  End
Else
  Begin
    (* ... *)
  End;
If A > 0 Then
  Begin
    If B > 0 Then
      Begin
          (* ... *)
        End
      Else
        Begin
          (* ... *)
        End;
  End
Else
  Begin
    If C > 0 Then
      Begin
        (* ... *)
      End
    Else
      Begin
        (* ... *)
      End;
  End;
If A > 0 Then
  Begin
    If B > 0 Then
      Begin
        Istr11;
        Istr12;
      End
    Else
      Begin
        Istr21;
        Istr22;
      End;
  End
Else
  Begin
    If C > 0 Then
      Begin
        Istr31;
        Istr32;
      End
    Else
      Begin
        Istr41;
        Istr42;
      End;
  End;

Python

if(A > 0):
    pass
else:
    pass
if(A > 0):
    if(B > 0):
        pass
    else:
        pass
else:
    if(C > 0):
        pass
    else:
        pass
if(A > 0):
    if(B > 0):
        istr11
        istr12
    else:
        istr21
        istr22
else:
    if(C > 0):
        istr31
        istr32
    else:
        istr41
        istr42

Visual Basic

If A > 0 Then
    ' ...
Else
    ' ...
End If
If A > 0 Then
    If B > 0 Then
        ' ...
    Else
        ' ...
    End If
Else
    If C > 0 Then
        ' ...
    Else
        ' ...
    End If
End If
If A > 0 Then
    If B > 0 Then
        Istr11
        Istr12
    Else
        Istr21
        Istr22
    End If
Else
    If C > 0 Then
        Istr31
        Istr32
    Else
        Istr41
        Istr42
    End If
End If

Se l’uso delle istruzioni annidate risulta complesso è consigliabile utilizzare delle selezioni esplicite per ogni singolo blocco di istruzioni a costo di espressioni logiche più complesse…

C, C++, Java, …

if(A > 0 && B > 0)
{
    istr11;
    istr12;
}
else if(A > 0 && B <= 0)
{
    istr21;
    istr22;
}
...
...

Pascal

If(A > 0) And (B > 0) Then
  Begin
    Istr11;
    Istr12;
  End
Else If(A > 0) And (B <= 0) Then
  Begin
    Istr21;
    Istr22;
  End
...
...

Python

if(A > 0) and (B > 0):
    istr11
    istr12
elif(A > 0) and (B <= 0):
    istr21
    istr22
...
...

Visual Basic

If (A > 0) And (B > 0) Then
    Istr11
    Istr12
ElseIf (A > 0) And (B <= 0) Then
    Istr21
    Istr22
...
...
End If