1. 程式人生 > >chapter 4 : Flow Control - Note for BEGINNING C#7 Programming with Visual Studio 2017.pdf

chapter 4 : Flow Control - Note for BEGINNING C#7 Programming with Visual Studio 2017.pdf

Boolean Assignment Operators

OPERATOR CATEGORY EXAMPLE EXPRESSION RESULT
&= Binary var1 &= var2; var1 is assigned the value that is the result of var1 & var2.
|= Binary var1 |= var2; var1 is assigned the value that is the result of var1 | var2.
^= Binary var1 ^= var2; var1 is assigned the value that is the result of var1 ^ var2.

^:異或

Conditional Boolean Operators(&& and ||)
If the value of the first operand of the && operator is false, then there is no need to consider the value of the second operand, because the result will be false regardless. Similarly, the || operator returns true if its first operand is true, regardless of the value of the second operand.

Operator Precedence

PRECEDENCE OPERATORS
Highest ++,–(used as prefixes); (), +, -(unary), !, ~
*, /, %
+, -
<<, >>
<, >, <=, >=
==, !=
&
^
|
&&
||
=, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
Lowest ++, --(used as suffixes)

The Ternary Operator
<test> ? <resultIfTrue> : <resultIfFalse>

The Switch Statement
It is illegal for the flow of execution to reach a second case statement after processing one case block.

switch (<testVar>)
{
	case <comparisonVal1>:
		<code to execute if <testVar> == <comparisonVal1> >
		// 在執行一個case語句塊之後不能再執行下一個case語句塊,所以這裡必須加一個break;
		break;
	case <comparisonVal2>:
		<code to execute if <testVar> == <comparisonVal2> >
		break;
}
switch (<testVar>)
{
	case <comparisonVal1>:
		<code to execute if <testVar> == <comparisonVal1> >
		// 實現在執行一個case語句塊後,執行下一個語句塊
		goto case <comparisonVal2>;
	case <comparisonVal2>:
		<code to execute if <testVar> == <comparisonVal2> >
		break;
}
switch (<testVar>)
{
	case <comparisonVal1>:
	case <comparisonVal2>:
		// 對不同的判定結果執行相同的case語句塊
		<code to execute if <testVar> == <comparisonVal1> or <testVar> == <comparisonVal2> >
		break;
}