C # Branch Statements

Source: Internet
Author: User
Tags case statement switch case

In C #, statements are mainly divided into sequential statements, branch statements, and loop statements. In this part of the study, the main explanation of the branch statement. The branch statement mainly includes the IF statement and the switch statement.
The IF statement consists of 4 main formats:
1.if ()
{}
2.if ()
{}
else{}
If the if is satisfied, the if is executed, the else is no longer executed, and if the if is not satisfied, the else will be executed. (both must be selected)
3.if ()
{}
else if ()
{}
Else
{}
As long as the above has an if or else if satisfies the condition, executes, from him all the following does not need to judge read again.
Nesting of 4.if.

Example: Enter the year, month, and date to determine if the format you entered is correct.

Console.WriteLine ("Please enter a year:");
int year = Int. Parse (Console.ReadLine ());
if (year >= 0 && year <= 9999)
{
Console.WriteLine ("Please enter the month:");
int month = Int. Parse (Console.ReadLine ());
if (month >= 1 && month <=)
{
Console.WriteLine ("Please enter Date:");
int date = Int. Parse (Console.ReadLine ());
if (date >= 1 && date <=)
{
if (month = = 1 | | month = = 3 | | month = = 5 | | month = = 7 | | month = = 8 | | month = = Ten | | month = =
{
Console.WriteLine ("The time you entered is:" + year + "-" + month + "-" + date);
}
else if (month = = 4 | | month = = 6 | | month = = 9 | | month = =)
{
if (date >= 1 && date <=)
{
Console.WriteLine ("The time you entered is:" + year + "-" + month + "-" + date);
}
Else
{
Console.WriteLine ("You entered the wrong date!") ");
}
}
Else
{
if (date >= 1 && date <=)
{
Console.WriteLine ("The time you entered is:" + year + "-" + month + "-" + date);
}
else if (date = =)
{
if (year% 4 = = 0 && year%!! = 0 | | year% = = 0)
{
Console.WriteLine ("The time you entered is:" + year + "-" + month + "-" + date);
}
Else
{
Console.WriteLine ("You entered the wrong date!") ");
}
}
Else
{
Console.WriteLine ("You entered the wrong date!") ");
}
}
}
Else
{
Console.WriteLine ("You entered the wrong date!") ");
}
}
Else
{
Console.WriteLine ("The Month you entered is incorrect!) ");
}
}
Else
{
Console.WriteLine ("You entered the wrong year!") ");
}
console.readline ();

Example: A 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 ("Do you have a house?" ");
string S =console.readline ();
if (s = = "has")
{
Console.WriteLine ("Marriage Bar");
}
Else
{
Console.WriteLine ("Are You rich?" ");
string b = console.readline ();
if (b = = "yes")
{
Console.WriteLine ("Buy a house before you get Married");
}
Else
{
Console.WriteLine ("Do you have the ability?" ");
string c = Console.ReadLine ();
if (c = = "has")
{
Console.WriteLine ("Make money before you buy a house and then get Married");
}
Else
{
Console.WriteLine ("Goodbye ~ ~");
}
}
}
console.readline ();

The switch case statement is similar to the third format in the IF statement, if () {}else if () {}else{}, and is used to represent multiple selections, which can be converted to each other under certain conditions.

The format of the switch statement is:

switch (s)//In parentheses is the variable name
{
Case "1":
Break
Case "2":
Break
Case "3":
Break
Case "4":
Break
Default
Break
}

Example: What is the day of the year when judging a certain month? Assume that February of this year has 28 days.

int m1 = to, M2 =, M3 = To, M4 =, M5 = To, M6 =, M7 =-M8 = =, M9 = +, M10 = +, M11 = +;
Console.WriteLine ("Please enter the month:");
int m = Int. Parse (Console.ReadLine ());
Console.WriteLine ("Please enter the date:");
int d = Int. Parse (Console.ReadLine ());
switch (m)
{
Case 1:
Console.WriteLine ("+d+", "Day");
Break ;
Case 2:
Console.WriteLine ("First" + (M1+D) + "Day");
Break ;
Case 3:
Console.WriteLine ("First" + (M1+M2+D) + "Day");
Break ;
Case 4:
Console.WriteLine ("First" + (m1+m2+m3+ D) + "Day");
Break ;
Case 5:
Console.WriteLine ("First" + (m1+m2+m3+m4+ D) + "Day");
Break ;
Case 6:
Console.WriteLine ("First" + (M1+M2+M3+M4+M5+D) + "Day");
Break ;
Case 7:
Console.WriteLine ("First" + (m1+m2+m3+m4+m5+m6+ D) + "Day");
Break ;
Case 8:
Console.WriteLine ("First" + (M1+M2+M3+M4+M5+M6+M7+D) + "Day");
Break ;
Case 9:
Console.WriteLine ("First" + (M1+M2+M3+M4+M5+M6+M7+M8+D) + "Day");
Break ;
Case Ten:
Console.WriteLine ("First" + (M1+M2+M3+M4+M5+M6+M7+M8+M9+D) + "Day");
Break ;
Case One :
Console.WriteLine ("First" + (M1+M2+M3+M4+M5+M6+M7+M8+M9+M10+D) + "Day");
Break ;
Case :
Console.WriteLine ("First" + (M1+M2+M3+M4+M5+M6+M7+M8+M9+M10+M11+D) + "Day");
Break ;
Default:
Console.WriteLine ("You entered the wrong!") ");
Break ;
}

C # Branch 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.