C # statement,

Source: Internet
Author: User
Tags switch case

C # statement,

 

Select statement

If, else

If is the meaning of else, else is the other meaning. if is followed by the condition in brackets (), it enters the if statement to execute the command. If not, the if statement is not entered. Else does not need to add conditions, but must be used with if. else can also add if, but conditions are required after if. If-else can be nested.

Similar to conditional operators, the format is as follows:

If (expression) // the return value of the expression is True or False.
{
}
Note:
1. The expression returns the bool value;
2. No extra points are required for parentheses and curly braces.


(2)
If (expression)
{
}
Else
{
}
Example: 1. Have you ever run a leopard? Yes or no.

Console. Write ("have you ever run leopard? ");
String s = Console. ReadLine ();
If (s = "yes ")
{
Console. WriteLine ("You have more animals than animals !! ");
}
Else
{
Console. WriteLine ("You don't even have any animals !! ");
}
Console. ReadLine ();

(3)
If (expression)
{
}
Else if
{
}
Else if
{
}
...
Else
{
}
In various cases, only one of them can be used. If none of the above is used, the else will be executed.

(4)
If (expression)
{
If (){}
Else {}
}
Else
{
If (){}
}
If nesting

Application

I

Enter the Student name and the exam score double
If it is 100, [Congratulations ***, the full score is passed !]
If it is greater than or equal to 80 and less than 100, [**, you are excellent, continue to maintain !]
If the value is greater than or equal to 60 and less than 80, [** excellent performance]
If the value is greater than or equal to 50 and less than 60, [** it's just a little worse. You must pass at least next time !]
Less than 50, [** Are you stupid ?]
Console. Write ("enter your name :");
String name = Console. ReadLine ();
Console. Write ("Enter your score :");
Double score = double. Parse (Console. ReadLine ());
If (score> = 0 & score <= 100)
{
If (score = 100)
{
Console. WriteLine (name + ", congratulations, full score! ");
}
Else if (score> = 80)
{
Console. WriteLine (name + ", you are excellent! Continue! ");
}
Else if (score> = 60)
{
Console. WriteLine (name + ", excellent performance! ");
}
Else if (score> = 50)
{
Console. WriteLine (name + ", just a little bit! ");
}
Else
{
Console. WriteLine (name + ", are you stupid? ");
}
}
Else
{
Console. WriteLine ("incorrect input! ");
}

Console. ReadLine ();

II

Enter year, month, and day to determine whether the date format is correct!
Console. Write ("Enter the year :");
Int year = int. Parse (Console. ReadLine ());
If (year> = 0 & year <= 9999)
{
Console. Write ("Enter the month :");
Int month = int. Parse (Console. ReadLine ());
If (month> = 1 & month <= 12)
{
Console. Write ("Enter the day :");
Int day = int. Parse (Console. ReadLine ());
If (day> = 1 & day <= 31)
{
If (month = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = = 12)
{
Console. WriteLine ("the date format is correct. The date you entered is {0}-{1}-{2 }. ", Year, month, day );
}
Else if (month = 4 | month = 6 | month = 9 | month = 11)
{
If (day <= 30)
{
Console. WriteLine ("the date format is correct. The date you entered is {0}-{1}-{2 }. ", Year, month, day );
}
Else // day = 31
{
Console. WriteLine ("Date Format error! ");
}
}
Else // February
{
If (day <= 28)
{
Console. WriteLine ("the date format is correct. The date you entered is {0}-{1}-{2 }. ", Year, month, day );
}
Else if (day = 29)
{
If (year % 4 = 0 & amp; year % 100! = 0 | year % 400 = 0)
{
Console. WriteLine ("the date format is correct. The date you entered is {0}-{1}-{2 }. ", Year, month, day );
}
Else
{
Console. WriteLine ("the date format is incorrect !! ");
}
}
Else // day = 30 | 31
{
Console. WriteLine ("the date format is incorrect! ");
}
}
}
Else
{
Console. WriteLine ("incorrect input on the day !! ");
}
}
Else
{
Console. WriteLine ("Incorrect month input! ");
}
}
Else
{
Console. WriteLine ("year input error !! ");
}

Console. ReadLine ();

3.

Dating process: Do you have a house? Are you rich? Are you competent?
[Get married] [buy a house first and get married] [make money first and then buy a house again] None [Bye bye ~~]
Use if nesting for dating

Console. WriteLine ("Female: Do you have a house? ");
String a = Console. ReadLine ();
If (a = "yes ")
{
Console. WriteLine ("Female: get married ");
}
Else
{
Console. WriteLine ("Female: Do you have money? ");
String B = Console. ReadLine ();
If (B = "yes ")
{
Console. WriteLine ("Female: Buy a house first and get married ");
}
Else
{
Console. WriteLine ("Female: Do you have the ability? ");
String c = Console. ReadLine ();
If (c = "yes ")
{
Console. WriteLine ("Female: Making money before buying a house and getting married ");
}
Else
{
Console. WriteLine ("Female: Bye bye ~~ ");
}

}
}
Console. ReadLine ();

Switch case

 

Switch case must be used with break.

 

Break is a jump statement. When used with the switch case, it jumps out of the nearest {}.

Switch case and if, slse if, slse if... slse (Same)

 

 

Console. WriteLine ("1. Hamburger ");
Console. WriteLine ("2. Fries ");
Console. WriteLine ("3. Chicken nuggets ");
Console. WriteLine ("4. Chicken leg ");
Console. WriteLine ("5. Chicken Rice ");

 

Console. Write ("Enter the number of your selected project :");
String a = Console. ReadLine ();

 

Switch (a) // The variable name to be judged in brackets
{
Case "1": // The value after case is used to determine whether the variables in the brackets above are inconsistent.
Console. WriteLine ("hamburger selected ");
Break; // The break jump statement that jumps out of the nearest curly brackets.
Case "2": // there is a space between case and value, followed by a colon
Console. WriteLine ("You selected fries ");
Break;
Case "3 ":
Console. WriteLine ("chicken nuggets selected ");
Break;
Case "4 ":
Console. WriteLine ("You selected chicken leg ");
Break;
Case "5 ":
Console. WriteLine ("You selected chicken rice ");
Break; // The Last One also needs to jump out of curly brackets
}

 

Related Article

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.