# Unit IV Practice (If statements)

## Overview

Our familiarity with creating Boolean expressions (Unit III) allows us to move on to using them to control which actions occur in a program. We will use some of the Unit III exercises to address If statements. Also, we will provide some larger problem contexts in which require the use of If statements to achieve their goals. As you work on these exercises, be thinking of how some of them might apply to the project you are considering. Have fun.

The notes directory contains a zipped VB project that you can use to enter and test code for this assignment. Remember that programming practice activity can be checked by the computer—it it produces the answer you expect, your code is probably correct.

I encourage you to work with a partner, either using pair programming (one person types and one person watches for mistakes and swap roles every 30 minutes or so) or working side by side on separate computers. This way each person interacts with the code on each item—the only way to learn the material.

### Arithmetic Drill

Use the practice project's arithmetic drill form like that in the image at the right. You will want to examine the form to see the names of the various controls to produce appropriate code. Supply code for the following tasks.

1. Operation identification

When the form loads use an input box to ask the user if they wish to do A (addition), S (subtraction), M (multiplication), or D (division). Based on the response, change the Text properties of the lblTitle and the lblOperator to indicate the chosen operation. Use a class variable to save/indicate the operation. (When checking the operation, check for both upper and lower case letters.)

2. Problem generation

Also in the form-load subroutine, generate two random numbers between 0 and 10 (including both end points). Then assign appropriate values to class variables for the first value, second value, and answer. Name the variables as you wish. For subtraction & division, the "answer" will be one of the two random numbers.

Supply code for the "Check Me" button. Notify the user if nothing was entered. If something was entered before the button was clicked check to see if the value entered matches the "answer" and notify the user about the correctness.

### Pepe's Pizza App

Use the practice project's pizza app form like that in the image at the right. You will want to examine the form to see the names of the various controls to produce appropriate code. Supply code for the following tasks.

We imagine the form will be used by a server who "clicks" on the various radio buttons and button buttons to indicate the pizzas to be produced. The update button will normally cause a pizza description to be inserted in the list box (named lstOrder) and clear the form so another pizza could be added to the list. If, however, one of the items in the list is selected the update button will delete it from the order. The submit button finalizes the order.

1. Provide code that will declare a boolean status variable named orderOkay and set it to True. Declare a string variable called order and initialize it to the null string. (Other parts of your code will use them. The code we are writing here would go in the subroutine that handles a click of the "Update the Order" button.)
2. Check for size selection

Produce one or more If statements that will either make the order be 8" or 12" or 16" or 20" or set orderOkay to False depending on which radio button (or none) is checked. (To get the quotes to be part of a string I think you "escape" the quote by duplicating it, e.g., order = "12"" " should result in the variable containing 12".)

3. Check for crust selection

Produce one or more If statements that will either "add" to order or set orderOkay to False depending on which radio button (or none) is checked. Assuming rdo12 and rdoHand were checked, the value for order would be 12" Hand Tossed.

4. Check for toppings selection

Produce one or more If statements that will either "add" to order or cause a new form allowing topping selections to appear or set orderOkay to False depending on which radio button (or none) is checked. The new thing here is when the user selects "Build Your Own" (rdoCustom is checked) the code would normally open a window that allowed a variety of toppings to be chosen. For now, instead of doing that just put a comment in the if statement.

5. Check for deletion from order

If the list box contains one or more values the user can click on one to select it. If that happens the SelectedIndex property of the list box will indicate the position in the list of the selected item (numbering starts a 0). If nothing is selected the SelectedIndex property will have a vale of -1. To delete an item from a list, you use the RemoveAt() method. For example, is we have a variable pickedItem and it has a value of 3, then lstOrder.Items.RemoveAt(pickedItem) would cause the 4th item (the one in position 3) to be removed from the list.

Produce one or more If statements that will do nothing if one of the sets of buttons did not have something selected, that will remove the selected item from the list (if an item was selected), or that will add the current order to the list. Only one of the three things should occur.

### Miscellaneous Problems

Use the practice project's miscellaneous form like that in the image at the right for these exercises. You will want to examine the form to see the names of the various controls to produce appropriate code. Supply code for the following tasks. Use a message box to report the results.

1. Leap year
1. Produce a single If statement that takes the year value out of the text box and uses a message box to report whether it is a leap year.
2. Use nested If statements, each with a simple expression (no Ands or Ors), to set a boolean variable to True or False. After the if-then-else statement structure use another if statement to report whether it is a leap year or not.
3. Adapt the nested if-then-else structure above to indicate why the year is not a leap year when it isn't.
2. Grade "calculation" (use a message box to show the result)
1. Produce separate If statements (one for A, one for B, etc.) to set a value for the string variable grade depending on the value entered in the text box. Use the 90, 80, 70, 60, scale we have talked about in class.
2. Produce connected (or nested) If statements to set a value for the string variable grade.
3. Adjust the code for one of your results above to concatenate a plus (+) or minus (-) onto the value determined for grade.
3. IPERS retirement qualification

Recall that to draw retirement pay from the Iowa Public Employees Retirement System (IPERS) one must no longer work for IPERS employer and must fit into one of the following categories: Rule of 88 (age + years of service >= 88), Rule of 62/20 (age >= 62 and years of service >= 20), or age is 65 or older. Add a checkbox to the form and supply code to get necessary input and accomplish the tasks indicated below.

1. Produce a single If statement that determines whether one collect IPERS retirement. You may use a variable to store the result and have a single message box after the If or use two message box invocations and no variable.
2. Instead of reporting a single yes/no, report yes or no for each part of the expression, i.e., report the minimum number of years before retirement is possible or that retirement requires only that the person quit working for an IPERS employer.

Add a button to use for checking password strength. Make up your own definition for password strength or use this: Strength depends on: a) length (1 point per character) and one additional point for each of the following characteristics: b) lower-case letter, c) upper-case letter, d) digit, e) special character. Report strength as one of the following: weak (scores less than 10), okay (scores of 10-12, good (scores of 13-15), strong: (scores over 15).

1. Get the necessary strength data from the user and determine strength based on the planned password characteristics.
2. Ask the user for a password and then check its strength based on the actual password.

Note that you could compare a value and its .ToUpper() or its .ToLower() values. If they are not equal then they would contain lower case and upper case (respectively) characters. I am unaware of any "simple" way to check for special characters. You would need to check for each character using .IndexOf() for each of them. Probably the same for digits.

### Extra Credit

If you wish to submit something for extra credit download the blank projects and do the assignment in them. Then supply something that goes beyond the assignment. You might identify additional miscellaneous problems involving if statements. For the pizza app, it might include adding pricing information and/or preparing a receipt and storing it in a file (perhaps titled using the table value and a value for the current date & time. Or, you might do something else; impress me.