Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using ternary and chained operations

From: ColdFusion 9 New Features

Video: Using ternary and chained operations

One issue I've always had with the way ColdFusion handles conditional syntax is that you either have to use a full cfif tag or you have to use the convoluted and often difficult to understand IIF function if you want to keep your code looking nice and clean. In ColdFusion 9, Adobe has added support for ternary operators, which will allow you to drop a quick if statement into just about any block of code like you like. Another small addition we'll cover in this video is the ability to set multiple variables to the same value with a single statement, instead of multiple cfifs or multiple lines in the cfscript block.

Using ternary and chained operations

One issue I've always had with the way ColdFusion handles conditional syntax is that you either have to use a full cfif tag or you have to use the convoluted and often difficult to understand IIF function if you want to keep your code looking nice and clean. In ColdFusion 9, Adobe has added support for ternary operators, which will allow you to drop a quick if statement into just about any block of code like you like. Another small addition we'll cover in this video is the ability to set multiple variables to the same value with a single statement, instead of multiple cfifs or multiple lines in the cfscript block.

So to get started in the Chapter3 folder, we'll open ternary.cfm. And this isn't using into the new ColdFusion 9 features yet, but I am just going to review the file very quickly and then we'll go through and make some changes using new ColdFusion 9 syntax. In this file I am selecting all of the artists from our Artists table, and then I have a function here that will check and see how many total pieces the artist has versus the number that have been sold, and then returns a message based off of whether or not we have low stock or we have some percentage of stock remaining.

Down here at the bottom, I am looping over each of these artists, I am putting a header and then a table that shows all of their artwork, whether or not it's been sold, and at the end their total pieces, the number that they have sold, and then that stock message. I am also in the table row here setting a class of either even or odd row depending on whether it's an even or odd row. We'll go ahead and preview this file and you can see for Jeff Baclawski, I have their artwork, sold, No, No, Yes, Yes.

We have their total number of 6. They have sold 3 of them so they have 50% remaining, and for Aiden Donolan here we can see that we are low stock. Message shows that we only have 20% remaining. And we get our nice alternating row colors. So the first thing I am going to show you is the new ternary operators in ColdFusion 9. The ternary operator is a way to write an if statement in a single line, so here we are going to go back to our source code, scroll to the top, and the first if statement I have is if percentSold is greater than 50, then we show one of these two messages.

So I am going to convert this into a ternary operation. And we'll do that by doing cfreturn and I'm going to put my conditional statement inside parentheses, then I put a question mark, and then I put in what I want to happen if this statement is true. In this case, we are going to return this text. Then at the end of that block, I am going to say else, and I do the else with a colon. And then I put what I want displayed if my if statement is false.

In that case, I want it to be this text. We will go down to the end here, make sure my block is properly closed and get rid of my extra cfif. So I am going to just save my file here and we'll preview. And we can see that while nothing has changed,on the end I can still see can my low stock message where I am supposed to, and I am seeing my 50% stock remaining where I am supposed to. The Source code is changed entirely to use this new if syntax. Now in this particular case that doesn't seem all that useful.

However, if I go down to the bottom here where I have my alternating row colors, this syntax on this line has always bugged me. Having the cfif crammed inside of the HTML just doesn't look right. So what I can do instead of this as I can, because I'm already inside a CF output, I can just put a pound sign, I can say totalPieces MOD 2 equals 0. Then I'm going to output "evenRow", else I'll output "oddRow", and then I will close my pounds, save my file.

Now if I preview, again my alternating row colors are still working the way they are supposed to, but for me this is far more readable and more correct. Now you can use the ternary operators any place you would normally have an if statement. You can use them as I am here as an output. You can use them in a cfreturn block, anything that has a simple true and false return value is a good candidate for using the ternary operators. Now the other thing that ColdFusion 9 introduced is the ability to set multiple variables to a single value in a single line.

So in this case, I am setting total pieces and number sold both equal to zero. In ColdFusion I can do this. So both total pieces and number sold will be set to zero. Now those are called chained operators. So in this case I'm chaining two variables into a single value. Oh! And I just spotted another spot here where we can use a ternary operators, so let's replace this too. Let's do cfset numberSold =, if the current item is sold, and put that inside parens, then numberSold is going to be numberSold + 1. Else it's just going to be the previous value. Save our file.

So my statement says set the number sold to number sold plus one, if the current art has been marked as is sold. Otherwise just set it to it's old value. So the big takeaway in this video is the new ternary operator, which should help make your code leaner and cleaner as you move your applications to ColdFusion 9.

Show transcript

This video is part of

Image for ColdFusion 9 New Features
ColdFusion 9 New Features

36 video lessons · 2934 viewers

Daniel Short
Author

 
Expand all | Collapse all
  1. 4m 30s
    1. Welcome
      53s
    2. Using the exercise files
      3m 37s
  2. 7m 48s
    1. Adding managing and comparing servers
      5m 26s
    2. Monitoring server health
      2m 22s
  3. 22m 10s
    1. Setting the default data source and SMTP
      5m 6s
    2. Getting programmatic debugging and exception handling
      5m 54s
    3. Reviewing the new onCFCRequest method
      5m 29s
    4. Using ApplicationStop()
      2m 24s
    5. Working with Server.cfc and onServerStart
      3m 17s
  4. 17m 45s
    1. Using ternary and chained operations
      5m 46s
    2. Using New and Import
      4m 2s
    3. Reviewing CFScript enhancements
      7m 57s
  5. 20m 23s
    1. Building with implicit getters and setters
      4m 26s
    2. Working with LOCAL scope in functions
      4m 35s
    3. Serializing objects
      6m 6s
    4. Writing components in CFScript
      5m 16s
  6. 19m 27s
    1. Getting started with ORM
      5m 51s
    2. Logging ORM activity
      2m 13s
    3. Saving objects to the database
      6m 10s
    4. Defining table relationships
      5m 13s
  7. 16m 59s
    1. Caching page fragments
      5m 29s
    2. Managing the cache
      4m 48s
    3. Working with in-memory files using VFS (RAM disks)
      6m 42s
  8. 12m 39s
    1. Working with Excel spreadsheets
      4m 56s
    2. Working with Word documents
      4m 4s
    3. Working with PowerPoint presentations
      3m 39s
  9. 28m 58s
    1. Loading Google maps with markers onto a page
      4m 53s
    2. Adding a Flash movie with the built-in FLV player
      2m 14s
    3. Using multi-file upload control
      6m 7s
    4. Building a progress indicator
      3m 37s
    5. Working with accordion layouts
      3m 18s
    6. Showing information with data grid enhancements
      5m 1s
    7. Informing users with Confirm Alert and Prompt dialog boxes
      3m 48s
  10. 9m 8s
    1. Accessing IMAP accounts
      6m 31s
    2. Working with the new array functions
      2m 37s
  11. 15s
    1. Goodbye
      15s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed ColdFusion 9 New Features.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.