If else if else

Source: Internet
Author: User

1, If-else

Grammar:

if (condition)

{Statement 1;}

Else

{Statement 2;}

Execution process:

If the condition is true, the statement block 1 of the if band is executed and the statement block 2 of the else Band is skipped, and if the condition is false, the statement block 1 of the If band is skipped, and the statement block 2 of the else band is executed.

Example:

if (score>=90)

{

Console.WriteLine ("Dad rewards 100 Yuan");

}

Else

{

Console.WriteLine ("Write Learning Summary 100 times");

}

2, If-else-if

Grammar:

if (condition 1)

{Statement 1;}

else if (condition 2)

{Statement 2;}

Else

{Statement 3;}

Execution process:

When the condition 1 is not established, will enter the next if statement and the condition after the IF statement, once there is a condition after a if is true, then executes the statement block with this if, after the statement block execution completes, the program jumps out of the IF-ELSE-IF structure, if all the if conditions are not established, Executes the statement block with else, otherwise nothing is executed

Example:

if (score>=90)

{

Console.WriteLine ("A");

}else if (score>=80)

{

Console.WriteLine ("B");

}

Else

{

Console.WriteLine ("C");

}

3, Switch-case

Grammar:

switch (expression) {

Case value 1: statement block 1;

Break

Case Value 2: statement block 2;

Break

Default: statement block 3;

Break

Execution process:

The expression is evaluated first, and then according to the value followed by the match case, if there is a match, the statement following the match is executed until the break statement jumps out of switch-case, and if all case values do not match, then there is default, Executes the statement after default until the break ends and, if there is no default, jumps out of swtich-case and does nothing.

Example:

Switch (SCORE/10)

{

Case 9:

Console.WriteLine ("A");

Break

Case 8:

Console.WriteLine ("B");

Break

Case 7:

Console.WriteLine ("C");

Break

Case 6:

Console.WriteLine ("D");

Default

Console.WriteLine ("E");

Break

}

Summary: Comparison of if-else-if and switch:

The same point: can achieve multi-branch offices;

Different points: switch generally can only be used for equivalent comparison;

If-else-if can handle the range;

If else if else

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.