print banner
 

Basic Programing and Logic for Beginning ColdFusion Programmers

Variables, Types and Operators; Oh My!

Programming is all about being able to manipulate data. Web programming is about being able to have a site get information, do stuff with it and produce some output. That's all good and all, but where do keep this data when we're working on it? A variable. You can think of a variable as a container for data while your working. Variables have three basic parts, even if we don't always think about all three parts while we are working. It has a type, a name and a value.

The type tells us what kind of information the variable holds and what kinds of things can be done to it. For instance, it doesn't make sense to try and add a number to something that contains text. "Hello World" + 1 = ??? ColdFusion is loosely typed, which means that a variable that held a group of text (or a string) one minute can hold a number the next. A number can be treated as text in ColdFusion, but text can't be treated as number. Dates can also be treated as a string of text, but the result is typically ugly, so you generally want to use functions to format the date more approratly.

Types in ColdFusion are broken into two types: simple and complex. Simple types are strings (text), integers (whole numbers), floating point numbers (real numbers or decimals), Boolean values (true or false) and dates (date and time information). Complex types include arrays (counters of multiple values), queries (which contain the results of an database or LDAP query), structs (which is a structure of simple types, about a single thing, glued tohether) and objects(like structs but with methods).

Name is simply what you use to refer to it. The name has two parts, the scope and the actual name. Two variables can only have the same name if their scope is different, and if such a case exists you must use both the scope and the name to refer to the variable (which you should do anyway). Most cases you must surround the scope and name of the variable in pound (#) characters, with a dot "." between them to access the variable.

Lastly, the value of the variable is the actual data it holds. The value can be set, changed or cleared by using operators, cftags or functions on the variable. So, let's defined and use a variable:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Sample</title> </head> <body> <cfset x = "Hello, World."> <!--- This creates a variable x, and assigns it the value: Hello, World. This comment is a ColdFusion comment, and won't be rendered in the source. ---> <h1><cfoutput>#x#</cfoutput></h1> <p> <cfoutput>#lcase(x)# #ucase(X)# </cfoutput> </p> </body> </html>

In the above example, we create a variable with the name x and asign the value "Hello. World" to it. We do this using the cfset tag. Because we didn't specify scope, it's the current local scope, and the type is string (because we're assigning the variable a value that happens to be text). The quotes around the string aren't part of the value, but rather tell ColdFusion where the string begins and ends. ColdFusion allows you to either use single (apostrophe) or double (quotation mark) quotes, but the closing must be the same kind of quote. I also use the string function lcase and ucase, to make the string lower- an upper- case, respectively. This is often a good idea for string comparisons because you don't know if the user has/had caps lock turned on when filling out a form. A function is a block of code that can be reused, given different input. For now, you'll see them as things you can call and use; in later parts we will get to writing functions. Variable names are not case sensitive, notice that X in #ucase(X)# still works, and refers to the same variable as x used in the lcase above it. The output from the above code would be the HTML that you'll see bellow. Notice the extra white space from the ColdFusion lines that produce no output. This slightly increases the amount of data you are sending without adding any value to the end user. Don't worry though, because there is a way to clean it up. We'll get there.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <title>Sample</title> </head> <body> <h1>Hello, World.</h1> <p> hello, world. HELLO, WORLD. </p> </body> </html>

The users browser would render a page that would show all on one line: "hello, world. HELLO WORLD."

Math Operations

The math operations addition (+), subtration (-), multiplication (*), division (/) and modulus (mod) are all supported by ColdFusion. ColdFusion also now supports unary operators increment (++) and decrement (--).

<cfif not isdefined('form.submit')> <form method="post"> <label>Tempature in Celcious: <input name="tc" type="text" length="4" max="5" /></label> <input type="submit" name="submit" value="Convert to Fahrenheit" /> </form> <cfelse> <cfif isdefined('form.tc') and isnumeric(form.tc)> <cfset tf = (form.tc + 40) * (9/5) - 40> <cfoutput>#form.tc# degress celsius is #tf# degrees fahrenheit.</cfoutput> <cfelse> Tempatures in celsius are numbers. Please enter a number this time. <form method="post"> <label>Tempature in Celcious: <input name="tc" type="text" length="4" max="5" /></label> <input type="submit" name="submit" value="Convert to Fahrenheit" /> </form> </cfif> </cfif>

The above example creates a simple form (we'll go over forms in more detail later) that asks for a tempature in Celsius, and when submitted tells you the Fahrenheit conversion. Note the need to put parentheses around the form.tc + 40, this is because ColdFusion follows order of operations (like a scientific calculator). The order is Parentasis, then exponents (and other functions), followed by multiplicaton and devision and modulus, then addition or subtraction.

Knights of the Truth Tables

Declaring variables is not useful unless your able to do things conditionally. If the user does X then we need to do Y. In order to get that functionality we need to use if-then, if-else or if-elseif-else type logic.

Before we do, there are some very basic things we need to know about truth. First, there is no Grey area in truth to a computer (well there can be, but it's an advanced research topic currently being worked on by major universities). Things are either true or they are false, there is no in between "sorta" state.

And

Statements joined with an and create a compound statement that is only true with both of the clauses are true at the same time.

  • true and true is true
  • true and false is false
  • false and true is false
  • false and false is false

Or and Xor

Inclusive or logical or or is different from the either or (or exclusive or xor). Statements joined with an or are true if at least one of the clauses are true. Exclusive or (xor) is similar to the either or we use in common speech. It's true when either or the clauses are true, but not both.

For OR:
  • true or true is true
  • true or false is true
  • false or true is true
  • false or false is false
For XOR:
  • true xor true is false
  • true xor false is true
  • false xor true is true
  • false xor true is false

Not

Not works to negate the value of the statement after it, and unlike English double negative are allowed.
  • not true is false
  • not false is true
  • not not true is true
  • not not false is false

With this we can do things on conditional statements in two ways. The first is preferred for readability, especially for people new to the language, the if statement. The general form of an if statement is something like this:

<cfif #x# eq "Hello, World."> <b>It's true!</b> </cfif> <!--- If something is true, do this stuff. --->

And can be expanded to a more complex structure of:

<cfif #x# eq "Hello, World."> <b>Hi there.</b> <cfelseif #x# is "Goodbye."> <b>So long, and thanks for all the fish</b> <cfelse> <b>How's the weather?</b> </cfif> <!--- If this is true, do this stuff Otherwise if another thing is true instead, do this other stuff Otherwise if neither of the above is true, do this last bit of stuff. --->

The second type of conditional statement is called a switch, which works the same basic way, but written in a slightly shorter syntax, but can be more difficult for beginners. An example of a switch may show up in a future tutorial.

Note: the eq operator is a test for equality, not an assignment. it's return is true or false. A side note, false is defined as 0, and true is defined as any non-zero value.

Part 2