If / Then / Else

 

Making decisions with if/then

 

if/then is used to make a program respond to different situations.  the general syntax is

 

if condition then  response

 

"Condition" is some question your program is asking, and the "response" is what your program does if the answer is Yes. 

 

Some real world examples:

 

if it is cold out, then bring a coat

if the light is green, then cross the street

if you're hungry, then go get a sandwich 

 

Some lingo code examples: (try these in the message window)

 

x = 10

put x

if x = 10 then put "bing!"

if x = 5 then put "boing"

 

the if statement only does something if the answer is yes.   yes/no questions are also called true/false questions.  true and false are special values used to keep track of simple pieces of data, like whether it is cold out:

 

isColdOut = true   --try 'false' also

if isColdOut = true then put "you better bring a coat"

 

variables used to hold true/false values are called flags.  because they are so common in if/then statements, here's a shortcut you can use:

 

if isColdOut then put "you better bring a coat"

 

true/false values and decision-making is called "Boolean logic", and is the lowest-level foundation of what makes computers work: down at the bottom, everything is just bits, tiny electrical impulses turning on and off, all 1's and 0's.

 

 

 

 

 

 

 

 

 

Other comparisons:

 

a = b                a equals b       

a < b                a is less than b

a > b    a is greater than b

a <= b              a is less than or equal to b

a >= b              a is greater than or equal to b

a <> b              a is not equal to b

 

try these, with different values of a and b:

 

a = 5

b = 10

 

if a<b then put "a is less than b"

 

if a>b then put "a is greater than b"

 

if b>a then put "b is greater than a"

 

a = 10 

 

if a>b then put "a is greater than b"

if a=b then put "a equals b"

if a>=b then put "a is greater than or equal to b"

 

if a <> b then put "a is not equal to b"

 

one way of planning out how your program should behave is to use a Flowchart.  this is a diagram of the different steps in the program, with lines from one to the next and multiple lines to represent if/then statements.   (see diagram)

 

Using if/then in an event handler:

 

-- flip back and forth while dragging

on mousewithin

  if sprite(1).loch < 160 then sprite(1).flipH = false

  if sprite(1).loch > 160 then sprite(1).flipH = true

end

 

notice that you can have multiple if/then statements to respond to different conditions.

 

 

 

 

you can also make an if/then statement run more than one command:

 

-- flip back and forth and grow/shrink

on mousewithin

  if sprite(1).loch < 160 then

    sprite(1).flipH = false

    sprite(1).height=100

  end if

 

  if sprite(1).loch > 160 then

    sprite(1).flipH = true

    sprite(1).height=50

  end if

 

end

 

so the script will respond to moving the sprite to the left side or the right side of the stage, but what about the center?  add a third if/then statement and it will cover all possibilities.

 

-- flip back and forth and grow/shrink

-- all three cases: left, right, and

-- exact center.

 

on mousewithin

  if sprite(1).loch < 160 then

    sprite(1).flipH = false

    sprite(1).height=100

    sprite(1).width=50

    sprite(1).rotation = 0

  end if

 

  if sprite(1).loch > 160 then

    sprite(1).flipH = true

    sprite(1).height=50

    sprite(1).width=100

    sprite(1).rotation = 0

  end if

 

  if sprite(1).loch = 160 then

    sprite(1).flipH = false

    sprite(1).rotation = 90

    sprite(1).width=200

    sprite(1).height=200

  end if

 

end


 

 

if/then statements that all test the same variable can be combined using else if:

 

  if sprite(1).loch < 160 then

    sprite(1).rotation = 0    

  else if sprite(1).loch > 160 then

    sprite(1).rotation = 180  

  else if sprite(1).loch = 160 then

    sprite(1).rotation = 90

  end if

 

you can have as many else if clauses as you want.  you can also simplify them by making the last one just an 'else' - a default option if none of the above are true.

 

  if sprite(1).loch < 160 then

    sprite(1).rotation = 0    

  else if sprite(1).loch > 160 then

    sprite(1).rotation = 180  

  else

    sprite(1).rotation = 90

  end if

 

 

Other examples:

 

-- make sprite 1 moveable.

-- it likes to be at the right edge

-- of the stage

on mouseup

  if sprite(1).loch > 300 then

    sprite(1).member="happy"

  else

    sprite(1).member="sad"

  end if

end

 

 

-- make sprite 2 moveable.

-- put this behavior on it.

-- it will always face sprite 1.

 

on mousewithin

  if sprite(2).loch < sprite(1).loch then

    sprite(1).fliph = false

  else

    sprite(1).fliph = true

  end if

end

 

 

 

Better Drag and Drop

 

note that there is a mousewithin event that runs repeatedly while the mouse is over a sprite - but there is no corresponding one that runs repeatedly while the mouse is down.  you can get the same result, though, with the lingo function stilldown().  stilldown() returns true if the button is still down, otherwise it returns false.  you can use it inside a mousewithin handler like this:

 

on mousewithin

  if stilldown() then

    sprite(1).blend = random(100)

  end if

end

 

 

To drag and drop one sprite onto another, use the intersects function:

 

if sprite(dragging sprite).intersects(drop sprite) then ...

 

here's an example handler that you could use to open a door.

 

-- put this behavior on sprite 2

-- drag onto sprite 1

 

on mouseup

  if sprite(2).intersects(1) then

    sprite(1).member = "open"

  else

    sprite(1).member = "closed"

  end if

end

 

Using the within() function instead:

 

on mouseup

  if sprite(2).within(1) then

    sprite(2).member = "happy"

  else

    sprite(2).member = "sad"

  end if

end


 

Click counters: global variables

 

this handler should pop up an alert with the number of times you've clicked the sprite.  'clicks' is a variable to count the clicks - add one to it each time.

 

on mousedown

  clicks = clicks + 1

  alert ("you have clicked me" && clicks && "times")

end

 

it doesn't actually work because the variable is forgotten when the handler ends.

 

to fix this, you can use a global variable.  this is one that will persist, and can also be accessed from other scripts throughout the movie.  to make a variable global, declare it with the global command at the beginning of the script.  if you use that variable in multiple scripts, you must declare it global in each one.

 

global clicks

on mousedown

  clicks = clicks + 1

  alert ("you have clicked me" && clicks && "times")

end

 

here's another use for the if/then statement: resetting counters

this is an example of a boundary condition.

 

-- click up to 5 and reset

global clicks

on mousedown

  clicks = clicks + 1

  if clicks > 5 then clicks = 0

  alert ("you have clicked me" && clicks && "times")

end 

 

or:

 

-- click up to 5 and hold

global clicks

on mousedown

  clicks = clicks + 1

  if clicks > 5 then clicks = 5

  alert ("you have clicked me" && clicks && "times")

end  

 


Using drag & drop or Counters for Navigation

 

set up a basic navigational movie, with two sections named "section 1" and "section 2".

set up frame scripts to loop in the two sections. (see diagram).

put this behavior on sprite 2

drag sprite 2 inside sprite 1 to jump to "section 2."

 

on mouseup

  if sprite(2).within(1) then

    go "section 2"

  end if

end

 

 

in section 2, make a click counter to jump back:

 

-- click up to 5 and jump

global clicks

on mousedown

  clicks = clicks + 1

  if clicks > 5 then

    go "section 1"

  else

    alert ("click me again!")

  end if

 

end