Home

Conditional Operators

 

Incrementing a Number

We are used to counting numbers such as 1, 2, 3, 4, etc. In reality, when counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. The simplest technique of incrementing a value consists of adding 1 to a variable. 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:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    uint stories = 2;
%>

<%
    Response.Write("<pre>Stories: " + stories);
    stories = stories + 1;
    Response.Write("<br>Stories: " + stories + "</pre>");
%>
</body>
</html>

This would produce:

Real Estate

C# provides a special operator that takes care of this operation. The operator is called the increment operator and is ++. Instead of writing stories = stories + 1, you can write stories++ and you would get the same result. The above program can be re-written as follows:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    uint stories = 2;
%>

<%
    Response.Write("<pre>Stories: " + stories);
    stories++;
    Response.Write("<br>Stories: " + stories + "</pre>");
%>
</body>
</html>

++ is called 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 stories++ is executed, the compiler takes the previous value of the variable and adds 1 to it and the variable holds the incremented value:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    uint stories = 2;
%>

<%
    Response.Write("<pre>Stories: " + stories);
    stories++;
    Response.Write("<br>Stories: " + stories);
    stories++;
    Response.Write("<br>Stories: " + stories);
    stories++;
    Response.Write("<br>Stories: " + stories);
    stories++;
    Response.Write("<br>Stories: " + stories + "</pre>");
%>
</body>
</html>

This would produce:

Real Estate

Decrementing: Pre and Post-Decrementing

When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a variable. This operation works as if a variable called Stories had its value diminished by 1, as in

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    uint stories = 2;
%>

<%
    Response.Write("<pre>Stories: " + stories);
    stories = stories - 1;
    Response.Write("<br>Stories: " + stories + "</pre>");
%>
</body>
</html>

As done with the increment, C++ provides a quicker way of subtracting 1 from a variable. This is done using the decrement operation, that is --. To use the decrement operator, type –- on the left or the right side of the variable when this operation is desired. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    uint stories = 2;
%>

<%
    Response.Write("<pre>Stories: " + stories);
    stories--;
    Response.Write("<br>Stories: " + stories + "</pre>");
%>
</body>
</html>

Both versions would produce:

Real Estate

If you want to decrement the variable before calling it, position the operator on the left side of the operator. If you plan to decrement a variable only after it has been accessed, position the operator on the right side of the variable.

Techniques of Incrementing and Decrementing a Variable

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

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    double PropertyValue = 612750;
    double NewValue;

    Response.Write("<pre>Property Value: " + PropertyValue);
    NewValue = PropertyValue + 2420;
    Response.Write("<br>Property Value: " + NewValue + "</pre>");
%>
</body>
</html>

This would produce:

Real Estate

The above technique requires that you use an extra variable in your application. The advantage is that each variable can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable. Sometimes in your program you will not need to keep the value of the source variable. You may want to simply modify the value that a variable is holding. In this case you can perform the addition operation directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and doesn't need an additional variable. To add a value to a variable and change the value that the variable is holding, use the assignment “=” and the addition “+” operators as follows:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    double PropertyValue = 588640;

    Response.Write("<pre>Property Value: " + PropertyValue);
    PropertyValue = PropertyValue + 8250;
    Response.Write("<br>Property Value: " + PropertyValue + "</pre>");
%>
</body>
</html>

This would produce:

Real Estate

C# provides an operator that combines both + and = to allow you to increase the value held by a numeric variable. To use it, type += on the right side of the variable, followed by the value by which the variable will be incremented. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>
<title>Altair Real Estate</title>
</head>
<body>

<h3>Altair Real Estate</h3>

<p>Altair Real Estate is a regional leader is property management and 
sales.</p>

<%
    double PropertyValue = 355850;

    Response.Write("<pre>Property Value: " + PropertyValue);
    PropertyValue += 6540;
    Response.Write("<br>Property Value: " + PropertyValue + "</pre>");
%>
</body>
</html>

To decrease the value of a variable, instead of the addition operation, use the subtraction and apply the same techniques. In the above program, the variable can have its value decremented by applying the -= operator.

 

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