|
| 1 | +using System; |
| 2 | + |
| 3 | +class OperadoresEstructurasControl |
| 4 | +{ |
| 5 | + public static void Execute() |
| 6 | + { |
| 7 | + // Operadores aritméticos |
| 8 | + int suma = 5 + 3; |
| 9 | + int resta = 10 - 4; |
| 10 | + int multiplicacion = 6 * 7; |
| 11 | + double division = 20 / 4; |
| 12 | + int modulo = 15 % 4; |
| 13 | + |
| 14 | + Console.WriteLine("Operadores Aritméticos:"); |
| 15 | + Console.WriteLine($"Suma: {suma}"); |
| 16 | + Console.WriteLine($"Resta: {resta}"); |
| 17 | + Console.WriteLine($"Multiplicación: {multiplicacion}"); |
| 18 | + Console.WriteLine($"División: {division}"); |
| 19 | + Console.WriteLine($"Módulo: {modulo}"); |
| 20 | + |
| 21 | + // Operadores lógicos |
| 22 | + bool and = true && false; |
| 23 | + bool or = true || false; |
| 24 | + bool not = !true; |
| 25 | + |
| 26 | + Console.WriteLine("\nOperadores Lógicos:"); |
| 27 | + Console.WriteLine($"AND: {and}"); |
| 28 | + Console.WriteLine($"OR: {or}"); |
| 29 | + Console.WriteLine($"NOT: {not}"); |
| 30 | + |
| 31 | + // Operadores de comparación |
| 32 | + bool igual = 5 == int.Parse("5"); // Conversión necesaria en C# |
| 33 | + bool diferente = 10 != 5; |
| 34 | + bool mayorQue = 15 > 10; |
| 35 | + bool menorQue = 7 < 12; |
| 36 | + |
| 37 | + Console.WriteLine("\nOperadores de Comparación:"); |
| 38 | + Console.WriteLine($"Igual (==): {igual}"); |
| 39 | + Console.WriteLine($"Diferente (!=): {diferente}"); |
| 40 | + Console.WriteLine($"Mayor Que (>): {mayorQue}"); |
| 41 | + Console.WriteLine($"Menor Que (<): {menorQue}"); |
| 42 | + |
| 43 | + // Operadores de asignación |
| 44 | + int x = 10; |
| 45 | + x += 5; // equivalente a x = x + 5 |
| 46 | + int y = 20; |
| 47 | + y *= 2; // equivalente a y = y * 2 |
| 48 | + |
| 49 | + Console.WriteLine("\nOperadores de Asignación:"); |
| 50 | + Console.WriteLine($"x: {x}"); |
| 51 | + Console.WriteLine($"y: {y}"); |
| 52 | + |
| 53 | + // Operadores bitwise |
| 54 | + int bitwiseAnd = 5 & 3; // AND |
| 55 | + int bitwiseOr = 5 | 3; // OR |
| 56 | + int bitwiseXor = 5 ^ 3; // XOR |
| 57 | + int bitwiseNot = ~5; // NOT |
| 58 | + int leftShift = 5 << 1; // Left Shift |
| 59 | + int rightShift = 5 >> 1; // Right Shift |
| 60 | + int zeroFillRightShift = (int)((uint)5 >> 1); // Zero-fill Right Shift |
| 61 | + |
| 62 | + Console.WriteLine("\nOperadores Bitwise:"); |
| 63 | + Console.WriteLine($"Bitwise AND (&): {bitwiseAnd}"); |
| 64 | + Console.WriteLine($"Bitwise OR (|): {bitwiseOr}"); |
| 65 | + Console.WriteLine($"Bitwise XOR (^): {bitwiseXor}"); |
| 66 | + Console.WriteLine($"Bitwise NOT (~): {bitwiseNot}"); |
| 67 | + Console.WriteLine($"Left Shift (<<): {leftShift}"); |
| 68 | + Console.WriteLine($"Right Shift (>>): {rightShift}"); |
| 69 | + Console.WriteLine($"Zero-fill Right Shift (>>>): {zeroFillRightShift}"); |
| 70 | + |
| 71 | + // Estructuras de control |
| 72 | + // Condicionales |
| 73 | + int edad = 18; |
| 74 | + if (edad >= 18) |
| 75 | + { |
| 76 | + Console.WriteLine("\nEres mayor de edad."); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + Console.WriteLine("\nEres menor de edad."); |
| 81 | + } |
| 82 | + |
| 83 | + // Iterativas |
| 84 | + Console.WriteLine("\nNúmeros entre 10 y 55 (pares, no 16 ni múltiplos de 3):"); |
| 85 | + for (int i = 10; i <= 55; i++) |
| 86 | + { |
| 87 | + if (i % 2 == 0 && i != 16 && i % 3 != 0) |
| 88 | + { |
| 89 | + Console.WriteLine(i); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Excepciones |
| 94 | + try |
| 95 | + { |
| 96 | + throw new Exception("Este es un ejemplo de excepción."); |
| 97 | + } |
| 98 | + catch (Exception ex) |
| 99 | + { |
| 100 | + Console.WriteLine($"\nExcepción: {ex.Message}"); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments