ASP.NET

Operators and Operands

An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by…

6 min read1,261 words24 code samples

C# Tutorials - Lesson 04: Operators and Operands

Operators and Operands#

Fundamental C# Operators#

Introduction#

An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and at least one value. The symbol used in an operation is called an operator. A value involved in an operation is called an operand. A unary operator is an operator that performs its operation on only one operand. An operator is referred to as binary if it operates on two operands.

Regular Operators#

C# supports various types of operators. Curly brackets are used to create a section of code. As such they are required to delimit the bodies of objects or the sections in code. Parentheses are used to isolate a group of items that must be considered as belonging to one entity. Parentheses can also be used to isolate an operation or an expression with regard to another operation or expression. The semi-colon is used to indicate the end of an expression or a declaration. The comma can be used to separate variables declared as a group. Here is an example:

C#
string firstName, lastName, fullName;

There are other uses of the comma. The assignment operation is used to give a value to a variable. The formula to use it is:

C#
variable-name = Value

The variable-name must be a valid variable name. It cannot be a value such as a numeric value or a (double-quoted) string. The single quote is used to include one character to initialize, or assign a symbol to, a variable declared as char. The double-quote " is used to delimit a string. Square brackets are mostly used in arrays.

The Size of a Data Type#

When you declare a variable, the compiler reserves a portion of space in the computer memory to hold that variable. The unary sizeof operator allows you to find out how much space a data type is using. To use it, enter the data type in the parentheses of the sizeof() operator. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Employee Record</title>

</head>
<body>

<h2>Employee Record</h2>

<%
    double period = 155.50;
    int size = sizeof(double);

    Response.Write("The value ");
    Response.Write(period);
    Response.Write(" uses ");
    Response.Write(size);
    Response.Write(" bytes");
%>

</body>
</html>

This would produce:

Here are a few more example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Data Types Sizes</title>

</head>
<body>
<div align="center">
<h2>Data Types Sizes</h2>

<%
    Response.Write("<table border=5><tr><td><b>Data Type</b></td><td><b>Memory Size</b></td></tr>");
    Response.Write("<tr><td>char</td><td>");
    Response.Write(sizeof(char));
    Response.Write(" Bytes</td></tr><tr><td>bool</td><td>");
    Response.Write(sizeof(bool));
    Response.Write(" Byte</td></tr><tr><td>int</td><td>");
    Response.Write(sizeof(int));
    Response.Write(" Bytes</td></tr><tr><td>uin</td><td>");
    Response.Write(sizeof(uint));
    Response.Write(" Bytes</td></tr><tr><td>short</td><td>");
    Response.Write(sizeof(short));
    Response.Write(" Bytes</td></tr><tr><td>ushort</td><td>");
    Response.Write(sizeof(ushort));
    Response.Write(" Bytes</td></tr><tr><td>byte</td><td>");
    Response.Write(sizeof(byte));
    Response.Write(" Byte</td></tr><tr><td>sbyte</td><td>");
    Response.Write(sizeof(sbyte));
    Response.Write(" Byte</td></tr><tr><td>float</td><td>");
    Response.Write(sizeof(float));
    Response.Write(" Bytes</td></tr><tr><td>double</td><td>");
    Response.Write(sizeof(double));
    Response.Write(" Bytes</td></tr><tr><td>decimal</td><td>");
    Response.Write(sizeof(decimal));
    Response.Write(" Bytes</td></tr><tr><td>long</td><td>");
    Response.Write(sizeof(long));
    Response.Write(" Bytes</td></tr><tr><td>ulong</td><td>");
    Response.Write(sizeof(ulong));	
    Response.Write(" Bytess</td></tr></table>");
%>
</div>
</body>
</html>

This would produce:

The Addition Operation#

Introduction#

The positive operator is used as a unary operator to indicate that a number is positive. The addition operation, represented by the + symbol, is used to add values.

String Addition#

Two or more strings can be combined to get a new string. This is done using the + operator. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Employee Record</title>

</head>
<body>

<h2>Employee Record</h2>

