Home

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.

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.

Semi-Colon;

The semi-colon is used to indicate the end of an expression or a declaration. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<h3>Exercise</h3>

<%
    ;
%>
</body>
</html>

There are other uses of the semi-colon.

Curly Brackets { }

Curly brackets are used to create a section of code. They are required to delimit the bodies of namespaces, classes, structures, exceptions, conditional statements.

Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<h3>Exercise</h3>

<%
    {
    }
%>

</body>
</html>

Parentheses ( )

Parentheses are used to isolate a group of items that must be considered as belonging to one entity. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<h3>Exercise</h3>

<%
    Response.Write();
%>
</body>
</html>

Parentheses can also be used to isolate an operation or an expression with regard to another operation or expression.

The Comma ,

The comma is used to separate variables used in a group. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<h3>Exercise</h3>

<%
    string FirstName, LastName, FullName;
%>

</body>
</html> 

The comma can also be used to separate the member of an enumeration or the arguments of a method. We will review all of them when the time comes.

The Assignment =

The assignment operation gives a value to a variable. Its syntax is:

VariableName = Value

Here is an example that assigns a numeric value to a variable:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<h3>Exercise</h3>

<%
    decimal Salary;

    // Using the assignment operator
    Salary = 12.55M;
%>
</body>
</html>

Single-Quote '

The single quote is used to include one character to initialize, or assign a symbol to, a variable declared as char. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    char Gender;

    Gender = 'M';
%>
</body>
</html>

You can include only one character between single-quotes except if the combination of symbols can be evaluated to one character. This is the case for escape sequences.

Double-Quotes "

The double-quote " is used to delimit a string. Like the single-quote, the double-quote is usually combined with another. Between the combination of double-quotes, you can include an empty space, a character, a word, or a group of words, making it a string. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    Response.Write("ASP.NET With C#");
%>
</body>
</html>

A double-quoted string can also be declared and then assigned to a variable.

Square Brackets [ ]

Square brackets are mostly used to control the dimension or index of an array.

The Positive Operator +

A number that doesn't display a sign is referred to as unsigned. To express a variable as positive or unsigned, you can just type it. here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    Response.Write("Number = ");
    Response.Write(+802);
%>
</body>
</html>

This would produce:

Operators

The Negative Operator -

To express any number on the left side of 0, it must be appended with -. Examples are -12, -448, -32706. A value accompanied by - is referred to as negative. Here is an example that uses two variables. One has a positive value while the other has a negative value:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    // Displaying a positive number
    Response.Write("Number = ");
    Response.Write(+802);
    Response.Write("<br />");

    // Displaying a negative number
    Response.Write("Second Number ");
    Response.Write(-802);
%>
</body>
</html>

This would produce:

Operators

The Addition Operations

 

Introduction

To add two numbers, you use the + operation between them. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    Response.Write("244 + 835 = ");
    Response.Write(244 + 835);
%>
</body>
</html>

You can also add string variables to get a new string. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    string FirstName = "Alexander";
    string LastName = "Kallack";
    string FullName = FirstName + " " + LastName;
%>

<%
    Response.Write("Full Name: ");
    Response.Write(FullName);
%>
</body>
</html>

This would produce:

Operators

Incrementing a Variable

To increment the value of a variable, you can add 1 to it. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Value = 12;
%>

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

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

<%  Value = Value + 1; %>

<%
    Response.Write("<br />");
    Response.Write("Value = " + Value);
%>
</body>
</html>

This would produce:

Incrementing

As a shortcut, you can use the ++ operator. 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:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Value = 12;
%>

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

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

<%  Value++; %>

<%
    Response.Write("<br />");
    Response.Write("Value = " + Value);
%>
</body>
</html>

The ++ is a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it. Every time the Value++ is executed, the compiler takes the previous value of the variable, adds 1 to it, 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, as in ++Value.

