Operators and Expressions in C#
Imagine you're buying snacks from a shop. You add prices, subtract discounts, compare which item is cheaper, and decide whether you have enough money.
Funny thing? Computers do the exact same things, using operators.
Today, you’ll teach C# how to add, compare, and make decisions using expressions, and then build a small program that calculates a bill automatically.
By the end of this lesson, your program will do math for you.
The Project: Simple Bill Calculator
You will build a tiny app that prints the cost of three items and the total bill:
1 2 3 4 5 6 7 8======================== SIMPLE BILL Item 1: 120 Item 2: 80 Item 3: 50 -------------------- Total: 250 ========================
This mini-project uses arithmetic operators just like real shopping apps. Operators are symbols that tell C# what action to perform.
You’ll use three categories today:
1. Arithmetic Operators
Used for basic math:
- + addition
- - subtraction
- * multiplication
- / division
- % remainder
Example:
1int total = 5 + 3; // total becomes 8
2. Relational Operators
Used to compare values:
- == equal
- != not equal
- > greater than
- < less than
- >= greater or equal
- <= less or equal
Example:
1bool result = 10 > 5; // true
These become powerful when combined with if–else statements later.
3. Logical Operators
Used to connect multiple conditions:
- && AND → both must be true
- || OR → at least one is true
- ! NOT → reverses the value
Example:
1bool ok = (age > 12) && (age < 20);
Increment & Decrement
Sometimes you want to increase or decrease a value by 1.
- x++ → x = x + 1
- x-- → x = x - 1
Example:
1 2int count = 0; count++; // becomes 1
This is very useful in loops (coming soon!).
Operator Precedence
Just like math, some operations happen before others.
Example:
1int x = 5 + 3 * 2; // result is 11, not 16
Because multiplication (*) happens before addition (+). If you want to control the order, use parentheses:
1int y = (5 + 3) * 2; // result is 16
Let’s Build the Simple Bill Calculator
Here is the full program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22using System; class Program { static void Main() { int item1 = 120; int item2 = 80; int item3 = 50; int total = item1 + item2 + item3; Console.WriteLine("========================"); Console.WriteLine(" SIMPLE BILL"); Console.WriteLine($" Item 1: {item1}"); Console.WriteLine($" Item 2: {item2}"); Console.WriteLine($" Item 3: {item3}"); Console.WriteLine(" --------------------"); Console.WriteLine($" Total: {total}"); Console.WriteLine("========================"); } }
This is your first “math-powered” C# program.
How It Works
- Three variables store item prices.
- The expression item1 + item2 + item3 calculates the total automatically.
- String interpolation prints neat, readable output.
If you change one price, the total updates instantly, no extra work!
Run it in the DevsCall AI Code Runner to see it in action.
Learn Together with AI
Try prompts like:
- “Change the bill calculator to include a 10% discount.”
- “Add tax calculation to my C# bill program.”
- “Explain operator precedence with more examples.”
- “Write a version where user enters the item prices.”
AI can help you build more advanced calculators step-by-step.
Practice Time
Create a program that:
- Stores the prices of 4 grocery items
- Calculates the total price
- Prints everything in this format:
1 2 3 4 5 6 7 8 9======================== GROCERY BILL Apple: 100 Milk: 180 Bread: 140 Eggs: 200 -------------------- Total: 620 ========================
Use arithmetic operators to compute the total.
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24using System; class Program { static void Main() { int apple = 100; int milk = 180; int bread = 140; int eggs = 200; int total = apple + milk + bread + eggs; Console.WriteLine("========================"); Console.WriteLine(" GROCERY BILL"); Console.WriteLine($" Apple: {apple}"); Console.WriteLine($" Milk: {milk}"); Console.WriteLine($" Bread: {bread}"); Console.WriteLine($" Eggs: {eggs}"); Console.WriteLine(" --------------------"); Console.WriteLine($" Total: {total}"); Console.WriteLine("========================"); } }
If your output matches, you’ve mastered expressions and operators!
Frequently Asked Questions
Operators are symbols that tell the computer to perform actions like addition, comparison, or logical checking.
They perform basic math operations such as addition (+), subtraction (-), multiplication (*), division (/), and remainder (%).
Relational operators compare values (e.g., >, <, ==) and return true or false.
Logical operators (&&, ||, !) combine or modify conditions, mostly used in decision-making statements.
It determines which operation runs first. For example, multiplication happens before addition unless parentheses change the order.
Yes. You can combine variables and values to calculate totals, compare results, or build conditions.
Still have questions?Contact our support team