Basic. Net knowledge-condition statements

Source: Internet
Author: User

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.

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.