Java |
---|
if/else
switch
:?
if statement
public static void main(String[] args) {
if (args.length == 0)
System.out.println("No args");
else
System.out.println("Args");
}
if (myInt < 0 || myInt == 0)
{
System.out.println("Your number "+myInt+" is less than or equal to zero.");
}
else if (myInt > 0 && myInt <= 10)
{
System.out.println("Your number "+myInt+" is between 1 and 10.");
}
else if (myInt > 10 && myInt <= 20)
{
System.out.println("Your number "+myInt+" is between 11 and 20.");
}
else if (myInt > 20 && myInt <= 30)
{
System.out.println("Your number "+myInt+" is between 21 and 30.");
}
else
{
System.out.println("Your number "+myInt+" is greater than 30.");
}
switch statement
public static void main(String[] args) {
switch (args.length) {
case 0:
System.out.println("No args");
break;
case 1:
System.out.println("One arg ");
break;
default:
int n = args.length;
System.out.println(n + " args");
break;
}
}
switch (myInt)
{
case 1:
case 2:
case 3:
System.out.println("Your number is "+ myInt);
break;
default:
System.out.println("Your number "+myInt+" is not between 1 and 3.");
break;
}
String[] selection= {"run","save","quit"};
void DoCommand(int idx) {
switch (idx) {
case 0:
DoRun();
break;
case 1:
DoSave();
break;
case 2:
DoQuit();
break;
default:
InvalidCommand(idx);
break;
}
}
int a = 5;
System.out.print(a==5 ? "A is 5" : "A is not 5");
prints: A is 5
|
|
C# |
---|
if/else
switch
:?
if statement
static void Main(string[] args) {
if (args.Length == 0)
Console.WriteLine("No args");
else
Console.WriteLine("Args");
}
if (myInt < 0 || myInt == 0)
{
Console.WriteLine("Your number {0} is less than or equal to zero.", myInt);
}
else if (myInt > 0 && myInt <= 10)
{
Console.WriteLine("Your number {0} is between 1 and 10.", myInt);
}
else if (myInt > 10 && myInt <= 20)
{
Console.WriteLine("Your number {0} is between 11 and 20.", myInt);
}
else if (myInt > 20 && myInt <= 30)
{
Console.WriteLine("Your number {0} is between 21 and 30.", myInt);
}
else
{
Console.WriteLine("Your number {0} is greater than 30.", myInt);
}
switch statement
static void Main(string[] args) {
switch (args.Length) {
case 0:
Console.WriteLine("No args");
break;
case 1:
Console.WriteLine("One arg ");
break;
default:
int n = args.Length;
Console.WriteLine("{0} args", n);
break;
}
}
switch (myInt)
{
case 1:
case 2:
case 3:
Console.WriteLine("Your number is {0}.", myInt);
break;
default:
Console.WriteLine("Your number {0} is not between 1 and 3.", myInt);
break;
}
void DoCommand(string command) {
switch (command.ToLower()) {
case "run":
DoRun();
break;
case "save":
DoSave();
break;
case "quit":
DoQuit();
break;
default:
InvalidCommand(command);
break;
}
}
int a = 5;
System.Console.WriteLine(a==5 ? "A is 5" : "A is not 5");
prints: A is 5
|
|