If --- (switch-case) Statement

Source: Internet
Author: User

 

Daily sentence: Happiness is about having each tiny wish come true. Happiness means reaching every Tiny Wish.

 

Ctrl + e d c # automatic layout.

Forced conversion:

If the expression contains an operand of the double type, the entire expression is upgraded to the double type.

Int a = (int) 3.14; Convert 3.14 to the int type forcibly and assign the value to.

 

Int variable Convert. ToInt32 (Console. ReadLine (input string); converts the input string to int type.

 

Convert conversion is not only a memory-level conversion, but a conversion of data meaning. Convert is a process of processing and conversion.
(Why switch ?!)
Convert. ToInt32 ();
Convert. ToString (); (all types can be converted to the string type)

Int a = 10;
Console. WriteLine (a. ToString (); converts int to string type.

Main (a = age = Console. WriteLine (number = Convert. ToInt32 (Console. ReadLine (); Console. WriteLine (View Code

 

Short circuit between logic and logic

Logic and short circuit: when the first expression is invalid, the subsequent expression is no longer executed.

Int a = 10;

Int B = 15;

Bool result = ++ a> 15 & ++ B> 10;

Console. WriteLine ("the value of a is {0} B and the value of B is {1}", a, B );

When the first bool expression ++ a> 15 is invalid for the execution logic and (&), the next ++ B> 10 will not be skipped, And the last value of a is 11, the value of B is still 15.

+ + B> 15 is run only when ++ a> 15 is set. The values of a and B are added with 1. That is, a = 11, B = 16.

 

Logical or short circuit: when the first expression is set, the subsequent expression is no longer run. (| if one is set)

Int a = 10;

Int B = 15;

Bool result = ++ a <15 | ++ B> 10;

Console. WriteLine ("the value of a is {0} B and the value of B is {1}", a, B );

Because ++ a <15 is true. The value of ++ B> 10 is not executed, the value of a is 11, and the value of B is still 15.

Main (a = B = result = ++ a >&++ B>View Code

 

If structure (if/if-else if)

How can I optimize the program through multiple exercises? when using the if statement, the above three questions are selected.

If structure ()


If (bool expression) ---- must be a bool expression
{
Statement 1
}


If-else Structure
If (condition)
{Statement 1 ;}
Else
{Statement 2 ;}

If-else if structure (only when the if is not true will it enter the else if for judgment)

Main (secret =View Code Main (number = (number >=( number >=View Code Main (useName = useSecret = (useName ==& useSecret =View Code Main (age = (age> =View Code Main (Console. WriteLine (age = (age >=( age >=input = (input =View Code

 

(In-depth understanding and application are also required)

Control the output of some statements: Apply the bool expression and add the if structure.

Bool variable = false;

If (variable = false)

{

Specifies whether the program is output.

}

It can solve some complicated code for if statements.

Structure:

Switch (expression)

Case: "value 1 ":

Statement Block 1;

Break;

......

Case: "value n ":

Statement Block n;

Break;

Default: Statement block;

Break;

In the above syntax, break is required for every sentence. the values in the expression and the values in case are compared one by one until they are found to be equal. If they are not found, they will jump into the default statement and execute the program in the default statement,

Without the default statement, the program jumps out.

Main (input = salary = sign = + =-= (sign = +View Code

 

Long way to go ......

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.