If, else if, else, and switch...case use Small (C #)

Source: Internet
Author: User


Tag: The code implementation has one fill highlight than the multi-label round



Sometimes the programming for a long time, if you stop to think about it, even some of the most basic knowledge points, it may also make their own circle. In fact, at the end of the laying of the foundation of the time did not fight, or self-righteous think that they understand, and then when laying the groundwork of the lazy to think too lazy to see, the result is that the basic knowledge of the point of view has been stuck in their own inherent understanding.



OK, let's get down to the chase. The IF, else if, and else are the basic syntactic constructs that we often use. One day, in the garden to see a blog posted such a problem.



Console input a score, if the score >=90: Output A, if 90> score >=80 output B, if 80> results >=70 output C, if 70> results >=60 output D, if the results <60 output E. think about using if or if-else good or if else if yes, why good?



  The IF code is implemented as follows:


if (score >= 90)
            {
                Console.WriteLine("A");
            }
            if (score >= 80)
            {
                Console.WriteLine("B");
            }
            if (score >= 70)
            {
                Console.WriteLine("C");
            }
            if (score >= 60)
            {
                Console.WriteLine("D");
            }
            if (score < 60)
            {
                Console.WriteLine("E");
            } 


The If...else code is implemented as follows:


if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else//<90
            {
                if (score >= 80)
                {
                    Console.WriteLine("B");
                }
                else//<80
                {
                    if (score >= 70)
                    {
                        Console.WriteLine("C");
                    }
                    else//<70
                    {
                        if (score >= 60)
                        {
                            Console.WriteLine("D");
                        }
                        else
                        {
                            Console.WriteLine("E");
                        }
                    }
                }
            } 


The code for If...else if is implemented as follows:


if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("D");
            } 


In the company to do development, I believe there are a lot of people disorderly use these several structures.



We first look at the if implementation, it is obvious that the IF statements are independent, regardless of whether the previous if statement is not true, the current if statement will be executed, obviously, this implementation is inefficient.



Second and third implementations, in essence, the two implementations are equivalent, in most programming language implementations, the else if is not as a basic keyword appears, it is actually an else statement and if statement of the combination, which avoids the use of only if, else when multiple nesting, Like the second implementation above, like the syntax sugar, it's easy for programmers to knock code.



In addition, this scenario can also be implemented using the SWITCH...CASE structure. In C #, when you use the switch structure on an integer constant, the CLR always translates switch into a lookup table structure, so that each case branch gets the same execution time, in this situation, compared to using the If ... Else structure has a value at the same time or a small number of values appear significantly higher, then the use of the IF...ELSE structure and the higher the frequency of the first execution speed than the switch structure will be faster.



Finally, the switch...case structure in C # also allows the use of string constants, which are slightly different from integer constants. It will be compiled into a structure similar to the IF statement in cases where the case label is less (approximately 6 or so). However, if the case label is large enough (greater than 6), the C # compiler creates a Hashtable object, populates it with string constants, and then finds it on the table and jumps. Hashtable lookup is not strictly O (1) and has a significant constant cost, but if the number of case labels is large enough, it will be faster than comparing string constants in multiple if statements.



If, else if, else, and switch...case use Small (C #)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.