Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 101

Deprecated: Function eregi_replace() is deprecated in /home3/vvaswani/sql.php on line 301

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 107

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 109

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 124

Deprecated: Function ereg_replace() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 125

Deprecated: Function split() is deprecated in /home3/vvaswani/public_html/community/columns/trog/article.php on line 127

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174

Deprecated: Function ereg() is deprecated in /home3/vvaswani/db-mysql.php on line 174
The Melonfire Community - Trog
Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
ASP.NET Basics (part 3): Hard Choices
Learn all about operators and conditional tests in ASP.NET

| Apples And Oranges |

You'll remember how, in the second part of this tutorial, I used a bunch of mathematical operators to demonstrate how you could perform mathematical operations with numeric variables. But C# doesn't just stop at numeric and string operators - the language also comes with a bunch of comparison operators, whose sole raison d'etre is to evaluate expressions and determine if they are true or false.

The following table should make this clearer.


assume x=4 and y=10

Operator     What It Means       Expression   Result
----------------------------------------------------------------
==          is equal to       x == y       False

!=          is not equal to    x != y        True

>           is greater than    x > y       False

<           is less than       x < y       True

>=          is greater than
            or equal to       x >= y        False

<=         is less than
            or equal to        x <= y        True


Why do you need to know all this? Well, comparison operators come in very useful when building conditional expressions - and conditional expressions come in very useful when adding control routines to your code. Control routines check for the existence of certain conditions, and execute appropriate program code depending on what they find.

The first - and simplest - decision-making routine is the "if" statement, which looks like this:


if (condition)
{
   do this!
}


The "condition" here refers to a conditional expression, which evaluates to either true or false. For example,


if (car brakes fail)
{  
   start praying
}


or, to put it in language C# understands:


<%
if (brake == 0)
{
   pray();
}
%>


If the conditional expression evaluates as true, all statements within the curly braces are executed. If the conditional expression evaluates as false, all statements within the curly braces will be ignored, and the lines of code following the "if" block will be executed.

Let's take a quick example:


<script language="c#" runat="server">
void Page_Load()
{  
   // temperature
   int temp = 40;

   if (temp > 35)
   {
      message.Text = "Man, it's hot out there!";
   }  
}
</script>
<html>
<head><title>Weather Forecast</title></head>
<body>
<asp:label id="message" runat="server" />
</body>
</html>


Output image

In this case, a variable named "temp" has been defined, and initialized to the value 40. Next, an "if" statement has been used to check the value of the "temp" variable and display a message if it's over 35. Note my usage of the greater-than (>) conditional operator in the conditional expression.

An important point to note - and one which many novice programmers fall foul of - is the difference between the assignment operator (=) and the equality operator (==). The former is used to assign a value to a variable, while the latter is used to test for equality in a conditional expression.

So


<%
a = 47;
%>


assigns the value 47 to the variable "a", while


<%
a == 47
%>


tests whether the value of "a" is equal to 47.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-
Strict Standards: mktime(): You should be using the time() function instead in /home3/vvaswani/public_html/community/columns/trog/article.php on line 731
2018 Melonfire. All rights reserved
Terms and Conditions | Feedback