Logical and Bitwise Operators
These operators are used for logical and bitwise calculators. Common logical and bitwise operators in C#
are:
The operators &, | and ^ are rarely used in usual programming practice. The NOT operator is used to negate a Boolean or bitwise expression like:
Logical Operators && and || are used to combine comparisons like
In the first comparison: i>3 && j<10 will result in true only if both the conditions i>3 and j<10 result in true.
In the second comparison: i>3 || j<10 will result in true in true if any of the conditions i>3 and j<10 result in true. You can, of course, use both && and || in single statement like:
In the statement we used parenthesis to group our conditional expressions and to avoid any ambiguity.
You can use & and | operators in place of && and || but for combining conditional expressions, && and || are more efficient because they use "short circuit evaluation". For example, if in the expression (i>3 && j<10), i>3 evaluates to false, the second expression j<10 won't be checked and false will be returned (when using AND, if one of the participant operands is false, the whole operands will result in false). Hence, one should be very careful when using assignment expressions with && and || operators. The & and | operator don't do short circuit evaluation and do execute all the comparisons before returning the result.
Other Operators
There are some other operators present in C#. A short description of these is given below:
Operator Precedence
All operators are not treated equally. There is a concept of "operator precedence" in C#. For example:
2 will be multiplied by 6 first then the result will be added to 3. This is because the multiplication operator *
has precedence over the addition operator + . For a complete table of operator precedence, consult MSDN or the .Net framework documentation.
Flow Control And Conditional Statements
The if...else statement
Condition checking has always been the most important construct in any language right from the time of the assembly language days. C# provides conditional statements in the form of the if..else statement. The structure of this statement is:
The else clause above is optional. A typical example is:
In the above example, the console message will be printed only if the expression i==5 evaluates to true. If you would like to take some action when the condition does not evaluate to true, then you can use else clause:
Only the first message will be printed if i is equal to 5. In any other case (when i is not 5), the second message will be printed. If you want to use a block of statements (more than one statement) under if or else, you can enclose your block in { } brackets:
I would always recommend to use { } brackets to enclose the statement following if and else even if only have a single statement. It increases readability and prevents many bugs that otherwise can result if you neglect the scope of if and else statements.
You can also have if after else for further conditioning: