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
course content

Contenido del Curso

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

bookBasic 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

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

You can also store the result of expressions inside variables:

cs

main

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

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:

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

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:

cs

main

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 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` ?

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

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 10
some-alt