24
Variables and Conditionals Hal Helms halhelms.com

Variables and Conditionals Hal Helms halhelms.com

  • View
    232

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Variables and Conditionals Hal Helms halhelms.com

Variables and Conditionals

Hal Helms

halhelms.com

Page 2: Variables and Conditionals Hal Helms halhelms.com

What we'll cover

• Basics of variables

• Types of variables (strings, arrays, etc.)

• Scopes of variables (session, application, etc.)

• Conditionals (working with boolean variables)

Page 3: Variables and Conditionals Hal Helms halhelms.com

Basics

• Variables are name/value pairs

• Variables permit our programs to work with values that can't be known at design time or that may change from their original value customerName yourStreetAddress currentUser

Page 4: Variables and Conditionals Hal Helms halhelms.com

Basics

• Variables give context and constraint to valuesThe value "42" is meaningless apart from a

context, usually provided by variable name (e.g. meaningOfLife)

Data typing constrains variables to hold meaningful values• isNumeric() returns a boolean value

• randRange() returns an integer value

Page 5: Variables and Conditionals Hal Helms halhelms.com

Strong data typing

• Some languages employ strong typing, requiring the programmer to declare a variable's data type prior to using itString myName;int myAge;Company myEmployer;

• ExamplesJavaC#

Page 6: Variables and Conditionals Hal Helms halhelms.com

Weak data typing

• Other languages use weak typing, in which the language implicitly determines the data type of a variable

• ExamplesSmalltalkColdFusion

• Weakly typed languages are not untyped languages!

Page 7: Variables and Conditionals Hal Helms halhelms.com

Data typing

• Different data types hold different types of valuesstring

int

structure

Car

"Visualize whirled peas"

1024

name "Hal Helms"ACMmember true

numberOfChildren 1

Page 8: Variables and Conditionals Hal Helms halhelms.com

ColdFusion data types

• string• numeric• query• list (string)• structure• array• object• boolean• date• GUID• UUID

Page 9: Variables and Conditionals Hal Helms halhelms.com

Complex variable type: query

• queryseries of 1 or more rows with column or field

values for each rowid descriptionprice

100 Widget29.99200 Framus, left-handed75.00

• addressable products.price — refers to current row products.price[2] — specifies row

products

Page 10: Variables and Conditionals Hal Helms halhelms.com

Complex variable type: structure

• structurecollection of name/value pairs grouped around a

central idea or itemteacher Hal Helms

• addressable javaClass.maxStudents javaClass['maxStudents']

maxStudents 10isActive true

Ben Edwards

javaClass

Page 11: Variables and Conditionals Hal Helms halhelms.com

Complex variable type: 1-d array

• one-dimensional arraycollection of ordered valuesresembles a single row of a spreadsheet

Hal Helms

• addressable onedee[2]

Sean CorfieldBen Edwards

onedee

[1] [2] [3]

Page 12: Variables and Conditionals Hal Helms halhelms.com

Complex variable type: 2-d array

• two-dimensional arraycollection of ordered valuesresembles multiple rows of a spreadsheet

68

• addressable twodee[1][3]

8472

twodee

[1] [2] [3]

62 6969[1]

[2]

Page 13: Variables and Conditionals Hal Helms halhelms.com

Complex variable type: object

• Objects are fundamentally different from all other data types in that they encapsulate both data and behavior

• Objects allow us to create scale models of the world under consideration Car

• getMake(): string

• addGas(gallons): void

• accelerate(): void

Page 14: Variables and Conditionals Hal Helms halhelms.com

Quiz time!

• For each of the following,explain which data type you'd use and whyfinal test scores for all studentscollection of Student objectslyrics to "Memories" (thank heavens for garbage collection!)

number of students in a classinformation from LDAP servercomposer's date born, date died, nationality,

typical genrean hourly employee

Page 15: Variables and Conditionals Hal Helms halhelms.com

Understanding scopes

• A variable typically has a lifespan and a scopelifespan: how long the system keeps a reference

to a variablescope: location from which variable is visible

• In ColdFusion, a variable's scope and lifespan are bound together in a single attribute, scope, which is prefixed to the variable name

Page 16: Variables and Conditionals Hal Helms halhelms.com

Understanding scopesprefix visibility lifespan

application everywhere in same app life of application

attributes within custom tag (for variables passed into custom tag)

current request

caller within custom tag (for variables on calling page)

current request

cgi everywhere current request

client everywhere in same application for an individual user

varies by purge timeframe

cookie everywhere in all apps on domain

varies; set by cookie

form everywhere current request

request everywhere current request

server everywhere by all apps on single server

varies by server timeout setting

session everywhere in same app for an individual user

varies by session timeout setting

url everywhere current request

variables local to page/CFC and any included pages

current request

Page 17: Variables and Conditionals Hal Helms halhelms.com

Quiz time!

• For each of the following,explain which scope you'd use and whyglobal "constants" such as datasource namequery meant to be created and used on same

pagea CFC, currentUser, that encapsulates data and

behavior for the currently logged in usera cookie crumb arraya userID meant to identify the user between visitsa custom tag "reaching into" the base template to

read or set a variable on that page

Page 18: Variables and Conditionals Hal Helms halhelms.com

Conditionals

• All conditionals involve carrying out some instructions based on whether an expression evaluates to true

Page 19: Variables and Conditionals Hal Helms halhelms.com

<cfif>

<cfif form.betNumber EQ winningNumber><cflocation url="index.cfm?fuseaction=home.winner" />

</cfif>

Page 20: Variables and Conditionals Hal Helms halhelms.com

<cfelse>

<cfif form.betNumber EQ winningNumber><cflocation url="index.cfm?fuseaction=home.winner" />

<cfelse><cflocation url="index.cfm?fuseaction=home.loser" />

</cfif>

Page 21: Variables and Conditionals Hal Helms halhelms.com

<cfelseif>

<cfif form.betNumber EQ winningNumber><cflocation url="index.cfm?fuseaction=home.winner" />

<cfelseif form.betNumber LT 0><cflocation url="index.cfm?fuseaction=home.cheater" />

<cfelse><cflocation url="index.cfm?fuseaction=home.loser" />

</cfif>

Page 22: Variables and Conditionals Hal Helms halhelms.com

iif()

<cfset randomBoolean = iif(RandRange(0,1), true, false) />

Page 23: Variables and Conditionals Hal Helms halhelms.com

switch/case

<cfswitch expression="#attributes.fuseaction#"><cfcase value="home">

<cfinclude template="dspHome.cfm" /></cfcase>

<cfdefaultcase><cfinclude template="qryMenuItems.cfm"

/><cfinclude template="dspMenu.cfm" />

</cfdefaultcase></cfswitch>

Page 24: Variables and Conditionals Hal Helms halhelms.com

Conclusion

• Variables are the basis of all programs• Data typing, although mostly hidden in

ColdFusion, is very important—particularly in OO languages

• Do experiment with "complex" data types; you'll find they make your code easier to write and maintain

• Conditionals allow us to execute commands based on the result of some run time boolean value