switch

La instrucción switch es una instrucción de decisión múltiple, donde el compilador prueba o busca el valor contenido en una variable contra una lista de constantes ints o chars, cuando el computador encuentra el valor de igualdad entre variable y constante, entonces ejecuta el grupo de instrucciones asociados a dicha constante, si no encuentra el valor de igualdad entre variable y constante, entonces ejecuta un grupo de instrucciones asociados a un default, aunque este ultimo es opcional. El formato de esta instrucción es el siguiente;  capturar o asignar variable de condición; switch(var int o char) { case const1: instrucción(es); break; case const2: instrucción(es); break; case const3: instrucción(es); break; ……………… default: instrucción(es); };  ejemplo de la estructura switch <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">#include <stdio.h> <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">#include <conio.h> <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">#include <string.h> <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">void main <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">{ <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">// declaracion variables <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">char letra; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">//capturando <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">clrscr; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">gotoxy(10,5);printf("dame una letra : "); <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">letra=getchar;getchar; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">//empieza switch <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">switch(letra) <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">{ <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case 'a': gotoxy(30,5);puts("aguila");break; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case 'b': case 'B': gotoxy(30,5);puts("baca");break; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case 'c': gotoxy(30,5);puts("caballo ");puts("camello");break; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">default:gotoxy(30,5);puts("no hay"); <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">} <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">getchar; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">} <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;"> <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">Notas: <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">1.- Solo se puede usar como variable de condición una variable entera o variable char. <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">2.- Las constantes que estamos buscando y comparando son de tipo char, por eso se deben encerrar entre apóstrofes ( '). <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">3.- Si se quiere resolver el problema de mayúsculas o minúsculas en el teclado, observar que se usan dos case, pero con un solo break; <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">6.- Recordar que switch solo trabaja con constantes y variables de tipo char o int, en este ultimo caso, solo usar una variable de opción de tipo entero y en los case poner la constante numérica, sin apóstrofes <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;"> <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">La estructura switch permite elegir ejecutar diferentes códigos (conjunto de <span style="background-color: transparent; color: #0000ff; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: underline; vertical-align: auto;">[|__sentencias__] de programacion) dependiendo de un valor. <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">La forma general es: <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">switch (valor) { ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case ‘1’: ejecuta esto si valor es 1 ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case ‘2’: ejecuta esto si valor es 2 ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case ‘3’: ejecuta esto si valor es 3 ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">default: ejecuta esto si valor no es ninguno de los anteriores ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">}; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">Cada case puede ejecutar múltiples líneas de código. Todos deben terminan con un break, que indica que debe seguirse la ejecución del programa luego de ejecutar las líneas superiores. La ejecución continúa luego de la estructura switch. El caso default se ejecuta si no hubo coincidencia entre el contenido de valor y los case superiores. <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">Un ejemplo del uso de la estructura switch en <span style="background-color: transparent; color: #0000ff; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: underline; vertical-align: auto;">[|__lenguaje__] C: <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">int hijos; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">printf("Ingrese la cantidad de hijos que usted tiene: ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">"); ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">scanf("%d", &hijos); ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">switch (hijos) { ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case ‘0’: ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">printf("No le corresponde asignación familiar por hijo ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">"); ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case ‘1’: ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">printf("Le corresponden 50usd de asignación familiar por su único hijo ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">"); ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">case ‘2’: ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">printf("Le corresponden 75usd de asignación familiar por sus dos hijos ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">"); ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">default: ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">printf("Le corresponden 100usd de asignación familiar por tener más de dos hijos ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">"); ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">break; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">**<span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">}; ** <span style="background-color: transparent; color: #000000; font-family: Times New Roman; font-size: 16px; text-align: left; text-decoration: none; vertical-align: auto;">Se define la variable del tipo entero hijos, se le pide al usuario que ingrese el número de hijos que posee y luego entra en acción la estructura switch, actuando de diferentes formas dependiendo del valor de la variable hijos en tiempo de ejecución <span style="background-color: transparent; color: #000000; font-family: Arial; font-size: 2px; text-align: left; text-decoration: none; vertical-align: auto;">