Alternativa ciondolante


Si tratta di un errore che si può commettere annidando le selezioni.

Considera il diagramma di flusso
image
e le codifiche corrispondenti
BasicPascalC
SE(A > 0) ALLORA
    SE(B > 0) ALLORA
        INIZIO
            Istr11
            Istr12
        FINE
    ALTRIMENTI
        INIZIO
            Istr21
            Istr22
        FINE
IF(A > 0) THEN
    IF(B > 0) THEN
        Istr11
        Istr12
    ELSE
        Istr21
        Istr22
    END IF
END IF
If A > 0 Then
    Begin
        If B > 0 Then
            Begin
                Istr11;
                Istr12;
            End
        Else
            Begin
                Istr21;
                Istr22;
            End;
    End;
if(A > 0)
{
    if(B > 0)
    {
        istr11;
        istr12;
    }
    else
    {
        istr21;
        istr22;
    }
}


Con poche modifiche al codice
BasicPascalC
SE(A > 0) ALLORA
    INIZIO
        SE(B > 0) ALLORA
            INIZIO
                Istr11
                Istr12
                ...
            FINE
    FINE
ALTRIMENTI
    INIZIO
        Istr21
        Istr22
        ...
    FINE
IF(A > 0) THEN
    IF(B > 0) THEN
        Istr11
        Istr12
        ...
    END IF
ELSE
    Istr21
    Istr22
    ...
END IF
If A > 0 Then
    Begin
        If B > 0 Then
            Begin
                Istr11;
                Istr12;
            End
    End
Else
    Begin
        Istr21;
        Istr22;
    End;
if(A > 0)
{
    if(B > 0)
    {
        istr11;
        istr12;
    }
}
else
{
    istr21;
    istr22;
}


si realizza l'algoritmo corrispondente al diagramma di flusso
image

Note

Un programmatore principiante potrebbe erroneamente scrivere il codice seguente per riferirsi al secondo diagramma di flusso ottenendo in realtà il primo...
PascalC
SE(A > 0) ALLORA
    SE(B > 0) ALLORA
        INIZIO
            Istr11
            Istr12
        FINE
ALTRIMENTI
    INIZIO
        Istr21
        Istr22
    FINE
If A > 0 Then
    If B > 0 Then
        Begin
            Istr11;
            Istr12;
        End
Else
    Begin
        Istr21;
        Istr22;
    End;
if(A > 0)
    if(B > 0)
    {
        istr11;
        istr12;
    }
else
{
    istr21;
    istr22;
}


Note
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki