C # if, ifelse statement,

Source: Internet
Author: User

C # if, ifelse statement,

1. The if statement is determined based on the value of the Boolean expression.

The if statement can be either of the following methods:

Code(1)if(boolean)       do (2)if(boolean){ do}

Generally, the second method is used, because the first method only executes the first statement after the if condition.

For example:

Codeif(Boolean)  Console.WriteLine("ok1");  Console.WriteLine("ok2"); 


No matter whether the value of boolean is true or false, ok2 will always output, but it will be different if you use the second method.

2. if-else statement

The if -- else statement and the if statement have the same judgment conditions. The code is executed based on the boolean result of the condition. However, if-else has an alternative opportunity than if. Just like sending a prize in a competition, if you lose all the prizes, you can also get a consolation prize if you add an else.

If-else statement format is, must have braces

Codeif(Boolean){  do-something;}else{  do-something;}

If we only want to perform simple if-else operations, we can use a very simple method provided by C # without using the if-else statement, such

Codeint i=0;
      int j=0;
If (I> 0) {j = 1;} else {j = 2 ;}


It is easy to assign an I value. We can abbreviated it to int I = 0; int j = I> 0? .

3. Statement nesting

If, if-else statements can be nested with each other

Codeif(boolean){   if(boolean){         do something;     }else{       do something;    }}if(boolean){     if(boolean){    do something;   }}else{   if(Boolean){   do something; }}
Today, it is time to take a moment to carefully understand the knowledge.

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.