Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Basic Operators & Expressions | Getting Started
C# Basics

Basic Operators & ExpressionsBasic Operators & Expressions

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:

cs

main.cs

You can also store the result of expressions inside variables:

cs

main.cs

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 Usage:

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:

cs

main.cs

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

The steps explained in the picture are executed below:

  • Expression: (10 + 5) * 2 - 8 / 4 + 1)
  • Step 1: 15 * 2 - 8 / 4 + 1
  • Step 2: 15 * 2 - 2 + 1
  • Step 3: 30 - 2 + 1
  • Step 4: 28+1
  • Step 5: 29

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

cs

main.cs

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

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

cs

main.cs

Note

An expression can have a combination of operators, numbers and variables. Examples of expressions from the above code are
value_1 + value_2 and (value_1 + 10) / 2. Each expression always has a resultant or return value.

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

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 10
course content

Зміст курсу

C# Basics

Basic Operators & ExpressionsBasic Operators & Expressions

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:

cs

main.cs

You can also store the result of expressions inside variables:

cs

main.cs

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 Usage:

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:

cs

main.cs

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

The steps explained in the picture are executed below:

  • Expression: (10 + 5) * 2 - 8 / 4 + 1)
  • Step 1: 15 * 2 - 8 / 4 + 1
  • Step 2: 15 * 2 - 2 + 1
  • Step 3: 30 - 2 + 1
  • Step 4: 28+1
  • Step 5: 29

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

cs

main.cs

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

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

cs

main.cs

Note

An expression can have a combination of operators, numbers and variables. Examples of expressions from the above code are
value_1 + value_2 and (value_1 + 10) / 2. Each expression always has a resultant or return value.

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

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 10
some-alt