C # statements

Source: Internet
Author: User
Tags switch case

SELECT statement

If,else

If is the meaning, else is the other meaning, if followed by () in parentheses to determine the condition, if the condition is met, enter the IF statement to execute the command. If the non-conformance does not enter the IF statement. else does not need to add conditions, but must be used with if, else can also be added if, but the if after the condition. If-else can be nested.

Similar to the conditional operator, in the following format:

if (expression)//Expression return value is True or False
{
}
Description
1. The expression returns a bool value;
2. Parentheses and braces do not need to be appended with semicolons.


Two
if (an expression)
{
}
Else
{
}
Example:1. Can you run over the leopard? Receive can or cannot.

Console.Write ("Can you run over the leopard?" ");
string s = Console.ReadLine ();
if (s = = "Can")
{
Console.WriteLine ("You are more animals than animals!!") ");
}
Else
{
Console.WriteLine ("You don't even have an animal!!") ");
}
Console.ReadLine ();

Three
if (an expression)
{
}
else if
{
}
else if
{
}
...
Else
{
}
Each situation can only go one of them, if none of the above go, will execute else inside.

Four
if (an expression)
{
if () {}
else{}
}
Else
{
if () {}
}
If nesting

Application

One

Enter student's name and enter test result double
If 100, "Congratulations on your * * *, full score through!" 】
If it is greater than or equal to 80 less than 100, "* *, you are excellent, keep it up! 】
If greater than or equal to 60 less than 80, "* * Good results"
Greater than or equal to 50 is less than 60, "* * just a little bit, next time must pass at least!" 】
Less than 50, "* * Are you stupid? 】
Console.Write ("Please enter your name:");
String name = Console.ReadLine ();
Console.Write ("Please enter your score:");
Double score = double. Parse (Console.ReadLine ());
If (score >= 0 && score <= 100)
{
if (score = = 100)
{
Console.WriteLine (name+ ", congratulations, full score through!) ");
}
else if (score >= 80)
{
Console.WriteLine (name+ ", you are excellent! Keep going! ");
}
else if (score >= 60)
{
Console.WriteLine (name+ ", good grades!) ");
}
else if (score >= 50)
{
Console.WriteLine (name + ", it's a little bit worse!) ");
}
Else
{
Console.WriteLine (name+ ", Are you stupid? ");
}
}
Else
{
Console.WriteLine ("Wrong input! ");
}

Console.ReadLine ();

Two

Enter the year, month and day to determine if the date format is correct!
Console.Write ("Please enter the Year:");
int year = Int. Parse (Console.ReadLine ());
if (year >= 0 && year <= 9999)
{
Console.Write ("Please enter the month:");
int month = Int. Parse (Console.ReadLine ());
if (month >= 1 && month <= 12)
{
Console.Write ("Please 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 = = | | month = = 12)
{
Console.WriteLine ("The date format is correct, you entered the date: {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, you entered the date: {0}-{1}-{2}.") ", year, month, day);
}
Else//day==31
{
Console.WriteLine ("Date format is wrong! ");
}
}
ELSE//2 Month
{
if (Day <= 28)
{
Console.WriteLine ("The date format is correct, you entered the date: {0}-{1}-{2}.") ", year, month, day);
}
else if (day = = 29)
{
if (year% 4 = = 0 && year%!! = 0 | | year% 400 = = 0)
{
Console.WriteLine ("The date format is correct, you entered the date: {0}-{1}-{2}.") ", year, month, day);
}
Else
{
Console.WriteLine ("Date format is wrong!! ");
}
}
else//day==30| | 31
{
Console.WriteLine ("Date format is incorrect! ");
}
}
}
Else
{
Console.WriteLine ("Day input wrong!! ");
}
}
Else
{
Console.WriteLine ("Incorrect month input!) ");
}
}
Else
{
Console.WriteLine ("The Year is wrong!! ");
}

Console.ReadLine ();

Three

Blind Date process: Do you have a house? Do you have any money? Do you have the power?
"Get married," "Buy a house before you get married," "Make money before you buy a house and then get married."
Using if nesting as a blind date process

Console.WriteLine ("Woman: Do you have a house?") ");
String a = Console.ReadLine ();
if (A = = "has")
{
Console.WriteLine ("Woman: Marriage Bar");
}
Else
{
Console.WriteLine ("Woman: Do you have money?") ");
String B = Console.ReadLine ();
if (b = = "yes")
{
Console.WriteLine ("Woman: First buy a house in marriage");
}
Else
{
Console.WriteLine ("Woman: Do you have the ability?");
String c = Console.ReadLine ();
if (c = = "has")
{
Console.WriteLine ("Woman: First make money and then buy a house in marriage");
}
Else
{
Console.WriteLine ("Woman: Bye ~ ~");
}

}
}
Console.ReadLine ();

Switch case

Switch case must be used in conjunction with break.

Break is a jump statement. When you are in conjunction with the switch case, you jump out of the nearest {}.

Switch case with If, slse if, Slse if....slse (same)

Console.WriteLine ("1. Hamburger");
Console.WriteLine ("2. French fries");
Console.WriteLine ("3. Chicken nuggets");
Console.WriteLine ("4. Chicken Leg");
Console.WriteLine ("5. Chicken rice flower");

Console.Write ("Please enter your choice of item number:");
String a = Console.ReadLine ();

Switch (a)//in parentheses is the variable name that is judged
{
Case "1": The value behind the//case is used to determine that the variables in the brackets above are not equal
Console.WriteLine ("You have chosen a hamburger");
Break;//break Jump statement, jump out of the nearest curly brace
Case "2": a space-separated value between the//case and the value is followed by a colon
Console.WriteLine ("Your choice is French fries");
Break
Case "3":
Console.WriteLine ("You have selected chicken nuggets");
Break
Case "4":
Console.WriteLine ("Your choice is chicken leg");
Break
Case "5":
Console.WriteLine ("Your choice is chicken rice flower");
Break The last one also needs to jump out of curly braces
}

C # statements

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.