Learn C # (Statement 2) C # (QQ: 249178521)
5. Boolean operator
1. Assignment =
2. Is equal to ==! =
3. Logic! && || ^ & |
INT TENS = (9 * 6) / 13;
INT UNITS = (9 * 6)% 13;
Bool isfour = TENS == 4;
BOOL ISTWO = Units == 2;
BOOL HHG;
HHG = ISFOUR & ISTWO;
HHG =! (isfour & iStwo);
hhg =! isfour |! iStwo;
hhg =! hhg;
The result is true when the operator is True when the operator is TRUE. If the number of operands on the left of && is false, then whether the number of operands on the right is false or true, the value of the entire && expression is false.
|| The result is that when the number of operators is one side, as a true is True, it is TRUE. If the number of operands on the left of && is true, then regardless of the number of operands on the right is false or true, the value of the entire && expression is TRUE.
Express the meaning of it.
There is a technology called "short circuit" very useful. For example, the expression on the left can determine if a value is 0, and then the value on the right can use the value as a division. E.g:
IF ((x! = 0) && (Y / X> tolerance)) ...
6.IF statement
String Daysuffix (int days)
{
String result;
IF (days / 10 == 1)
Result = "th";
ELSE IF (days% 10 == 1)
Result = "ST";
ELSE IF (days% 10 == 2)
Result = "nd";
ELSE IF (days% 10 == 3)
Result = "rd";
Else
Result = "th";
Return Result;
}
The conditional expression of the IF statement must be a pure BOOL type expression. For example, the following verses are wrong:
IF (CurrentValue = 0) ...
C # requires all variables to be pre-defined before they can be used, so the following programs are wrong:
Int m;
IF (Inrange)
m = 42;
INT Copy = m; // error, because M may not be assigned.
In C #, variable declaration statements cannot be included in the IF statement, for example:
IF (Inrange)
INT useless; // error
7.switch statement
· Grammar
W for integer class types
The logo after W Case must be a constant time
W No scope of thumbnail form
String Daysuffix (int days)
{
String result = "th";
IF (days / 10! = 1)
Switch (days% 10)
{
Case 1:
Result = "st"; "Break;
Case 2:
Result = "nd"; Break;
Case 3: Result = "rd"; Break;
Default: // Indicates that the situation does not meet the above conditions
Result = "th"; Break;
}
Return Result;
}
You can only use the Switch statement using the Switch statement for user-defined types of integer, strings, or can implicit to integer or string. The CASE flag must be constant when compiling.
The IS key in Visual Basic in C # is compared in Case, for example:
Switch (Expression ())
{
Case IS <42: // Error
Case method (): // error
}
There is no range comparison in C #.
Switch (Expression ())
{
Case 16 to 21: // Error
Case 16..21: // error
}
Note: Each CASE segment must include a BREAK statement, and the default statement is no exception.
8.While / Do
INT DIGIT = 0;
While (Digit! = 10)
{
Console.write ("{0}", DIGIT;
Digit ;
} // No semicolon
INT DIGIT = 0;
DO
{
Console.write ("{0}", DIGIT;
Digit ;
}
While (Digit! = 10); // has a semicolon