Exercice langage C corrigé structure alternative condition if – else if – else

Considérez la séquence d’instructions suivante:

        if (A>B) 
        if (A>10) 
        printf ("premier choix \n"); else if (B<10)
        printf ("deuxième choix \n"); else
        if (A==B) printf ("troisième choix \n");
        else printf ("quatrième choix \n");

a) Copiez la séquence d’instructions en utilisant des tabulateurs pour marquer les blocs if – else appartenant ensemble.

        if (A>B) 
            if (A>10) 
                printf ("premier choix \n");
            else if (B<10)
                printf ("deuxième choix \n");
            else if (A==B)
                printf ("troisième choix \n");
            else
                printf ("quatrième choix \n");

b) Le résultat:

"premier choix" apparaît pour (A>B) et (A>10)
"deuxième choix" apparaît pour (10A>B)
"troisième choix" apparaît pour (10A>B10) et (A=B)
10>10 impossible
A>B et A=B impossible => « troisième choix » n’apparaît jamais
"quatrième choix" apparaît pour (10A>B10) et (AB)
10>10 impossible => « quatrième choix » n’apparaît jamais

c) On n’obtient pas de réponses pour (AB). Si (A>B) alors la construction if – else if – … – else garantit que toutes les combinations sont traitées et fournissent un résultat.

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *