# Lesson 4: Operators

{% hint style="warning" %}
At the end of this lesson, you should be able to answer the following:

* What is an operator?
* What is an expression?
* What are some of the commonly used operators in C#?
  {% endhint %}

In the [previous lesson](/csharp-for-beginners/tutorial/lesson-3-data-types.md), we mentioned that the type of a value can determine the *operations* that can be performed with it.

For example, we can add two integers:

```csharp
10 + 2
```

The `+` symbol between the two numbers is an *operator*. It represents an action or operation that can be done to the values supplied with it. The `+` operator here is the same used in arithmetic addition.

The whole line above is called an *expression*. An expression can be evaluated further by C# to produce a new value.

Type the line into your code box and run the program. You'll see that C# evaluates the expression and displays the result.

![](/files/-MeFFmqAb0wBCZGG0KNP)

{% hint style="danger" %}
Why did it display the result even if we didn't call `Console.WriteLine()`?&#x20;

Dotnet Interactive is also a [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) environment. REPL stands for "read-evaluate-print loop". REPL environments allow quick evaluation of an expression, after which the output is displayed.&#x20;

If we write more than one statement in the code box, it won't be a single expression anymore. Instead of doing the read-evaluate-print loop, Dotnet Interactive will compile the program as a whole.
{% endhint %}

We can also do subtraction, multiplication, and division. Each has its own operator.

```csharp
10 - 2    // subtraction
```

```csharp
10 * 2    // multiplication
```

```csharp
10 / 2    // division
```

Type each line in the code box one at a time and run the program each time. The output will be the result of the arithmetic expression.

If you try to write all of them at the same time and run the program, you'll get an error.&#x20;

```csharp
10 + 2;
10 - 2;
10 * 2;
10 / 2;
```

Even if we put semicolons at the end of each line, the program will still be invalid!

![](/files/-MeFG5K7k8B-wcMPSk66)

That's because expressions are not valid statements. As we learned in [Lesson 1](/csharp-for-beginners/tutorial/lesson-1-hello-world.md), C# programs are made up of statements. Expressions in C# programs must be used as part of a valid statement.

What's a statement we've already learned? That's right, the `Console.WriteLine` statement!

Wrap each expression into a `Console.WriteLine` call.

```csharp
Console.WriteLine(10 + 2);
Console.WriteLine(10 - 2);
Console.WriteLine(10 * 2);
Console.WriteLine(10 / 2);
```

The result of each expression is now displayed.

![](/files/-MeFGINyjB8irKIS72QY)

Here are the common operators in C#.

### Arithmetic Operators

These operators work with numeric values.

| Operator | Description        | Example   |
| -------- | ------------------ | --------- |
| `+`      | Addition           | `17 + 18` |
| `-`      | Subtraction        | `65 - 21` |
| `*`      | Multiplication     | `12 * 4`  |
| `/`      | Division           | `60 / 15` |
| `%`      | Remainder (modulo) | `20 % 3`  |

### Comparison Operators

These operators compare two values. The result will either be `true` or `false`.

| Operator | Description              | Example               |
| -------- | ------------------------ | --------------------- |
| `>`      | Greater than             | `5 > 2`               |
| `<`      | Less than                | `1 < 7`               |
| `>=`     | Greater than or equal to | `9 >= 6`              |
| `<=`     | Less than or equal to    | `1000 < 84`           |
| `==`     | Equal to                 | `"apple" == "orange"` |
| `!=`     | Not equal to             | `"apple" != "orange"` |

### Logical Operators

These operators perform [Boolean logic operations](https://en.wikipedia.org/wiki/Boolean_algebra). The result will either be `true` or `false`.

| Operator | Description | Example           |
| -------- | ----------- | ----------------- |
| `&&`     | Logical AND | `true && false`   |
| `\|\|`   | Logical OR  | `false \|\| true` |
| `!`      | Logical NOT | `!false`          |

{% hint style="info" %}
**Question**

Is the following an expression? Why or why not?

```csharp
"100 + 25"
```

{% endhint %}

{% hint style="info" %}
**Question**

True or False: An expression by itself is a valid statement.
{% endhint %}

{% hint style="info" %}
**Challenge**

Wrap each example in the list of operators into a `Console.WriteLine` statement. Can you guess what each result will be?
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codingmama.gitbook.io/csharp-for-beginners/tutorial/lesson-4-operators.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
