Chapter 3.5.3 – If Statement Examples | Introduction to Programming Using Java

Chapter 3.5.3 – If Statement Examples | Introduction to Programming Using Java

 

3.5.3 If Statement Examples

 

As an example of using if statements, lets suppose that x, y, and z are variables of type int, and that each variable has already been assigned a value. Consider the problem of printing out the values of the three variables in increasing order. For examples, if the values are 42, 17, and 20, then the output should be in the order 17, 20, 42.

One way to approach this is to ask, where does x belong in the list? It comes first if it’s less than both y and z. It comes last if it’s greater than both y and z. Otherwise, it comes in the middle. We can express this with a 3-way if statement, but we still have to worry about the order in which y and z should be printed. In pseudocode,

 

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

 

Determining the relative order of y and z requires another if statement, so this becomes

 

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

 

You might check that this code will work correctly even if some of the values are the same. If the values of two variables are the same, it doesn’t matter which order you print them in.

Note, by the way, that even though you can say in English “if x is less than y and z,”, you can’t say in Java “if (x < y && z)”. The && operator can only be used between boolean values, so you have to make separate tests, x<y and x<z, and then combine the two tests with &&.

There is an alternative approach to this problem that begins by asking, “which order should x and y be printed in?” Once that’s known, you only have to decide where to stick in z. This line of thought leads to different Java code:

 

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

 

Once again, we see how the same problem can be solved in many different ways. The two approaches to this problem have not exhausted all the possibilities. For example, you might start by testing whether x is greater than y. If so, you could swap their values. Once you’ve done that, you know that x should be printed before y.

 

Finally, let’s write a complete program that uses an if statement in an interesting way. I want a program that will convert measurements of length from one unit of measurement to another, such as miles to yards or inches to feet.

So far, the problem is extremely underspecified. Let’s say that the program will only deal with measurements in inches, feet, yards, and miles. It would be easy to extend it later to deal with other units. The user will type in a measurement in one of these units, such as “17 feet” or “2.73 miles”.

The output will show the length in terms of each of the four units of measure. (This is easier than asking the user which units to use in the output.) An outline of the process is

 

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

 

The program can read both parts of the user’s input from the same line by using TextIO.getDouble() to read the numerical measurement and TextIO.getlnWord() to read the unit of measure. The conversion into different units of measure can be simplified by first converting the user’s input into inches. From there, the number of inches can easily be converted into feet, yards, and miles. Before converting into inches, we have to test the input to determine which unit of measure the user has specified:

 

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

 

Since units is a String, we can use units.equals(“inches”) to check whether the specified unit of measure is “inches”. However, it would be nice to allow the units to be specified as “inch” or abbreviated to “in”. To allow these three possibilities, we can check if

(units.equals(“inches”) || units.equals(“inch”) || units.equals(“in”)). It would also be nice to allow upper case letters, as in “Inches” or “IN”. We can do this by converting units to lower case before testing it or by substituting the function units.equalsIgnoreCase for units.equals.

In my final program, I decided to make things more interesting by allowing the user to enter a whole sequence of measurements. The program will end only when the user inputs 0. To do this, I just have to wrap the above algorithm inside a while loop, and make sure that the loop ends when the user inputs a 0. Here’s the complete program:

 

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

Chapter 3.5.3 - If Statement Examples | Introduction to Programming Using Java

 

 

(Note that this program uses formatted output with the “g” format specifier. In this program, we have no control over how large or how small the numbers might be. It could easily make sense for the user to enter very large or very small measurements. The “g” format will print a real number in exponential form if it is very large or very small, and in the usual decimal form otherwise.

Remember that in the format specification %12.5g, the 5 is the total number of significant digits that are to be printed, so we will always get the same number of signifant digits in the output, no matter what the size of the number.

If we had used an “f” format specifier such as %12.5f, the output would be in decimal form with 5 digits after the decimal point. This would print the number 0.0000000007454 as 0.00000, with no significant digits at all! With the “g” format specifier, the output would be 7.454e-10.)

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 2 – Names and Things | Introduction to Programming Using Java

Chapter 2.5 – Details of Expressions | Introduction to Programming Using Java

Chapter 2.5.1 – Arithmetic Operators | Introduction to Programming Using Java

Chapter 2.5.3 – Relational Operators | Introduction to Programming Using Java

Chapter 2.6.2 – Command Line Environment | Introduction to Programming Using Java

Chapter 2.6.3 – IDEs and Eclipse | Introduction to Programming Using Java

Chapter 3.1.3 – The Basic If Statement | Introduction to Programming Using Java

1 thought on “Chapter 3.5.3 – If Statement Examples | Introduction to Programming Using Java”

Leave a Comment