ASP.NET

Introduction to Conditions

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

2 min read505 words12 code samples

html Lesson 09: Introduction to Conditions

Introduction to Conditions#

Logical Operators#

Introduction#

The C# language includes specialized symbols known as Boolean operators, which are essential for validating data values.

The Equality Operator ==#

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

C#
value1 == Value2

It can be illustrated as follows:

The Logical Not Operator !#

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

C#
!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:

C#
Value1 < Value2

It can be illustrated as follows:

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:

C#
value1 <= value2

It can be illustrated as follows:

Greater Than or Equal To: >#

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

C#
value1 > value2

It can be illustrated as follows:

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:

C#
value1 >= value2

It can be illustrated as follows:

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

OperatorMeaningExampleOpposite
==Equality toa == b!=
!=Not equal to12 != 7==
<Less than25 < 84>=
<=Less than or equal toCab <= Tab>
>Greater than248 > 55<=
>=Greater than or equal toVal1 >= Val2<

Boolean Values#

A Boolean Variable#

Boolean variables are those designed to store only two possible states: true or false. You have the option of using the bool keyword or the var keyword to declare them, as demonstrated below:

C#
<%
    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:

C#
<%
    var drinkingUnderAge = true;
%>

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

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

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

C#
<%
    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:

C#
<%
    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:

C#
<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#

Just like any other data type, you can pass bool arguments into functions. These parameters will then be processed based on their true or false state. Previous Copyright © 2008-2016, FunctionX Next