Java Whole-cl sdfsdass module 1: Controlling program flow with if() and while()

Using boolean expression testing to selectively run blocks of code

Language Core: Control of flow with if() and while()


Control of Flow: if() and while() structures

Controlling the path of execution of a program remains a central component of fluency in any programming language, Java included. This module involves creating a survey/quiz tool which tabulates multiple responses to the same set of questions and displays summary statistics to the user.

To get our mind jogging about how quizzes and computers work together, take this brief learning styles quiz and pay particular attention to the answer choices you are given and how you select them.

If() statements evaluate an expression that resolves to true or false to determine if its special block of code should be executed. By building in tests such as these, we can create paths of execution based on the data coming into the program during run time.

Many resources exist for learning about control structures:

Phase 1: Create a program flow diagram

Before we jump into coding, we want to have a clear picture of how the program will accomplish the task of repeatedly asking a question to the user and tabulating the responses for display. Note that we represent a decision structure in our flow diagrams with a diamond. The lines coming out of the diamond represent the possible outcomes of this choice.

Review the systems diagramming guide for more tips and explanation on how to sketch out flows in code and map relationship between the application's parts.

Review the flow chart below which depicts logic which notifies the user if he or she is of age to retire according to the US IRS. We'll then implement this logic in Java.

Notice that there is only one decision that the system makes: comparing the age entered by the user to a static value, which we store in a variable called retirementAge.

Phase 2: Create a basic question and response tool

Start with the most simple components and expand from there: write a program that asks a question of the user, stores the response, and prints a response based on the input of the user. This will use the if() statement to check the response of the user against a pre-set key.

Phase 3: Integrate a while() loop to repeatedly ask the user the question

Use a while() loop with a fixed value of TRUE inside the parentheses to loop through the program forever. It will look something like this:

This example was for our sample little retirement program. Implement this kind of logic for your question asking question.

When you have the loop going forever, we can now add some functionality to allow the user to exit the loop by changing the value of our continueLoop boolean variable.

Phase 4: Calculate summary statistics

At the end of your if() block, but BEFORE the end of the while() loop, print out the total responses received and the number of respondents who answered a certain way. You can use my sample code on github to check the method for calculating a percent with integer values.

Review sample code

You may look over Eric's commented code in the Tabulator class. Don't peek to avoid doing thinking--make sure you're trying pretty hard before looking at the key.

Extension Activities

  1. Add a method for the user to escape the while loop and display the overall statistics only when the while() loop is exited. You'll want to create a control variable that starts with a value of "true" and is toggled to "false" if the user enters a certain number instead of a response to the question.
  2. Create a question whose possible responses are more than 0 or 1, such as a scale of agree to disagree. Devise a method for tabulating and displaying these responses.
  3. Create an if() control system that asks the user a follow-up question based on their response to the first question. Tabulate all of these responses after the while loop is exited.
  4. Create a question that requires a text response, rather than just an integer. You'll use String objects to store the responses. Scanner.next() will gather text from the user. You'll also need to compare String values with String.equals() rather than an == operator that is appropriate only for comparing integer values. Check online resources for more tips on this.
`