15-07-02 Statement-Branch statement

Source: Internet
Author: User
Tags case statement switch case

Statement:

The types of statements include declaration statements, expression statements, select statements, loop statements, jump statements, exception statements.

One: declaration statement: Into the new variable and solid. Variable declarations can optionally assign values to variables. The value must be assigned in the re-const declaration.

Cases:

int i = 0; Declare the variable i and assign a value, or you can not assign a value.

Double A;

Two: expression statement: An expression statement used to calculate a value does not store the value in a variable.

Cases:

sum = i + j; Variables I and J must be assigned before this, and sum also needs to declare the type.

int x = a + b; or perform the operation at the same time as the declaration.

Three: SELECT statement: If,else,switch,case

Four: Circular statement: Do,for,foreach,while

V: Jump statement: Break,continue,default,retrun

VI: Exception statement: try-catch-finally

Sequential statements: Execute from top to bottom

Branch statements

1.if,else statements

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.

1. if (...) The conditions are judged in parentheses.
{
Execute when conditions are met;
}

2. if (...) The conditions are judged in parentheses.

{
Execute when conditions are met;
}
Else
{
Execution when conditions are not met;
}

3. if (...) The conditions are judged in parentheses.
{
Execute when condition 1 is met;
}
else if (...)
{
satisfies the condition 2 if the condition 1 is not satisfied;
}

4. if (...) Inside the brackets is the judging condition 1
{
if (...)
{
It satisfies the condition 1 and satisfies the condition 2 when executes;
}
}

else//finally does not meet the above conditions of execution here's the code

{

}

Example: 1. Do you have any money? Do you have a room? Do you have a car? Do you have the power?

Console.WriteLine ("do you have any money? "); stringA =Console.ReadLine (); if(A = ="No") {Console.WriteLine ("do you have a room? "); stringb =Console.ReadLine (); if(b = ="No") {Console.WriteLine ("do you have a car? "); stringc =Console.ReadLine (); if(c = ="No") {Console.WriteLine ("do you have the power? "); stringD =Console.ReadLine (); if(d = ="No") {Console.WriteLine ("There's nothing, roll Duzi! "); }                    Else if(d = ="have a") {Console.WriteLine ("then you have to work hard to make money!"); }                    Else{Console.WriteLine ("What exactly are you trying to say?"); }                }                Else if(c = ="have a") {Console.WriteLine ("It's good to have a car, you can travel in the future."); }                  Else{Console.WriteLine ("can you talk ?"); }            }            Else if(b = ="have a") {Console.WriteLine ("room is also good, let's talk about it! "); }                Else{Console.WriteLine ("What are you talking about? "); }        }            Else if(A = ="have a") {Console.WriteLine ("let's get married! "); }            Else{Console.WriteLine ("don't want to talk about rolling Duzi! "); }

2. The largest of the three numbers

Console.WriteLine ("Please enter the first number:"); intA =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Please enter a second number:"); intb =Convert.ToInt32 (Console.ReadLine ()); Console.WriteLine ("Please enter a third number:"); intc =Convert.ToInt32 (Console.ReadLine ()); if(A >=b) {if(A >=c) {Console.WriteLine ("three of the total"+a+"Maximum"); }                Else{Console.WriteLine ("three of the total"+c+"Maximum"); }            }            Else            {                if(b >=c) {Console.WriteLine ("three of the total"+b+"Maximum"); }                Else{Console.WriteLine ("three of the total"+c+"Maximum"); }            }


2.switch Case Statement

Switch case must be used in conjunction with break.

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

Cases:

Console.WriteLine ("Check the charges, please press 1, check the balance please press 2, check the flow please press 3, business please press 4, broadband please press 5, group business Please press 7, Labor service please press 0"); intA =Convert.ToInt32 (Console.ReadLine ()); Switch(a) { Case 1: Console.WriteLine ("your monthly call is 534 yuan.");  Break;  Case 2: Console.WriteLine ("your balance is low");  Break;  Case 3: Console.WriteLine ("your remaining traffic for the month is 27M");  Break;  Case 4: Console.WriteLine ("There's no business to be done.");  Break;  Case 5: Console.WriteLine ("wideband instability, not recommended for installation");  Break;  Case 6: Console.WriteLine ("Do you want to advertise?");  Break;  Case 0: Console.WriteLine ("We're off work, come back tomorrow.");  Break; default: Console.WriteLine ("Welcome to call China Pit Daddy Hotline, please check and then broadcast");  Break; }

15-07-02 Statement-Branch statement

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.