|
| 1 | +using System; |
| 2 | + |
| 3 | + |
| 4 | +namespace _01_Operadores_Estructuras |
| 5 | +{ |
| 6 | + internal class Program |
| 7 | + { |
| 8 | + static void Main(string[] args) |
| 9 | + { |
| 10 | + // Operadores aritméticos |
| 11 | + int a = 10; |
| 12 | + int b = 5; |
| 13 | + Console.WriteLine("Operadores aritméticos:"); |
| 14 | + Console.WriteLine($"a + b = {a + b}"); // Suma |
| 15 | + Console.WriteLine($"a - b = {a - b}"); // Resta |
| 16 | + Console.WriteLine($"a * b = {a * b}"); // Multiplicación |
| 17 | + Console.WriteLine($"a / b = {a / b}"); // División |
| 18 | + Console.WriteLine($"a % b = {a % b}"); // Módulo |
| 19 | + |
| 20 | + // Operadores lógicos |
| 21 | + bool verdad = true; |
| 22 | + bool falso = false; |
| 23 | + Console.WriteLine("\nOperadores lógicos:"); |
| 24 | + Console.WriteLine($"verdad && falso = {verdad && falso}"); // AND |
| 25 | + Console.WriteLine($"verdad || falso = {verdad || falso}"); // OR |
| 26 | + Console.WriteLine($"!verdad = {!verdad}"); // NOT |
| 27 | + |
| 28 | + // Operadores de comparación |
| 29 | + Console.WriteLine("\nOperadores de comparación:"); |
| 30 | + Console.WriteLine($"a == b = {a == b}"); // Igualdad |
| 31 | + Console.WriteLine($"a != b = {a != b}"); // Desigualdad |
| 32 | + Console.WriteLine($"a > b = {a > b}"); // Mayor que |
| 33 | + Console.WriteLine($"a < b = {a < b}"); // Menor que |
| 34 | + Console.WriteLine($"a >= b = {a >= b}"); // Mayor o igual que |
| 35 | + Console.WriteLine($"a <= b = {a <= b}"); // Menor o igual que |
| 36 | + |
| 37 | + // Operadores de asignación |
| 38 | + Console.WriteLine("\nOperadores de asignación:"); |
| 39 | + int c = 20; |
| 40 | + c += a; // Equivalente a c = c + a |
| 41 | + Console.WriteLine($"c += a -> c = {c}"); |
| 42 | + c -= a; // Equivalente a c = c - a |
| 43 | + Console.WriteLine($"c -= a -> c = {c}"); |
| 44 | + c *= a; // Equivalente a c = c * a |
| 45 | + Console.WriteLine($"c *= a -> c = {c}"); |
| 46 | + c /= a; // Equivalente a c = c / a |
| 47 | + Console.WriteLine($"c /= a -> c = {c}"); |
| 48 | + c %= a; // Equivalente a c = c % a |
| 49 | + Console.WriteLine($"c %= a -> c = {c}"); |
| 50 | + |
| 51 | + // Operadores de bits |
| 52 | + Console.WriteLine("\nOperadores de bits:"); |
| 53 | + int d = 6; // 110 en binario |
| 54 | + int e = 3; // 011 en binario |
| 55 | + Console.WriteLine($"d & e = {d & e}"); // AND bit a bit |
| 56 | + Console.WriteLine($"d | e = {d | e}"); // OR bit a bit |
| 57 | + Console.WriteLine($"d ^ e = {d ^ e}"); // XOR bit a bit |
| 58 | + Console.WriteLine($"~d = {~d}"); // NOT bit a bit |
| 59 | + Console.WriteLine($"d << 1 = {d << 1}"); // Desplazamiento a la izquierda |
| 60 | + Console.WriteLine($"d >> 1 = {d >> 1}"); // Desplazamiento a la derecha |
| 61 | + |
| 62 | + // Operadores de identidad y pertenencia (no nativos en C#, usando métodos de clase) |
| 63 | + Console.WriteLine("\nOperadores de identidad y pertenencia:"); |
| 64 | + object obj1 = new object(); |
| 65 | + object obj2 = obj1; |
| 66 | + Console.WriteLine($"obj1 es obj2: {object.ReferenceEquals(obj1, obj2)}"); // Identidad |
| 67 | + Console.WriteLine($"'a' pertenece a 'abcd': {"abcd".Contains('a')}"); // Pertenencia |
| 68 | + |
| 69 | + // Estructuras de control condicionales |
| 70 | + Console.WriteLine("\nEstructuras de control condicionales:"); |
| 71 | + if (a > b) |
| 72 | + { |
| 73 | + Console.WriteLine("a es mayor que b"); |
| 74 | + } |
| 75 | + else if (a < b) |
| 76 | + { |
| 77 | + Console.WriteLine("a es menor que b"); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + Console.WriteLine("a es igual a b"); |
| 82 | + } |
| 83 | + |
| 84 | + // Estructuras de control iterativas |
| 85 | + Console.WriteLine("\nEstructuras de control iterativas:"); |
| 86 | + for (int i = 0; i < 5; i++) |
| 87 | + { |
| 88 | + Console.WriteLine($"For loop, iteración: {i}"); |
| 89 | + } |
| 90 | + |
| 91 | + int[] numeros = { 1, 2, 3 }; |
| 92 | + foreach (var num in numeros) |
| 93 | + { |
| 94 | + Console.WriteLine($"Foreach Loop, iteración: {num}"); |
| 95 | + } |
| 96 | + |
| 97 | + int j = 0; |
| 98 | + while (j < 5) |
| 99 | + { |
| 100 | + Console.WriteLine($"While loop, iteración: {j}"); |
| 101 | + j++; |
| 102 | + } |
| 103 | + |
| 104 | + int k = 0; |
| 105 | + do |
| 106 | + { |
| 107 | + Console.WriteLine($"Do-while loop, iteración: {k}"); |
| 108 | + k++; |
| 109 | + } while (k < 5); |
| 110 | + |
| 111 | + // Estructuras de control de excepciones |
| 112 | + Console.WriteLine("\nEstructuras de control de excepciones:"); |
| 113 | + try |
| 114 | + { |
| 115 | + int resultado = a / 0; |
| 116 | + } |
| 117 | + catch (DivideByZeroException ex) |
| 118 | + { |
| 119 | + Console.WriteLine("Excepción capturada: " + ex.Message); |
| 120 | + } |
| 121 | + finally |
| 122 | + { |
| 123 | + Console.WriteLine("Bloque finally ejecutado."); |
| 124 | + } |
| 125 | + |
| 126 | + // DIFICULTAD EXTRA |
| 127 | + Console.WriteLine("\nDificultad extra:"); |
| 128 | + for (int num = 10; num <= 55; num++) |
| 129 | + { |
| 130 | + if (num % 2 == 0 && num != 16 && num % 3 != 0) |
| 131 | + { |
| 132 | + Console.WriteLine(num); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | +} |
| 140 | + |
0 commit comments