No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

Structures de condition : l'instruction switch()

Syntaxe de l'instruction switch()

  1. switch(<expression>){
  2. case <_valeur1_> :
  3. <_instruction1_>;
  4. <_instruction2_>;
  5. break;
  6. case <_valeur2_> :
  7. <_instruction3_>;
  8. case <_valeur3_> :
  9. case <_valeur4_> :
  10. <_instruction4_>;
  11. break;
  12. default :
  13. <_instruction5_>;
  14. }

L'opérateur switch est un branchement conditionnel. Il nous permet d'exécuter directement le code qui correspond à une valeur donnée par <expression>.

Nous pouvons exécuter une ou plusieurs instructions correspondant à chaque valeur possible, et nous devons utiliser le mot clé break pour signifier la fin de l'instruction ou du groupe d'instructions qui correspond à cette valeur.

Certains langages permettent de spécifier plusieurs valeurs possibles pour un même groupe d'instructions, comme nous le constatons ici avec <_valeur3_> et <_valeur4_>.

Le mot clé default marque le début des instructions à exécuter si aucun branchement ne correspond à <expression>. Certains langages permettent de ne pas spécifier le branchement default, alors que d'autres langages nous obligent à le spécifier même s'il ne contient aucune instruction.
Certains langages nous obligent aussi à terminer le branchement default par le mot clé break, alors que dans d'autres langages il est facultatif.

Le code correspondant en utilisant une structure conditionnelle if est le suivant :

  1. if(<expression> == <_valeur1_>){
  2. <_instruction1_>;
  3. <_instruction2_>;
  4. }else if(<expression> == <_valeur2_>){
  5. <_instruction3_>;
  6. }
  7. }else if(<expression> == <_valeur3_> || <expression> == <_valeur4_>){
  8. <_instruction4_>;
  9. }else{
  10. <_instruction5_>;
  11. }

Exemple avec l'instruction switch()

  1. #include <stdio.h>
  2.  
  3. void main (void)
  4. {
  5. int i;
  6. printf ("\nIntroduisez un nombre entre 1 et 5: ");
  7. scanf ("%d", &i);
  8. switch (i)
  9. {
  10. case 1: printf("Vous avez introduit le nombre un");break;
  11. case 2: printf("Vous avez introduit le nombre deux");break;
  12. case 3: printf("Vous avez introduit le nombre trois");break;
  13. case 4: printf("Vous avez introduit le nombre quatre");break;
  14. case 5: printf("Vous avez introduit le nombre cinq");break;
  15. default: printf("Le nombre n'est pas compris entre 1 et 5...");break;
  16. }
  17. }

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 19/03/2002, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/programmation-switch.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.