# 8.1. Intro: What we can do with Turtles and ConditionalsÂ¶

So far, our programs have either been a series of statements which always execute sequentially or operations that are applied to each item in an iterable. Yet programs frequently need to be more subtle with their behavior. For example, a messaging app might only set a messageâ€™s title bold if it has not been read by the user. Or a video game needs to update the position of all the characters that are not asleep. This is done with something called a selection or a conditional statement.

In the context of turtle drawings, using this kind of statement will allow us to check conditions and change the behavior of the program accordingly

In the above code, we first set amyâ€™s pen color to be â€śPinkâ€ť and then move her forward. Next we want one of two actions to happen, either amy should move right and then forward, or left and then forward. The direction that we want her to go in depends on her pen color. If her pen color is set to pink - which is determined by writing amy.pencolor() == "Pink" which checks to see if the value returned by amy.pencolor() is the equivalent to the string â€śPinkâ€ť - then we should have her move right and forward. Else (or otherwise) she should move left and forward. Both things canâ€™t happen though. She canâ€™t move right, forward and left, forward. We then do the same thing for kenji, though in this case, we didnâ€™t change kenjiâ€™s pen color.

It might seem a bit odd to add the conditionals in this example. Wouldnâ€™t we already know that we set up amy and kenjiâ€™s colors, so why would we need a conditional? While itâ€™s true that this isnâ€™t the best place to use a conditional, we can combine conditional statements with for loops to make something pretty cool!

The above example combines a for loop with a set of conditional statements. Here, we loop through a list of colors and each iteration checks to see what amyâ€™s pen color is. Depending on the pen color, the turtle will move in a certain direction, for a certain distance. Before the for loop iterates, amyâ€™s pen color is changed to whatever color is in the for loop and it continues. Note how the color doesnâ€™t change until the end, so that we can start using whatever color amy is set to initally. This means that the last color in the list colors will not be used, though you can see how the icon changes to the appropriate color.

This chapter will further detail how to use conditional statements.

## 8.1.1. Learning GoalsÂ¶

• To understand boolean expressions and logical operators

• To understand conditional execution

• To be able to write a boolean function

• To know when to use binary, unary, chained and nested conditional statements

## 8.1.2. ObjectivesÂ¶

• To properly evaluate a (compound) boolean expression

• To use parenthesis to properly demonstrate operator precedence

• To use conditional statements to properly branch code