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.