PencilCoder

Conditional Logic!

Goals:

  • Use conditional logic to control program flow
  • Use Boolean functions to enable sprites to interact with each other and with other elements on the screen

Coding Snippets:

  • coding snippets
  • Follow the keyword if with an expression that evaluates to true or false.
  • The argument to touches and inside can be sprites or the background window. touches also accepts colors.
  • Use a break statement within an if block inside a for block to immediately exit a loop if the specified condition is met. For example, the code on the left could be used to end a race when the sprite named ant reaches the (red) finish line.

Math and Computer Concepts:

  • Certain special statements allow us to control the flow of our code. We already use for statements to cause designated blocks of code to repeat. The conditional statements if and else are also tools for control. If the expression in the if statement evaluates to true, the program will branch one way, otherwise it will go in another direction.
  • Precede calls to functions that return information about the current state of the program, such as touches or inside, with a call to await done defer() to ensure that they are evaluated in the right place.

Activities:

Save each of the following in a folder called ConditionalLogic.