## The if Statement [1/16]

### The if Statement

The **if** statement executes the following
statement block only if the given **condition** is true.

After you run the program, click on the "Restart" button several times to observe the behavior of the program.

An if statement can optionally have the **else** part.

After you run the program, click on the "Restart" button several times to observe the behavior of the program.

This program rolls the dice 15 times and notifies you if it gets a 6.

This program rolls two dice 15 times and notifies you if it gets two equal numbers.

The if statement treats statement lists the same as statement blocks. In this example the statement list contains only one statement.

This program draws 50 discs. The smaller ones are painted red, and the bigger ones are in blue.

### Exercise:

Modify this program so that the discs on the left half of the screen are painted in red, and the ones on the right half of the screen are in blue.

Solution

ifx<480: mycolor = /red;

(Note: change the highlighted expression, nothing else.)

This program draws 150 discs. The discs in the left third of the screen are painted in red, the discs in the right third of the screen in orange, and the ones in the middle are in blue.

### Exercise:

Modify this program so that the discs with a radius smaller than 40 are painted in red, the discs with a radius greater than 85 are painted in orange, and the other ones are in blue.

Solution

ifradius<40: mycolor = /red; ifradius>85: mycolor = /orange;

To check whether a number is within a given range, two conditions must be used.

The conditions can be joined by the **and** operator.

This example contains an error. The given syntax cannot be used to check if x is between 4 and 7.

Correct this example by using the and operator to join two conditions, as in the previous example.

Run the program to verify that it works correctly.

If there are multiple conditions, they can be separated by commas (,) instead of using the and operator.

The **or** operator can be used to join two conditions.

This program draws 150 discs. The discs in the middle of the screen are painted in red, and the discs on the sides are in blue.

### Exercise:

Modify this program so that only the discs in the top left quarter of the screen are painted in red, and other ones in blue.

Solution

ifx<480andy<360: mycolor = /red;

This program draws 150 discs. The discs in the upper third and bottom third of the screen are painted in red, and the ones in the middle are in blue.

### Exercise 1:

Modify this program so that only the discs with a radius smaller then 40 or radius greater than 85 are painted in red, and other ones in blue.

Solution

ifradius<40orradius>85: mycolor = /red;

This program draws 2000 small discs. The discs to the right of line x=300 and above line y = 500 are painted in red, and the other ones are in blue.

### Exercise:

Imagine a rectangle in the middle of the screen with the left edge at x = 200 , right edge at x = 760 , top edge at y = 170 and bottom edge at y = 550 . Modify the given prorgam so that only the discs whose centers lie in the descibed rectangle are painted in red, and other discs in blue.

Solution

ifx>200,x<760,y>170,y<550: mycolor = /red;

Some of the **comparison operators** include:

< | less than |

> | more than |

<= | less than or equal to |

>= | more than or equal to |

= | equal to |

<> | not equal to |

This program uses several if statements to draw a row of discs in various colors.

The else-if chain can be used to accomplish the same result.