# [DOTNET skill series] If else if misuse and correct use in 8. C #

Source: Internet
Author: User

Question: Evaluate the final test scores of Students

Score> = 90:

90> score> = 80: B

80> score> = 70: c

70> score> = 60: d

Solution 1: If else if is not understood, and this error is easy to make.

`If (score> = 90) // condition 1 {console. writeline ("A");} else if (80 = <score& Score <90) // Condition 2 score <90 is not executed at all, and the essence of if else if is not understood {console. writeline ("B ");}//...`

The above statement does not actually understand the nature of if else if.

The essence of if else if is: if the if condition is not met, the condition judgment in else is executed. Based on this understanding, if condition 1 of the above if statement does not meet the requirements, it actually means that

Therefore, the sub-condition score <90 in condition 2 is a single stroke multiple times!

Or else if (score <90 & score <= 80), here the score <90 is true after condition 1 is false!

Solution 2: mathematical thinking, compilation and translation fail

`if (80 <= score < 90)  // BUILD ERROR: Operator '<' cannot be applied to operands of type 'bool' and 'int'            {                Console.WriteLine("B");            }`

Correct writing

`Console. writeline ("Enter your score"); int score = convert. toint32 (console. readline (); 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 ");}`

Question: Compare the user name and password and enter the corresponding prompt

Prompt the user to enter the user name, and then prompt the user to enter the password. If the user name is "admin" and the password is "888888", the prompt is correct.

Otherwise, if the user name is not admin, the system prompts that the user name does not exist. If the user name is admin, the system prompts that the password is incorrect.

`Static void main (string [] ARGs) {console. writeline ("Enter the user name"); string username = console. readline (); console. writeline ("Enter Password"); string Password = console. readline (); If (username = "admin" & Password = "888888") {console. writeline ("correct password");} else {If (username! = "Admin") {console. writeline ("incorrect user name");} else if (password! = "888888") {console. writeline ("Incorrect password") ;}} console. readkey ();}`

In the preceding statement, if else is nested in else. The following uses another method: If else if else

`Class program {static void main (string [] ARGs) {console. writeline ("Enter your username"); string username = console. readline (); console. writeline ("enter your password"); string Password = console. readline (); // The if else below can be understood in pairs, for example, else if else can still be understood as an IF (username = "admin" & Password = "888888") {console. writeline ("correct username and password");} else if (username! = "Admin") {console. writeline ("incorrect user name");} else // note the above if else if {console. writeline ("Incorrect password");} console. readkey ();}}`

If else statement used {}

If the if expression has only one statement after it, the same form of {} is not used, but there are different results.

`If (true) string test = "test"; // this will cause a compilation error! If (true) {string test = "test"; // The statement is correct}`

Combination of else and near if

If the if expression is followed by only one statement, it usually does not write {}, but this habit may also cause bugs in the program, such as the following code

`Class program {static void main (string [] ARGs) {int age = 15; char sex = 'F'; If (age <10) if (sex = 'F ') console. writeline (""); else // note that else only works with the recent if, so no console is output for this question. writeline ("You have grown up"); console. readkey ();}}`

Conclusion: In actual situations, we usually think that we will if else, but in fact, if else can be combined to construct very complex business logic.

If else can understand the meaning of the service at a glance, but the difference if else can be misleading or difficult to understand the meaning of this if else.

In a word, I would like to explain how to use a language to express the meaning of if else. Only in this way can I understand how the business logic and business rules of a program are described in programming languages.

Finally, the level of use of if else and while determines your programming capability!

The program only has three basic structures: sequence, branch, and loop. It is very important to master these basic structures.

Related Keywords:
Related Article

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.