If statement
C # |
VB |
Int I = 3; If (I <5) { Console. writeline ("I less than 5 "); } |
Dim I as integer = 3 If I <5 then Console. writeline ("I less than 5 ") End if |
Int I = 9; If (! (I <5 )) { Console. writeline ("I greater than or equal to 5 "); } |
Dim I as integer = 9 If not I <5 then Console. writeline ("I greater than or equal to 5 ") End if |
Int I = 3; If (I <5) { Console. writeline ("I less than 5 "); } Else { Console. writeline ("I greater than or equal to 5 "); } |
Dim I as integer = 3 If I <5 then Console. writeline ("I less than 5 ") Else Console. writeline ("I greater than or equal to 5 ") End if |
Int I = 3; If (I <5) { Console. writeline ("I less than 5 "); } Else if (I = 5) { Console. writeline ("I equals 5 "); } Else { Console. writeline ("I greater than 5 "); } |
Dim I as integer = 3 If I <5 then Console. writeline ("I less than 5 ") Elseif I = 5 then Console. writeline ("I equals 5 ") Else Console. writeline ("I greater than 5 "); End if |
Switch statement
C # |
VB |
Int I = 9; Switch (I) { Case 3: Console. writeline ("I = 3 "); Break; Case 5: Console. writeline ("I = 5 "); Break; Case 9: Console. writeline ("I = 9 "); Break; Default: Console. writeline ("other "); Break; } |
Dim I as integer = 9 Select case I Case 3 Console. writeline ("I = 3 ") Case 5 Console. writeline ("I = 5 ") Case 9 Console. writeline ("I = 9 ") Case else Console. writeline ("other ") End select |
Int I = 5; Switch (I) { Case 3: Case 5: Case 9: Console. writeline ("I = 3 or 5 or 9 "); Break; Default: Console. writeline ("other "); Break; } |
Dim I as integer = 5 Select case I Case 3, 5, 9 Console. writeline ("I = 3 or 5 or 9 ") Case else Console. writeline ("other ") End select |
Not Supported |
Dim I as integer = 9 Select case I Case 5 to 9 Console. writeline ("I = 5 to 9 ") End select |
Enum tricolor {red, green, blue} //... Tricolor color = tricolor. blue; Switch (color) { Case tricolor. Blue: Console. writeline ("blue "); Break; Case tricolor. Green: Console. writeline ("green "); Break; Default: Console. writeline ("other color "); Break; } |
Enum tricolor Red Green Blue End Enum '... Dim color as tricolor = tricolor. Blue Select case color Case tricolor. Blue Console. writeline ("blue ") Case tricolor. Green Console. writeline ("green ") Case else Console. writeline ("other color ") End select |
Tips
1. imposible is nothing
I remember that the most common sentence I made when debugging a program was "impossible ......". Because we always have to assume many conditions during programming. We will assume that the sky will not go down, the door will not be hit by a car, the meal will not be killed, otherwise we will not be able to live. For example, we will write a program like this: If (sex = 1)
{
Console. writeline ("man ");
}
Else if (sex = 0)
{
Console. writeline (" ");
}
Because we thought it was impossible for the sex variable to have another value.
However, once such a code is written, it will soon be regretted, because, as Mr. winberger said, "Everything that is impossible will happen ", so the correct statement should be: If (sex = 1)
{
Console. writeline ("man ");
}
Else if (sex = 0)
{
Console. writeline (" ");
}
Else
{
System. Diagnostics. Debug. Assert (false, "shocould never reach here! ");
}
Or the write is good: system. Diagnostics. Debug. Assert (sex = 1 | sex = 0), "Wrong sex ");
If (sex = 1)
{
Console. writeline ("man ");
}
Else
{
Console. writeline (" ");
}
In short, as long as you write the if or switch statement, make sure that it overwrites all values of the variable.
2. Avoid Multi-layer nested if statements
I believe that everyone has been suffering from code layers and layers. Especially when every layer of code is very long, it feels like you are lost in the tropical rain forest, after several rounds, I don't know where I am. Compare the following two codes with the same functions.
Code with poor Readability: Public String Foo (INT age)
{
If (age> = 0 & age <= 150)
{
If (age> = 0 & age <= 18)
{
Return "Teenagers ";
}
Else if (age> 18 & age <= 60)
{
Return "middle-aged ";
}
Else
{
Return "elder care ";
}
}
Else
{
Return "invalid age ";
}
}
Readable code: Public String Foo (INT age)
{
If (age <0 | age> 150)
{
Return "invalid age ";
}
If (age> = 0 & age <= 18)
{
Return "Teenagers ";
}
Else if (age> 18 & age <= 60)
{
Return "middle-aged ";
}
Else
{
Return "elder care ";
}
}
Does it feel a lot fresh at once? More importantly, the second part of the Code clearly divides the processing of normal and abnormal situations into two parts, making it easier for readers to concentrate.