0% found this document useful (0 votes)
40 views

Switvh

The document contains code that demonstrates the use of the switch statement in C# to evaluate different math operations based on a user's input. The code prompts the user to enter two numbers and an operation to perform: addition, subtraction, multiplication, division or exit. It uses a switch statement to evaluate the operation variable and perform the corresponding math operation, displaying the result. The switch statement allows the code to evaluate the user's choice and execute the appropriate case block to handle each operation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Switvh

The document contains code that demonstrates the use of the switch statement in C# to evaluate different math operations based on a user's input. The code prompts the user to enter two numbers and an operation to perform: addition, subtraction, multiplication, division or exit. It uses a switch statement to evaluate the operation variable and perform the corresponding math operation, displaying the result. The switch statement allows the code to evaluate the user's choice and execute the appropriate case block to handle each operation.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

using System;

class Sample
{
enum Color {Yellow = 1, Blue, Green};
static DateTime thisDate = DateTime.Now;

public static void Main()


{
Console.Clear();

Fausto A. Saazar Fierro


// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
Console.WriteLine(
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);

Fausto A. Saazar Fierro


// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
Console.WriteLine(
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal full date/time: {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);

Fausto A. Saazar Fierro


// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
Console.WriteLine(
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
}
}

Fausto A. Saazar Fierro


Switch

Fausto A. Saazar Fierro


The Switch element allows us to evaluate a variable and do different
things depending on the result of the evaluation. It's similar to using
multiple IFs sequentially, although you could say that with Switch you
can achieve neater code. So… when to use switch or if? It is up to you
as a developer to use what you think is most convenient within your
program; you know that you should always keep in mind that your
code is understandable. You will not be the only one who is going to
modify it!

Fausto A. Saazar Fierro


Switch is usually used when the response has to be evaluated,
and a message is displayed according to the code they return. Or
when I am asked to display a personalized message based on the
action the user has taken in our program.

Fausto A. Saazar Fierro


static void Main(string[] args)
{
int op = 0;
int num1 = 0;
int num2 = 0;
int resultado = 0;
Console.Write("Ingrese Numero 1 : ");
num1= Convert.ToInt16(Console.ReadLine());
Console.Write("Ingrese Numero 2 : ");
num2 = Convert.ToInt16(Console.ReadLine());

Fausto A. Saazar Fierro


Console.WriteLine("______________________");
Console.WriteLine("Meu Principal");
Console.WriteLine("_______________________");
Console.WriteLine("1 Suma");
Console.WriteLine("2 Resta");
Console.WriteLine("3 Multiplicacion");
Console.WriteLine("4 Division");
Console.WriteLine("5 Salir");
Console.Write("Opción : ");
op= Convert.ToInt16(Console.ReadLine());

Fausto A. Saazar Fierro


switch(op)
{
case 1:
resultado=num1+ num2;
Console.WriteLine("La suma es : {0:d} {1:d} {2:d}", resultado, num1, num2);
break;
case 2:
resultado = num1 - num2;
Console.WriteLine("La resta es : ", resultado);
break;
case 3:
resultado = num1 * num2;
Console.WriteLine("La multiplicacion de {0:d} y {1:d} es {2:d}: ",
num1, num2, resultado);
break;

Fausto A. Saazar Fierro


case 4:
if (num2 > 0)
{
resultado = num1 / num2;
Console.WriteLine("La division de {0:d} y {1:d} es {2:d}: ",
num1, num2, resultado);
}
else
{
Console.WriteLine("No se puede dividir para 0 ...");
}

break;

Fausto A. Saazar Fierro


case 5:
Console.WriteLine("Fin del programa");
break;
default:
Console.WriteLine("No se encuentra opción");
break;

}
Console.ReadKey();

Fausto A. Saazar Fierro

You might also like