<%
    string firstName = "Philippe";
    string lastName = "Fields";
    string fullName = firstName + " " + lastName;

    Response.Write("First Name:");
    Response.Write(firstName);
    Response.Write("<br>");
    Response.Write("Last Name:");
    Response.Write(lastName);
    Response.Write("<p>Full Name:");
    Response.Write(fullName);
    Response.Write("</p>");
%>

</body>
</html>

This would produce:

In the same way, strings can be combined in the parentheses of Response.Write(). Here are examples:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Employee Record</title>

</head>
<body>

<h2>Employee Record</h2>

<%
    string firstName = "Philippe";
    string lastName = "Fields";

    Response.Write("First Name:" + firstName + "<br>");
    Response.Write("Last Name:" + lastName + "<br>");
    Response.Write("<p>Full Name:" + firstName + " " + lastName + "</p>");
%>

</body>
</html>

This would produce:

Incrementing a Variable#

To increment a value, add 1 to it. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Incrementing a Variable</title>

</head>
<body>

<h2>Incrementing a Variable</h2>

<%
    int value = 12;

    Response.Write("Techniques of incrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    value = value + 1;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

As an alternative, C# provides the increment operator represented by ++. Instead of writing Value = Value + 1, you can write Value++ and you would get the same result. The above program can be re-written as follows:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    int value = 12;

    Response.Write("Techniques of incrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    value++;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

++ is a unary operator used to modify the value of the variable by adding 1 to it. Every time value++ is executed, the previous value of the variable is incremented by 1, and the variable holds the incremented value.

Pre and Post-Increment#

When using the ++ operator, the position of the operator with regard to the variable it is modifying can be significant. To increment the value of the variable before re-using it, you should position the operator on the left of the variable. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Pre Increment</title>

</head>
<body>

<h2>Pre Increment</h2>

<%
    int value = 12;

    Response.Write("Techniques of incrementing a value");

    Response.Write("<br>Value = ");
    Response.Write(value);

    Response.Write("<br>Value = ");
    Response.Write(++value);
		
    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

When writing ++value, the value of the variable is incremented before being accessed. On the other hand, if you want to first use a variable, then increment it, in other words, if you want to increment the variable after calling it, position the increment operator on the right side of the variable. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Incrementing a value</title>

</head>
<body>

<h2>Incrementing a value</h2>

<%
    int value = 12;

    Response.Write("Techniques of incrementing a value");

    Response.Write("<br>Value = ");
    Response.Write(value);

    Response.Write("<br>Value = ");
    Response.Write(value++);
		
    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

Compound Addition#

As you may be aware aready, you can add a constant value to a variable. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Compound Addition</title>

</head>
<body>

<h2>Compound Addition</h2>

<%
    double value = 12.75;
    double newValue;

    Response.Write("Techniques of incrementing and decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    newValue = value + 2.42;
		
    Response.Write("<br>Value = ");
    Response.Write(newValue);
%>

</body>
</html>

This would produce:

To add a value to a variable and change the value that the variable is holding, you can combine the assignment "=" and the addition "+" operators to produce a new operator as +=. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    double value = 12.75;

    Response.Write("Techniques of incrementing and decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    value += 2.42;
		
    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This program produces the same result as the previous.

The Subtraction Operations#

Introduction#

The negative operator must be used to indicate that a number is negative. Examples are -12, -4.48, or -32706. The subtraction operation is used to take out or subtract a value from another value.

Decrementing a Variable#

Decrementing a variable consists of subtracting 1 from it. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Decrementing a Variable</title>

</head>
<body>

<h2>Decrementing a Variable</h2>

<%
    int value = 12;

    Response.Write("Techniques of decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    value = value - 1;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

As done to increment, C# provides a quicker way to subtract 1 from a value. This is done using the decrement operator, that is --. To use the decrement operator, type the operator on the left or the right side of the variable. Using the decrement operator, the above program could be written:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    int value = 12;

    Response.Write("Techniques of decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    value--;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

Pre-Decrementing a Value#

Once again, the position of the operator can be important. If you want to decrement the variable before calling it, position the decrement operator on the left side of the operand. This is illustrated in the following program:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Decrementing a Variable</title>

</head>
<body>

<h2>Decrementing a Variable</h2>

<%
    int value = 12;

    Response.Write("Techniques of decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    Response.Write("<br>Value = ");
    Response.Write(--value);
		
    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>
<html>
<head>
<title>Decrementing a Variable</title>

</head>
<body>

<h2>Decrementing a Variable</h2>

<%
    int value = 12;

    Response.Write("Techniques of decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    Response.Write("<br>Value = ");
    Response.Write(value--);
		
    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

Compound Subtraction#

To decrement a value from a variable, use the -= operator. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    double value = 12.75;

    Response.Write("Techniques of incrementing and decrementing a value");
    Response.Write("<br>Value = ");
    Response.Write(value);

    value -= 2.42;
		
    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

The Multiplication Operations#

Introduction#

The multiplication allows adding one value to itself a certain number of times, set by a second value. Like the addition, the multiplication is associative: a * b * c = c * b * a.

Compound Multiplication#

As seen with the addition, you can multiply a constant value by a variable. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    double value = 12.75;

    Response.Write("Value = ");
    Response.Write(value);

    value = value * 2.42;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

To make this operation easy, the C# language supports the compound multiplication assignment operator represented as *=. To use it, apply the *= operator to the variable and assign the desired value. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    double value = 12.75;

    Response.Write("Value = ");
    Response.Write(value);

    value *= 2.42;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

The Division Operation#

Introduction#

The division operation consists of cutting a number in pieces or fractions. When performing the division, be aware of its many rules. Never divide by zero (0). Make sure that you know the relationship(s) between the numbers involved in the operation.

Compound Division#

As you can add, subtract, or multiply a value to a variable and assign the result to the variable itself, you can also divide a variable by a constant value. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Compound Division</title>

</head>
<body>

<h2>Compound Division</h2>

<%
    double value = 12.75;

    Response.Write("Value = ");
    Response.Write(value);

    value = value / 2.42;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

This would produce:

To let you perform this operation faster, the C# language provides the /= operator. Here is an example of using it:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Compound Division</title>

</head>
<body>

<h2>Compound Division</h2>

<%
    double value = 12.75;

    Response.Write("Value = ");
    Response.Write(value);

    value /= 2.42;

    Response.Write("<br>Value = ");
    Response.Write(value);
%>

</body>
</html>

The Remainder#

Introduction#

The remainder operation is usedt to get the value remaining after a division renders a natural result. The operation is performed with the percent sign (%). Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>The Remainder</title>

</head>
<body>

<h2>The Remainder</h2>

<%
    int players = 18;
    int remainder = players % 11;

    // When the game starts, how many players will wait?.
    Response.Write("Out of ");
    Response.Write(players);
    Response.Write(" players, ");
    Response.Write(remainder);
    Response.Write(" players will have to wait when the game starts.\n");
%>

</body>
</html>

This would produce:

The Compound Remainder#

As seen with the other arithmetic operators, you can find the remainder of a variable and assign the result to the variable itself. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    int players = 18;

    // When the game starts, how many players will wait?.
    Response.Write("Out of ");
    Response.Write(players);
    Response.Write(" players, ");

    players = players % 11;
            
    Response.Write(players);
    Response.Write(" players will have to wait when the game starts.\n");

%>

</body>
</html>

To support a faster version of this operation, the C# language provides the compound remainder operator represented as %=. To use it, assign its value to the variable. Here is an example:

C#
<%@ Page Language="C#" %>

<!DOCTYPE html>

<html>
<head>
<title>Exercises</title>

</head>
<body>

<h2>Exercises</h2>

<%
    int players = 18;

    // When the game starts, how many players will wait?.
    Response.Write("Out of ");
    Response.Write(players);
    Response.Write(" players, ");

    players %= 11;
            
    Response.Write(players);
    Response.Write(" players will have to wait when the game starts.\n");
%>

</body>
</html>

Previous Copyright © 2004-2016, FunctionX Next