Nikolay Kostov (Nikolay.IT)
Topics covered:
- Comparison and Logical Operators
- The if Statement
- The if-else Statement
- Nested if Statements
- The switch-case Statement
Video (in Bulgarian)
Presentation Content
Comparison Operators
| Operator |
Notation in C# |
| Equals |
== |
| Not Equals |
!= |
| Greater Than |
> |
| Greater Than or Equals |
>= |
| Less Than |
< |
| Less Than or Equals |
<= |
Logical Operators
| Operator |
Notation in C# |
| Logical NOT |
! |
| Logical AND |
&& |
| Logical OR |
|
| Logical Exclusive OR (XOR) |
^ |
- De Morgan laws
!!A -> A
!(A || B) -> !A && !B
!(A && B) -> !A || !B
The if Statement
- The most simple conditional statement
- Enables you to test for a condition
- Branch to different parts of the code depending on the result
Condition and Statement
- The condition can be:
- Boolean variable
- Boolean logical expression
- Comparison expression
- The condition cannot be integer variable (like in C / C++ or JavaScript)
- The statement can be:
- Single statement ending with a semicolon
- Block enclosed in braces
How It Works?
- The condition is evaluated
- If it is true, the statement is executed
- If it is false, the statement is skipped
The if-else Statement
- More complex and useful conditional statement
- Executes one branch if the condition is true, and another if it is false
- If it is true, the first statement is executed
- If it is false, the second statement is executed
Nested if – Good Practices
- Always use { … } blocks to avoid ambiguity
- Even when a single statement follows
- Avoid using more than three levels of nested if statements
- Put the case you normally expect to process first, then write the unusual cases
- Arrange the code to make it more readable
Multiple if-else-if-else-…
- Sometimes we need to use another if -construction in the else block
How switch-case Works?
- The expression is evaluated
- When one of the constants specified in a case label is equal to the expression
- The statement that corresponds to that case is executed
- If no case is equal to the expression
- If there is default case, it is executed
- Otherwise the control is transferred to the end point of the switch statement
Using switch: Rules
- Variables types like string , enum and integral types can be used for switch expression
- The value null is permitted as a case label constant
- The keyword break exits the switch statement
- “No fall through” rule – you are obligated to use break after each case
- Multiple labels that correspond to the same statement are permitted
Using switch – Good Practices
- There must be a separate case for every normal situation
- Put the normal case first
- Put the most frequently executed cases first and the least frequently executed last
- Order cases alphabetically or numerically
- In default use case that cannot be reached under normal circumstances
Summary
- Comparison and logical operators are used to compose logical conditions
- The conditional statements if and if-else provide conditional execution of blocks of code
- Constantly used in computer programming
- Conditional statements can be nested
- The switch statement easily and elegantly checks an expression for a sequence of values