### Objectives

Students who complete this lab will demonstrate that they can:

• Implement logical expressions
• Implement selective behavior using simple and multi-branch `if` statements

# Logical Expressions

The text discusses logical expressions in Chapter 3, Section 3.3.4, but we’ve deferred their use until now. You will use them to specify the conditions for selection statements. Remember that you can find the value of an expression by printing it out to the console.

Exercise 5.1

Record your answers to the following questions in a text file called `Exercise51`

1. What is the return value of the following expressions (if any)?

```true
true == "true"
false || !false
!!true
(1/0 == 1) || true
true || (1/0 == 1)
```
2. What is the return value of the following expressions (if any)?

```5 = 5
5 == 5
4 != 5
4 <> 5
```
3. Declare an integer variable `i` , initialize it to 5 and then try computing the value of the following expression:

```1 <= i <= 100
```

Record what happens and why. If there is a problem, rewrite the expression so that it performs as intended.

Test your expression by verifying that you get the right answer when `i` is set to different values covering the following cases:

1. `i` is too small;
2. `i` is too large;
3. `i` is nicely in the range;
4. `i` is on the border of the range.

# Simple `if` Statements

Introducing selection statements allows us to greatly expand the range of programs we’re able to program.

Exercise 5.2
 Create an animated program called `Circle` that draws random points on the canvas provided that they fall within a large circular area in the very middle. See the sample output given on the right (which draws 25-pixel points). We suggest that you proceed iteratively as follows: Start with a simple animation that draws red points at random locations anywhere on the output canvas. This first iteration only requires techniques from the previous chapters. Now, modify your program to selectively draw the points only when they are in a circular area centered in the middle of canvas with a diameter equal to the width of the canvas. Do this by implementing the following algorithm for your `draw()` method: Pick random values for x and y. If the point (x,y) falls in the target circle then Draw a point at (x,y) You most likely already had statements 1 and 2a in your program. You need to add the `if` statement. Use the `dist()` to specify the selection condition statement.

There are, of course, situations in which we would like to choose between two alternatives.

Exercise 5.3
 Make a copy of your program from the previous exercise and name it `Split` . This new program should still animate random dots, but it should put one color on one side and another color on the other side, see the example given on the right. For this one, you should implement the following, somewhat different algorithm: Pick random values for x and y. If the point (x,y) falls on the left side then Set the stroke color to blue. otherwise Set the stroke color to green. Draw a point at (x,y) Notice that in this algorithm, you always draw a point, but the color is chosen selectively.

# Multi-branch `if` Statements

All control statements can be used recursively, and the selection statement is no exception. If you need more than two alternatives, use a selection statement inside another selection statement.

Exercise 5.4
 Create a copy of the previous program and call it `ThreeWay` . This program should split the colors three ways across the canvas as shown in the sample output given on the right. Create your own algorithm for this one, and proceed iteratively as follows: Start by building a point-drawing version similar to the ones you created above. Choose whatever colors you’d like to use and use a multi-branch `if` statement to implement your selection. Modify your program to draw circles rather than points, whose color includes random shades of your three chosen colors and whose size is randomly assigned. The sample output on the right is an example only. Make any modifications you’d like provided that you still demonstrate the use of the multi-branch ` if` statement. As you complete this exercise, try to create and implement efficient algorithms, i.e., algorithms with no unnecessary elements.

# Challenge

The following exercise is optional and offers extra credit.

Exercise 5.5 (extra credit)

Create a program called `Checkerboard` that implements something like the sample output shown below. The three images show the same animation over time. At first, it’s not at all clear what’s happening, but the checker-board structure soon becomes apparent.

→      →

This program is a rather straightforward use of the selection statements used in the previous exercises, but the trick it so get the condition expression(s) done just right. The use of integer division and of modulus are critical here.

# Checking In

Submit all the code and supporting files for the exercises in this lab. We will grade this exercise according to the following criteria:

• Correctness:
• 10% - Boolean Expressions - Demonstrate an understanding of the use of logical expressions, see exercise 5.1.
• 70% - Programs - Complete the three selection exercises (25% each).
• Efficiency:
• 10% - Conciseness - Use clear, concise and efficient selection statements and logical conditions.
• Understandability:
• 5% - Header Documentation - Document the code’s basic purpose, authors and assignment number.
• 5% - Code Documentation - Separate the logical blocks of your program with useful comments and white space.