Arithmetic Operations

This section describes arithmetic operations: Multiplication, Division, Addition, and Subtraction.

So far we have learned how to enter data, store data, and retrieve data. The next step is to learn how to operate data, like how to add two values together to get the sum. This requires the help of data operations. Let's start with arithmetic operations first. Other types of operations will be discussed in later chapters.

There are 4 basic arithmetic operations that we can perform on integral and real data. Each of them is represented by special symbol called operator. Bellow is the operators of the 4 arithmetic operations:

A single arithmetic operation can be entered into C# statements in a format called arithmetic expression, with the following syntax:

   literal arithmetic_operator literal

Note that the literals used in an arithmetic expression must be of the integral or real data type. For example: 1 + 2, 1.4142 * 1.4142, 10 / 3.

We can also put variables instead of literals into arithmetic expression, as long as the variables are of integral or real data type. For example: 3.14159*diameter, sqrt_s*sqrt_s. Note that space characters before or after the operator will be ignored.

When a statement with an arithmetic express is executed, the expression will be evaluated by performing the arithmetic operation, and the resulting value will be placed into the statement where the expression is located. For example, the following statements will store the value 3 to a, and 15 to b.

   long a, b;
   a = 1 + 2;
   b = 5*a;

Multiple arithmetic operations can also be entered into C# statements as an arithmetic expression with a more complex syntax:

   literal arithmetic_operator literal arithmetic_operator literal ...

For example: 5*1+2, pi*r*r, 199.99*qty+shipping_fee.

Rule: When multiple operators are used in an expression, data operation must be started with the operator that has the highest level of precedence. If two operators have the same level of precedence, starts with the left operator first. If an operator with a lower level of precedence needs to be processed first, enclose this operator and the related data in a pair of parentheses.

Levels of precedence of arithmetic operators:

Examples of expressions:

So now we know that an arithmetic expression can have one or many arithmetic operations. It can also have parentheses to change alter the order evaluation process. At the end of the evaluation process, an arithmetic express will always produce an integer or real value. Base on this observation, we can also call a single literal or variable of integral or real data type as an arithmetic expression, because it is also producing an integer or real value. So we can expend our arithmetic expression definition with the following syntax:


With the understanding that an arithmetic expression always produces an integer or real value, we can now generize our definition of assignment statement to take an arithmetic expression at the right hand side of the assignment sign "=", with following syntax:

assignment statement:
   integer_variable = arithmetic_expression
   real_variable = arithmetic_expression

Last update: 2010.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

Data Type and Variables

 Data Literals

 Data Literals - Example

 Variables and Assignment Statements

 Variables and Assignments - Example

Arithmetic Operations

 Arithmetic Operations - Example

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials


 PDF Printing Version