Branch statements for c entry use the methods of ternary operator if statement switch statement

  • 2020-06-19 11:36:26
  • OfStack

The 3 element operator:?

Many languages have this operator, and the principle is the same, but the difference may be in the writing format. Used to the excel way, always want to separate multiple expressions with, hehe. The 3 element operator in C#? Between the two returned expressions, use: instead of:. Note:


string h = (k<10) ? "k Less than 10":"k Greater than or equal to 10";

Of course, this expression is good for simple post-comparison return value processing, but not so good for large amounts of code executed based on comparison results.

IF statement

The IF statement does not return a value (unlike? ), and can execute complex blocks of statement code, surrounded by {} Numbers, OK. Like any other language: if... elseif... else... , for example, to see how it works:


string msg;
double i, j;
Console.WriteLine(" Please enter the first 1 Value of number: ");
i = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(" Please enter the first 2 Value of number: ");
j = Convert.ToDouble(Console.ReadLine());
if (i>j)
{
    msg = " Is greater than ";
}else if (i == j)
{
    msg = " Is equal to the ";
}else
{
    msg = " Less than ";
}
Console.WriteLine(" The first 1 An operand  {0}  The first 2 An operand ", msg);
Console.ReadKey();

This example is relatively simple, but it shows one programming idea. Instead of adding Console.WriteLine to each block of if statements to output the result, I've reduced this to just one output statement. We should pay more attention to this in the future.

switch statement

The IF statement is easy to use, but when it comes to discrete values, it becomes unreadable when compared with IF. In addition, switch matches the value of the expression with an option, rather than a conditional judgment like if ( > , < ). The value to be matched must be a constant value, which can be a literal (1, 2, 3), or a constant:


const string myName = "karli";
const string sexyName = "angelina";
const string sillyName = "ploppy";
string name;
Console.WriteLine(" Please enter your name: ");
name = Console.ReadLine();
switch(name.ToLower())
{
    case myName:
        Console.WriteLine(" You and I have the same name, {0}", myName);
        break;
    case sexyName:
        Console.WriteLine(" Wow! ~ . {0} That's a sexy name. ", sexyName);
        break;
    case sillyName:
    case "h":
        { 
        Console.WriteLine("{0} That's such a fancy name ~~", sillyName);
        Console.WriteLine(" That, of course, ~");
        break;
        }
    default:
        Console.WriteLine("hello {0}", name);
        break;
}
Console.ReadKey();

For each statement executed by case, one break needs to be placed, including default. In addition, case can be stacked multiple times, and as long as one of them satisfies the condition, the following statement will be executed. After case, you can also use {} to surround a block of statements and execute multiple statements.

conclusion

In fact, in the learning process, the compilation and debugging of examples is very important, not only deepen the impression, but also cause you to think. Of course, if there is a small procedures to practice hand, is very good ha, you have needs or ideas can contact me ha.


Related articles: