Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Grundlegende Operatoren und Ausdrücke | Getting Started
C# Grundlagen

bookGrundlegende Operatoren und Ausdrücke

Operators are symbols or a combination of symbols that perform various operations on values or variables.

On the other hand, an expression is a combination of values and operators that output (or return) an evaluated value. For example 7 + 9 is an expression that returns 16, and 7 * 9 is an expression that returns 63 as the * operator is the multiplication operator.

You can write expressions inside the System.Console.Write or System.Console.WriteLine method to see their output:

main.cs

main.cs

copy
1
System.Console.WriteLine(7 + 9);

You can also store the result of expressions inside variables:

main.cs

main.cs

copy
12
var result = 17 + 27; System.Console.WriteLine(result);

In this chapter, we will look at the arithmetic operators. Most of the remaining operators will be discussed in the later sections where relevant.

Example of Using This Expressions:

System.Console.WriteLine(1 + 2 + 3 + 4); // Outputs: 10

Note

The operators are always evaluated from left to right. For-example, if we have the statement 200 / 10 / 5 / 2, the order of operations will be:
200 / 10 / 5 / 220 / 5 / 24 / 2 -> 2.

A statement having multiple arithmetic operators is evaluated based on the BODMAS (also known as PEMDAS) rule by default.

BODMAS is an acronym for Brackets, Order (Exponent), Division, Multiplication, Addition, and Subtraction. It defines the order of operations from the highest to the lowest priority of execution:

  1. Brackets;
  2. Exponent;
  3. Division;
  4. Multiplication;
  5. Addition;
  6. Subtraction.

The following diagram shows the general order of operations in visual form:

Note

C# does not have an operator for exponents, instead we use a method when we want to raise a number to some power.

Here is an example which shows the order of execution:

main.cs

main.cs

copy
12
int result = (10 + 5) * 2 - 8 / 4 + 1; System.Console.WriteLine(result);

The statement in the code above is executed in the following order:

Similarly, in the case of nested brackets, inner brackets are solved first:

main.cs

main.cs

copy
12
int result = ((20 - 4) * 2) + 4; System.Console.WriteLine(result);

Process: ((20 - 4) * 2) + 4((16) * 2) + 4(32) + 436

We can also store values inside variables and perform operations on them:

main.cs

main.cs

copy
1234567891011121314151617
namespace TestConsoleApp { internal class Program { static void Main(string[] args) { var value_1 = 10; var value_2 = 7; System.Console.WriteLine("Value 1: " + value_1); System.Console.WriteLine("Value 2: " + value_2); var sum = value_1 + value_2; System.Console.WriteLine("Sum: " + sum); var result = (value_1 + 10) / 2; System.Console.WriteLine("(Value_1 + 10) / 2: " + result); } } }

Note

An expression can include operators, numbers, and variables. In the code above, examples of expressions are
value_1 + value_2 and (value_1 + 10) / 2. Each expression results in a value.

question mark

What will be the output of the statement: (6 * 3) + 12 / 4 ?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 10

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how the increment (++) and decrement (--) operators work in C#?

What happens if I use parentheses to change the order of operations in an expression?

Can you give more examples of expressions using variables?

bookGrundlegende Operatoren und Ausdrücke

Swipe um das Menü anzuzeigen

Operators are symbols or a combination of symbols that perform various operations on values or variables.

On the other hand, an expression is a combination of values and operators that output (or return) an evaluated value. For example 7 + 9 is an expression that returns 16, and 7 * 9 is an expression that returns 63 as the * operator is the multiplication operator.

You can write expressions inside the System.Console.Write or System.Console.WriteLine method to see their output:

main.cs

main.cs

copy
1
System.Console.WriteLine(7 + 9);

You can also store the result of expressions inside variables:

main.cs

main.cs

copy
12
var result = 17 + 27; System.Console.WriteLine(result);

In this chapter, we will look at the arithmetic operators. Most of the remaining operators will be discussed in the later sections where relevant.

Example of Using This Expressions:

System.Console.WriteLine(1 + 2 + 3 + 4); // Outputs: 10

Note

The operators are always evaluated from left to right. For-example, if we have the statement 200 / 10 / 5 / 2, the order of operations will be:
200 / 10 / 5 / 220 / 5 / 24 / 2 -> 2.

A statement having multiple arithmetic operators is evaluated based on the BODMAS (also known as PEMDAS) rule by default.

BODMAS is an acronym for Brackets, Order (Exponent), Division, Multiplication, Addition, and Subtraction. It defines the order of operations from the highest to the lowest priority of execution:

  1. Brackets;
  2. Exponent;
  3. Division;
  4. Multiplication;
  5. Addition;
  6. Subtraction.

The following diagram shows the general order of operations in visual form:

Note

C# does not have an operator for exponents, instead we use a method when we want to raise a number to some power.

Here is an example which shows the order of execution:

main.cs

main.cs

copy
12
int result = (10 + 5) * 2 - 8 / 4 + 1; System.Console.WriteLine(result);

The statement in the code above is executed in the following order:

Similarly, in the case of nested brackets, inner brackets are solved first:

main.cs

main.cs

copy
12
int result = ((20 - 4) * 2) + 4; System.Console.WriteLine(result);

Process: ((20 - 4) * 2) + 4((16) * 2) + 4(32) + 436

We can also store values inside variables and perform operations on them:

main.cs

main.cs

copy
1234567891011121314151617
namespace TestConsoleApp { internal class Program { static void Main(string[] args) { var value_1 = 10; var value_2 = 7; System.Console.WriteLine("Value 1: " + value_1); System.Console.WriteLine("Value 2: " + value_2); var sum = value_1 + value_2; System.Console.WriteLine("Sum: " + sum); var result = (value_1 + 10) / 2; System.Console.WriteLine("(Value_1 + 10) / 2: " + result); } } }

Note

An expression can include operators, numbers, and variables. In the code above, examples of expressions are
value_1 + value_2 and (value_1 + 10) / 2. Each expression results in a value.

question mark

What will be the output of the statement: (6 * 3) + 12 / 4 ?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 10
some-alt