When writing ++Value, the value of the variable is incremented before being called. 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.

Compound Addition

It is not unusual to add a constant value to a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:

<%
    NewValue = Value + 2.42;
%>

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:

<%
    Value += 2.42;
%>

The Multiplication Operations

 

Introduction

To multiply two numbers, you use the asterisk *. Here is an example:

<%
    double Value1 = 224.58, Value2 = 1548.26;
    double Result = Value1 * Value2;
%>

Compound Multiplication

We saw that you could add or subtract a value to a variable and assign the result to the same variable. You can perform the same operation with the multiplication. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    double Value = 12.75;
%>

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

    Value = Value * 2.42;

    Response.Write("Value = " + Value);
%>
</body>
</html>

This would produce:

Multiply

To make this operation faster, you can use the *= operator. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    double Value = 12.75;
%>

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

    Value *= 2.42;

    Response.Write("Value = " + Value);
%>
</body>
</html>
 
 
 
 

The Subtraction Operations

 

Introduction

To subtract, you use the - sign. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    // Values used in this program
    double Value1 = 224.58, Value2 = 1548.26;
    double Result = Value1 - Value2;
%>

<%
    Response.Write(Value1 + " - " + Value2 + " = " + Result);
%>
</body>
</html>

This would produce:

Subtraction

Decrementing a Variable

To decrement a variable, you can subtract 1 from it and assign the result to the same variable. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Value = 12;

    Response.Write("Techniques of decrementing a value<br />");
%>

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

<%  Value = Value - 1; %>

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

</body>
</html>

This would produce:

Decrementing a Variable

To simplify this operation, you can use the -- operator. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Value = 12;

    Response.Write("Techniques of decrementing a value<br />");
%>

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

<%  Value--; %>

<%
    Response.Write("Value = " + 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. If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable.

Compound Subtraction

You may want to subtract a constant value from a variable. To decrement a value from a variable, use the subtraction and apply the same technique. This is done with the -= operator. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    double Value = 12.75;

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

    Value -= 2.42;
	
    Response.Write("Value = " + Value);
%>

</body>
</html>

This would produce:

Subtraction

The Division Operation

 

Introduction

To perform the division, you use the forward slash /. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    // Initializing various variables when declaring them with the same data type
    double Value1 = 224.58, Value2 = 1548.26;
    double Result = Value1 / Value2;
			
    Response.Write(Value1 + " / " + Value2 + " = " + Result);
%>

</body>
</html>

This would produce:

Division

When performing the division, be aware of its many rules. Never divide by zero (0).

Compound Division

Remember that you can add, subtract, or multiply a value to a variable and assign the result to the variable itself. You can also perform this operation using the division. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    double Value = 12.75;

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

<%
    Value = Value / 2.42;

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

</body>
</html>

This would produce:

Division

To perform this operation faster, you can use the /= operator. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    double Value = 12.75;

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

<%
    Value /= 2.42;

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

</body>
</html>

The Remainder

 

Introduction

The remainder operation is performed with the percent sign (%) which is gotten from pressing Shift + 5.

Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Players = 18;
    int Remainder = Players % 11;
%>

<%
    // When the game starts, how many players will wait?.
    Response.Write("Out of " + Players + " players, " + Remainder);
    Response.Write(" players will have to wait when the game starts.");
%>

</body>
</html>

This would produce:

Remainder

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:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Players = 18;

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

<%  Players = Players % 11; %>

<%
    Response.Write(Players);
    Response.Write(" players will have to wait when the game starts.");
%>

</body>
</html>

To support a faster version of this operation, you can use the %= operator. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Exercise</title>
</head>
<body>

<%
    int Players = 18;

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

<%  Players %= 11; %>

<%
    Response.Write(Players);
    Response.Write(" players will have to wait when the game starts.");
%>

</body>
</html>
 
 
   
 

Previous Copyright © 2009-2016, FunctionX, Inc. Next