-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db6e6c9
commit 2597e0a
Showing
7 changed files
with
138 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,45 @@ | ||
# Logical | ||
Logical operators are used to combine two or more conditions to determine the logic between values | ||
|
||
- Not | ||
- exclamation ! | ||
- AND | ||
- Double ampersand && | ||
- OR | ||
- Double piped || | ||
- Short-circuiting | ||
> Short-circuiting refers to a feature of logical operators in programming languages that prevents unnecessary computation when the result of an expression becomes determinable based on earlier parts of the expression. Specifically, for the `OR` operator (`||`), short-circuiting means that once a true value is found among the operands, the entire expression returns true without evaluating subsequent operands. Conversely, for the `AND` operator (`&&`), short-circuiting means that once a false value is found among the operands, the entire expression returns false without evaluating subsequent operands | ||
Any non-zero value in case of C is considered as True | ||
|
||
Order of precedence NOT -> AND -> OR | ||
- NOT Operator (`!`) | ||
|
||
- The **Not Operator** is represented by an exclamation mark (!). It reverses the logical state of its operand. It converts true to false and false to true. | ||
- In this example, the condition x > 5 evaluates to false, but the ! operator reverses it, so the message will be printed. | ||
```c | ||
int x = 5; | ||
if (!(x > 5)) { | ||
printf("x is not greater than 5\n"); | ||
} | ||
``` | ||
- AND Operator (`&&`) | ||
|
||
- The AND Operator is represented by double ampersands (&&). It returns true only if both operands are true. For instance: | ||
- In this case, both x > 5 and y < 10 are true, so the message will be printed. | ||
```c | ||
int x = 6, y = 8; | ||
if (x > 5 && y < 10) { | ||
printf("Both conditions are true\n"); | ||
} | ||
``` | ||
|
||
- OR Operator (`||`) | ||
- The OR Operator is represented by double pipes (||). It returns true if at least one of the operands is true. | ||
- In this example, x > 5 is false, but y < 10 is true, so the message will still be printed. | ||
```C | ||
int x = 3, y = 12; | ||
if (x > 5 || y < 10) { | ||
printf("At least one condition is true\n"); | ||
} | ||
``` | ||
|
||
```admonish note title = "Short-curcuiting" | ||
Short-circuiting refers to a feature of logical operators in programming languages that prevents unnecessary computation when the result of an expression becomes determinable based on earlier parts of the expression. | ||
Specifically, for the `OR` operator (`||`), short-circuiting means that once a true value is found among the operands, the entire expression returns true without evaluating subsequent operands. | ||
Conversely, for the `AND` operator (`&&`), short-circuiting means that once a false value is found among the operands, the entire expression returns false without evaluating subsequent operands | ||
``` | ||
|
||
> Any non-zero value in case of C is considered as True | ||
> | ||
> Order of precedence NOT -> AND -> OR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,31 @@ | ||
# Relational | ||
|
||
`>`, `<`, `>=`, `<=`, `==`, `!=` | ||
Relational operators are fundamental for comparing two values. These operators evaluate the relationship between operands and return a Boolean value (1 for true or 0 for false). | ||
|
||
Relational operators are used to compare two values. | ||
- `>` Greater Than | ||
- `<` Less Than | ||
- `>=` Greater Than or Equal To | ||
- `<=` Less Than or Equal To | ||
- `==` Equal To | ||
- `!=` Not Equal To | ||
|
||
- associativity of relation is Left to Right | ||
## Key Points | ||
|
||
- Precedence of arithmetic is more than of Relational | ||
- Associativity of relational operators is Left to Right | ||
|
||
- Precedence of Arithmetic Operators is more than of Relational Operators | ||
|
||
```C | ||
int a = 5, b = 2, c = 1, d; | ||
d = c + a > b; | ||
// (c + a) > b | ||
// (1 + 5) > b | ||
// 6 > 2 | ||
// for True comparisons the output will be 1. | ||
// Since the comparison is true, the output will be 1. | ||
``` | ||
|
||
## Additional Point | ||
|
||
- Character Comparison: | ||
- Relational operators can also be used to compare characters in C. Characters are compared based on their ASCII values. For example, `'a' < 'b'` would evaluate to true because the ASCII value of `'a'` (97) is less than that of `'b'` (98). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters