Variables and Conditionals - PowerPoint PPT Presentation

About This Presentation
Title:

Variables and Conditionals

Description:

Variables give context and constraint to values ... Different data types hold different types of values. string. int. structure. Car ' ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 25
Provided by: michael118
Category:

less

Transcript and Presenter's Notes

Title: Variables and Conditionals


1
Variables and Conditionals
  • Hal Helms
  • halhelms.com

2
What we'll cover
  • Basics of variables
  • Types of variables (strings, arrays, etc.)
  • Scopes of variables (session, application, etc.)
  • Conditionals (working with boolean variables)

3
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

4
Basics
  • Variables give context and constraint to values
  • The 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

5
Strong data typing
  • Some languages employ strong typing, requiring
    the programmer to declare a variable's data type
    prior to using it
  • String myName
  • int myAge
  • Company myEmployer
  • Examples
  • Java
  • C

6
Weak data typing
  • Other languages use weak typing, in which the
    language implicitly determines the data type of a
    variable
  • Examples
  • Smalltalk
  • ColdFusion
  • Weakly typed languages are not untyped languages!

7
Data typing
  • Different data types hold different types of
    values
  • string
  • int
  • structure
  • Car

"Visualize whirled peas"
1024
name
"Hal Helms"
ACMmember
true
numberOfChildren
1
8
ColdFusion data types
  • string
  • numeric
  • query
  • list (string)
  • structure
  • array
  • object
  • boolean
  • date
  • GUID
  • UUID

9
Complex variable type query
  • query
  • series of 1 or more rows with column or field
    values for each row

id
description
price
100
Widget
29.99
200
Framus, left-handed
75.00
products
  • addressable
  • products.price refers to current row
  • products.price2 specifies row

10
Complex variable type structure
  • structure
  • collection of name/value pairs grouped around a
    central idea or item

teacher
Hal Helms
Ben Edwards
maxStudents
10
isActive
true
javaClass
  • addressable
  • javaClass.maxStudents
  • javaClass'maxStudents'

11
Complex variable type 1-d array
  • one-dimensional array
  • collection of ordered values
  • resembles a single row of a spreadsheet

1
2
3
Hal Helms
Sean Corfield
Ben Edwards
onedee
  • addressable
  • onedee2

12
Complex variable type 2-d array
  • two-dimensional array
  • collection of ordered values
  • resembles multiple rows of a spreadsheet

1
2
3
1
68
84
72
62
69
69
2
  • addressable
  • twodee13

twodee
13
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

14
Quiz time!
  • For each of the following,explain which data type
    you'd use and why
  • final test scores for all students
  • collection of Student objects
  • lyrics to "Memories" (thank heavens for garbage
    collection!)
  • number of students in a class
  • information from LDAP server
  • composer's date born, date died, nationality,
    typical genre
  • an hourly employee

15
Understanding scopes
  • A variable typically has a lifespan and a scope
  • lifespan how long the system keeps a reference
    to a variable
  • scope 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

16
Understanding scopes
prefix 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
17
Quiz time!
  • For each of the following,explain which scope
    you'd use and why
  • global "constants" such as datasource name
  • query meant to be created and used on same page
  • a CFC, currentUser, that encapsulates data and
    behavior for the currently logged in user
  • a cookie crumb array
  • a userID meant to identify the user between
    visits
  • a custom tag "reaching into" the base template to
    read or set a variable on that page

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

19
ltcfifgt
  • ltcfif form.betNumber EQ winningNumbergt
  • ltcflocation url"index.cfm?fuseactionhome.winner
    " /gt
  • lt/cfifgt

20
ltcfelsegt
  • ltcfif form.betNumber EQ winningNumbergt
  • ltcflocation url"index.cfm?fuseactionhome.winner
    " /gt
  • ltcfelsegt
  • ltcflocation url"index.cfm?fuseactionhome.loser"
    /gt
  • lt/cfifgt

21
ltcfelseifgt
  • ltcfif form.betNumber EQ winningNumbergt
  • ltcflocation url"index.cfm?fuseactionhome.winner
    " /gt
  • ltcfelseif form.betNumber LT 0gt
  • ltcflocation url"index.cfm?fuseactionhome.cheate
    r" /gt
  • ltcfelsegt
  • ltcflocation url"index.cfm?fuseactionhome.loser"
    /gt
  • lt/cfifgt

22
iif()
  • ltcfset randomBoolean iif(RandRange(0,1), true,
    false) /gt

23
switch/case
  • ltcfswitch expression"attributes.fuseaction"gt
  • ltcfcase value"home"gt
  • ltcfinclude template"dspHome.cfm" /gt
  • lt/cfcasegt
  • ltcfdefaultcasegt
  • ltcfinclude template"qryMenuItems.cfm" /gt
  • ltcfinclude template"dspMenu.cfm" /gt
  • lt/cfdefaultcasegt
  • lt/cfswitchgt

24
Conclusion
  • Variables are the basis of all programs
  • Data typing, although mostly hidden in
    ColdFusion, is very importantparticularly 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
Write a Comment
User Comments (0)
About PowerShow.com