Introduction
Operators help us do math and make decisions in code easily. They let us add, compare, and combine values quickly.
Operators help us do math and make decisions in code easily. They let us add, compare, and combine values quickly.
int sum = 5 + 3; bool isAdult = age >= 18; bool canEnter = isLoggedIn && hasAccess;
Operators include math (+, -, *, /), comparison (==, !=, >, <), and logic (&&, ||, !).
They work on values to produce new results or decisions.
int total = 10 + 20; bool passed = score >= 50; bool canVote = isCitizen && age >= 18;
int difference = 100 - 45; bool isEqual = name == "Alice"; bool isAllowed = !isBanned;
This program adds two numbers and uses logic to decide if you can eat the fruit.
using System; class Program { static void Main() { int apples = 5; int oranges = 3; int totalFruit = apples + oranges; bool isRipe = true; bool canEat = isRipe && totalFruit > 0; Console.WriteLine($"Total fruit: {totalFruit}"); Console.WriteLine($"Can eat fruit? {canEat}"); } }
Remember, operators follow order of operations like in math.
Using the wrong operator can cause bugs, so double-check your logic.
Operators let you do math and make decisions in code.
They are used to compare, combine, and calculate values.
Understanding operators helps you write clear and correct programs.
== operator do in C#?==== operator compares two values to see if they are the same.= which assigns, == checks equality.== means equality check [OK]== with === assigns valuesa and b in C#?+ is used to add two numbers in C#.int sum = a + b; correctly declares an integer and assigns the sum.+ operator [OK]+ to add numbers, not -, *, or / [OK]- instead of additionint x = 5; int y = 10; bool result = x > y || y == 10; Console.WriteLine(result);
x > yfalse.y == 10true.||true if either side is true, so overall result is true.false || true = true [OK]|| is true if any side is true [OK]|| with &&int a = 7;
int b = 3;
if (a = b) {
Console.WriteLine("Equal");
} else {
Console.WriteLine("Not Equal");
}= which assigns b to a, not compare.== to compare values in conditions.= instead of comparison == in the if condition -> Option D== for comparisons, not = [OK]== to compare in if statements [OK]= in conditionsn is between 10 and 20 inclusive in C#. Which operator combination correctly does this?n to be at least 10 and at most 20, so both conditions must be true.&& to combine conditionsn >= 10 && n <= 20 ensures n is between 10 and 20 inclusive.&& means both conditions true [OK]&& to check both limits together [OK]|| which allows values outside rangePass the test and this pattern moves unknown