C# process control judgment if, switch statement C# learning miscellaneous notes (3)

1. If statement The
most basic is if combined with the else statement, if the condition is not met, the else statementis executed

            if (a>b)
            {
    
    
                Console.WriteLine("a大于b");
            }
            else
            {
    
    
                Console.WriteLine("a小于b");
            }

When the judgment condition is increased, it can be used in conjunction with the else if statement, such as judging the grade of a person’s performance

			if (a>=90)
            {
    
    
                Console.WriteLine("优秀");
            }
            else if (a>=70 )
            {
    
    
                Console.WriteLine("良好");
            }
            else if (a>=60)
            {
    
    
                Console.WriteLine("及格");
            }
            else
            {
    
    
                Console.WriteLine("不及格");
            }

2. Switch statement The
basic switch statement is used. It should be noted that the case statement needs to be used with break to escape the case statement at the end, otherwise an error will be reported

string a = "吃饭";
            switch (a)//传入用于判断的变量
            {
    
    
                case "电影": //判断条件
                    Console.WriteLine("现在去看电影");
                    break; //跳出 case 语句,否则会报错
                case "吃饭":
                    Console.WriteLine("现在去吃饭");
                    break;
                case "逛街":
                    Console.WriteLine("现在去逛街");
                    break;
                default:
                    Console.WriteLine("现在去做其他的事情");
                    break;
            }

The conditions in the switch statement run through,
such as judging the daily status of students,
Monday to Friday: class,
Saturday: self-study,
week seven: rest

Console.WriteLine("请输入星期几");
            string day = Console.ReadLine();
            switch (day)
            {
    
    
                case "1": //使用条件贯穿,1~5
                case "2":
                case "3":
                case "4":
                case "5":
                    Console.WriteLine("上课");
                    break;
                case "6":
                    Console.WriteLine("自习");
                    break;
                case "7":
                    Console.WriteLine("休息");
                    break;
            }

When inputting 1~5, the output result is always "class"

3. If and switch performance analysis

First look at the principle. The switch case statement will generate a conditional jump table whose size is the largest case constant + 1. The program will first determine whether the switch variable is greater than the largest case constant. If it is greater, it will jump to the default branch processing; otherwise, get the index number and switch The address of the jump table entry with the same variable, the program jumps to this address to execute, and the branch jump is completed.

This is the classic efficiency of exchanging space for time . If there are many branches, using switch case is a good choice

The if else statement is usually used in range judgment, such as if (a>10) , similar to this range judgment, obviously switch case statement is not suitable

Summary: Try to use the if else statement when judging the scope , and use the switch case statement when judging multiple branch conditions.

Guess you like

Origin blog.csdn.net/qq_40385747/article/details/108900464