3 - NPL - Method, Operators, Conditional Statement
3 - NPL - Method, Operators, Conditional Statement
NET Programming
Language
Method, Operators, Conditional statement
1. Method
2. Operators
3. Conditional statements
01
This is a sample text that you can edit.
03 You can change font(size, color, name),
09e-BM/DT/FSOFT - @FPT SOFTWARE - FPT Software Academy - Internal Use 4
or apply any desired formatting.
Section 1
Operators
Exception handling
statements
The fix statement
Checked and unchecked The lock statement
An operation is an action
performed on single or multiple
Expressions in C# comprise one or The symbol is called an operator
values stored in variables in order
more operators that performs some and it determines the type of action
to modify them or to generate a
operations on variables. to be performed on the value.
new value with the help of
minimum one symbol and a value.
Logical Operators
Boolean Logical
Operators Bitwise Logical Operators
a !a
true false
false true
x y y&y
true true true
true false false
false true false
false false false
x y y&y
true true true
true false true
false true true
false false false
x y x^y
true true false
true false true
false true true
false false false
5 <, <=, >, >= Less than, Less than or equal to, Greater than, Left to Right
Greater than or equal to
6 ==, != Equal to, Not Equal to Left to Right
Conditional statement
if (condition)
{
// one or more statements;
}
where
condition: Is the boolean expression.
statements: Are set of executable instructions executed when the boolean expression
returns true.
The last case block (or the default C# introduced the no fall-through
block) also needs to have a rule to allow the compiler to
statement like break or goto to rearrange the order of the case
explicitly take the control outside blocks for performance
the switch…case construct. optimization.
whereas
A switch statement tests expressions based only on a single integer, enumerated value, or
String object.
When compiler compiles a switch statement, it will inspect each of the case constants and
create a “jump table” that it will use for selecting the path of execution depending on the value
of the expression. Therefore, if we need to select among a large group of values, a switch
statement will run much faster than the equivalent logic coded using a sequence of if-else.
The compiler can do this because it knows that the case constants are all the same type
and simply must be compared for equality with the switch expression, while in case of if
expressions, the compiler has no such knowledge.
If-else conditional branches are great for variable conditions that result into a boolean,
whereas
4. Speed
A switch statement might prove to be faster than ifs provided number of cases are good. If
there are only few cases, it might not effect the speed in any case. Prefer switch if the number
of cases are more than 5 otherwise, you may use if-else too.
If a switch contains more than five items, it’s implemented using a lookup table or a hash list.
This means that all items get the same access time, compared to a list of ifs where the last
item takes much more time to reach as it has to evaluate every previous condition first.
5. Clarity in readability
A switch looks much cleaner when you have to combine cases. Ifs are quite vulnerable to
errors too. Missing an else statement can land you up in havoc. Adding/removing labels is
also easier with a switch and makes your code significantly easier to change and maintain.
6. Conversion
BUT
Method
• Its copy is passed instead of the instance itself. • a reference to the object is passed
• changes to the argument have no effect on the • the change is reflected in the argument in the
original instance in the calling method. calling method