Posts

Showing posts with the label C# Operators

C# Comparison Operators

C# Comparison Operators Comparison operators in C# are used to compare values and return a boolean result (true or false). They are essential for making decisions and controlling the flow of your program. Here's a table of the common comparison operators: | Operator | Description | Example | |---|---|---| | == | Equal to | if (x == y) | | != | Not equal to | if (x != y) | | > | Greater than | if (x > y) | | < | Less than | if (x < y) | | >= | Greater than or equal to | if (x >= y) | | <= | Less than or equal to | if (x <= y) | Example: int x = 10; int y = 5; // Using comparison operators bool isEqual = x == y; // False bool isGreater = x > y; // True bool isLessOrEqual = x <= y; // False // Using comparison operators in an if statement if (x > y) {     Console.WriteLine("x is greater than y"); } else {     Console.WriteLine("x is not greater than y"); } Important Notes:  * Null Comparison: When comparing reference types (like str...

C# Increment and Decrement Operators

C# Increment and Decrement Operators In C#, the increment (++) and decrement (--) operators are used to increase or decrease the value of a numeric variable by 1. They can be used in both prefix and postfix modes. Prefix Mode:  * Increment: ++x    * Increments the value of x by 1, then returns the new value.  * Decrement: --x    * Decrements the value of x by 1, then returns the new value. Postfix Mode:  * Increment: x++    * Returns the current value of x, then increments it by 1.  * Decrement: x--    * Returns the current value of x, then decrements it by 1. Example: int x = 5; // Prefix increment int y = ++x; // x becomes 6, y becomes 6 // Postfix increment int z = x++; // z becomes 6, x becomes 7 // Prefix decrement int a = --x; // x becomes 6, a becomes 6 // Postfix decrement int b = x--; // b becomes 6, x becomes 5 Console.WriteLine("x: " + x); Console.WriteLine("y: " + y); Console.WriteLine("z: " + z); Console.WriteLine(...