Home

Introduction to Conditions

Logical Operators

Introduction

To perform the necessary validations of values, the C# language provides some symbols, referred to as Boolean operators.

The Equality Operator ==

To compare two variables for equality, use the == operator. The formula used is:

value1 == Value2

It can be illustrated as follows:

Equality

The Logical Not Operator !

To negate a Boolean expression, apply the logical not operator which is !. The formula to follow is:

!value

There are two main ways you can use the logical not operator. The most classic way of using the logical not operator is to check the state of a variable. To nullify a variable, you can write the exclamation point to its left.

Less Than: <

To find out whether one value is lower than another, use the < operator. The formula to follow is:

Value1 < Value2

It can be illustrated as follows:

Flowchart: Less Than

Less Than of Equal To: <=

The operator used to find out if two values are the same or if the first is less than the second  is <=. The formula to follow is:

value1 <= value2

It can be illustrated as follows:

Less Than Or Equal

Greater Than or Equal To: >

To find out if one value is greater than the other, use the > operator. Its formula is:

value1 > value2

It can be illustrated as follows:

Greater Than

The Greater Than or Equal Operator >=

The operator to find out if one value is greater than or equal to another value is >=. The formula to follow is:

value1 >= value2

It can be illustrated as follows:

Flowchart: Greater Than Or Equal To

Here is a summary table of the logical operators we have studied:

 
Operator Meaning Example Opposite
== Equality to a == b !=
!= Not equal to 12 != 7 ==
< Less than 25 < 84 >=
<= Less than or equal to Cab <= Tab >
> Greater than 248 > 55 <=
>= Greater than or equal to Val1 >= Val2 <

Boolean Values

A Boolean Variable

A variable is referred to as Boolean if it can hold a value that is either true or false. To declare a Boolean variable, use either the var or the bool keyword. Here is an example:

<%
    bool drinkingUnderAge;
%>

After the variable has been declared, you can initialize it with a true or a false value. In fact, if you declare it as var, you must initialize it. Here is an example:

<%
    var drinkingUnderAge = true;
%>

A Boolean variable can also be initialized with a Boolean expression. Here is an example:

<%
    int age = 21;
    
    bool drinkingUnderAge = age >= 21;
%>

In this case, it is a good idea to put the Boolean expression in parentheses:

<%
    int age = 21;
    
    bool drinkingUnderAge = (age >= 21);
%>

At any time and when you judge it necessary, you can change the value of the Boolean variable by assigning it a true or false value. Here is an example:

<%
    bool drinkingUnderAge = true;

    // . . .

    drinkingUnderAge = false;
%>

A Boolean Member in a Class

A member variable of a class can be a Boolean type. Here is an example:

<script runat="server">
public class House
{
    public float Bathrooms;
    public byte Stories;
    public bool HasCarGarage;
    public int YearBuilt;
    public double MarketValue;

</script>

A Boolean Parameter

Like parameters of the other types, you can pass an argument of type bool to a function. Such an argument would be treated as holding a true or false value.


Previous Copyright © 2008-2016, FunctionX Next