| 1. | <cfscript> |
| 2. | myList = 'George,Paul,John,Ringo'; // creates the variable myList, which holds 4 names |
| 3. | for (i = 1; i LTE listLen(myList); i = i+1) { |
| 4. | writeOutput(listGetAt(myList, i) & "<br>"); |
| 5. | } |
| 6. | </cfscript> |
| 1. | Opening our <cfscript> block |
| 2. |
create a variable called myList. Populate with 4 names (George,Paul,John,Ringo). You'll also notice the comment (the // and the text
after it). This is a standard single-line script comment (similar in 'regular' CFML to
<!--- comment --->). You can also do a multi-line
comment, such as: /* This is a multi-line comment. anything within the slash/asterisk - asterisk/slash will be ignored by the CF Server */ |
| 3. |
Here's where we get just a little funky. This line is essentially the same as
<cfloop from="1"
to="#listLen(myList)#"
index="i">, otherwise known
as a 'for' loop. It's comprised of 3 parts...let's break 'em down to make this easier to decipher:
|
| 4. | Here's our output. As we did in our first example, we use cfscript's writeOutput() function. Nested within that function, is a straightforward listGetAt() function...with a <br> concatenated at the end, so we get 4 lines of output after our looping is finished. Again, notice that we end the line with a semi-colon (;). |
| 5. | We're now done with our code inside of the loop, so we close our curly brace. |
| 6. | Close our cfscript block and we're done! |
| 1. | <cfscript> |
| 2. | myRandomNumber = randRange(1,100); |
| 3. | if (myRandomNumber GTE 1 AND myRandomNumber LTE 25) { |
| 4. | writeOutput(myRandomNumber & ' is in the first quarter'); |
| 5. | } else if (myRandomNumber GTE 26 AND myRandomNumber LTE 50) { |
| 6. | writeOutput(myRandomNumber & ' is in the second quarter'); |
| 7. | } else if (myRandomNumber GTE 51 AND myRandomNumber LTE 76) { |
| 8. | writeOutput(myRandomNumber & ' is in the third quarter'); |
| 9. | } else { |
| 10. | writeOutput(myRandomNumber & ' is in the fourth quarter'); |
| 11. | } |
| 12. | </cfscript> |
| 1. | Opening our <cfscript> block |
| 2. | Using ColdFusion's randRange() function, we generate a random number between 1 and 100...assinging that value to the variable myRandomNumber. |
| 3. | Our first condition. Checks to see if the value of myRandomNumber is between 1 and 25 (inclusive). This uses standard ColdFusion comparsion operators GTE and LTE for 'greater-than-or-equal-to' and 'less-than-or-equal-to'. We end the line with a curly brace...as with our loop example above, any code within the curly braces will appear if this condition is met (curly braces are not mandatory if there is only one line of code to execute in the condition...however, I include them here to show syntactically correct structure). |
| 4. | This is code that will execute if the condition in line 3 (above) is met. A simple writeOutput() (which you're familiar with by now) showing the actual value of the variable myRandomNumber, and concatenated with a string literal to show that the number is in quarter one. |
| 5. | If the previous condition is not met, we specify another condition (checking for the second quarter, or values between 26 and 50 (inclusive)). The logic is the same as in the first condition. The difference to note is the else if. This is different than ColdFusion's tag-based <cfelse>, which is one word. |
| 6. | The code to execute if the condition in line 5 (above) is met. Same as line 4. |
| 7. | See line 5 :) |
| 8. | See line 6 :) |
| 9. | Our final condition. We can use a simple else as opposed to the else if. If none of the previous 3 conditions are met, this is the only one left...as such, we do not have to specify 'if myRandomNumber GTE 76 AND myRandomNumber LTE 100', as it is implicit. |
| 10. | See line 6 :) |
| 11. | Close our one open curly brace. |
| 12. | Close our cfscript block and we're done! |