© Ron Dai 2019
R. DaiLearn Java with Mathhttps://doi.org/10.1007/978-1-4842-5209-3_10

10. Logical Control Structures

Ron Dai1 
(1)
Seattle, WA, USA
 
Very similarly to how we describe it verbally when there is a logical conversation, the if and the if/else in programming languages are common structures to make conditional decisions and choose corresponding execution paths (Figure 10-1).
../images/485723_1_En_10_Chapter/485723_1_En_10_Fig1_HTML.png
Figure 10-1

The if structure

if (a == 3) {
       <do something>
}
This is similar to, but not completely the same, as:
if (a == 3) {
       <do something>
} else {
       <do something else>
}
Figure 10-2 is the whole workflow of the if/else logical control structure.
../images/485723_1_En_10_Chapter/485723_1_En_10_Fig2_HTML.png
Figure 10-2

The if/else structure

Conditional Operators

The conditional operators listed in the next table are frequently used in conditional statements. For example, we use a == 3 to evaluate “is a equal to 3?” in a conditional statement. Java uses six different conditional operators to express a relationship between two operands. The results of the expressions are either true or false, a Boolean value, which determines the “yes” or “no” path of execution.

Conditional Operators

Description

==

Is equal to

>

Is greater than

>=

Is greater than, or equal to

<

Is less than

<=

Is less than, or equal to

!=

Is not equal to

Example

Which of the following if statement headers uses the correct syntax?
  1. (a)

    if x = 10 then {

     
  2. (b)

    if (x equals 42) {

     
  3. (c)

    if (x => y) {

     
  4. (d)

    if [x == 10] {

     
  5. (e)

    if (x == y) {

     

Answer

e

Example

Given the following method, what is the output from whatIsIt(9, 4)?
public static void whatIsIt(int x, int y) {
    int z = 4;
    if (z <= x) {
        z = x + 1;
    } else {
        z = z + 9;
    }
    if (z <= y) {
        y++;
    }
    System.out.println(z + " " + y);
}
Answer
10 4

Lab Work

  1. 1.

    Define an integer variable and assign value “3” to it.

     
  2. 2.

    Use an if statement to output “Hello” when the integer variable is assigned number 3.

     
  3. 3.

    Use an if/else statement to output “Goodbye” when any number other than 3 is assigned to the integer variable.

     
  4. 4.
    Is there anything wrong with the following code?
    int n = 4;
    if (n >= 3) {
           System.out.println("Hello!");
    }
    if (n == 4) {
           System.out.println("Hello again!");
    }
     
  5. 5.
    Use an if/else statement to implement the following requirements:
    • Output “less than 3” when the number is smaller than 3

    • Output “equals 3” when the number is 3

    • Output “greater than 3” when the number is bigger than 3

     
  6. 6.
    Input an integer number, and then,
    • Output “The number is greater than 6” when the input number is bigger than 6

    • Output “The number is smaller than 6” when the input number is smaller than 6

     
  7. 7.
    Explain what the following code snippet is trying to do:
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    if (n > 6) {
           if (n > 8) {
                  System.out.println("n is greater than 8");
           }
           else {
                  System.out.println("n is greater than 6, but n is smaller than 9");
           }
    }
     

Sometimes you may need to use a logical combination of multiple “true or false” conditions. Let’s introduce another concept here, in terms of “Logical Operators.”

Logical Operators

Math: Logical Operators

Logical operators and logical operations:
  • && ../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif AND relation

  • || ../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif OR relation

  • ! ../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif NOT relation

  • A && B ../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif indicates only when both A and B are true, the result is true. For example, in (x > 3 && x < 5), A is “x > 3”, B is “x < 5”.

  • A || B ../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif indicates when either A or B is true, the result is true.

  • !A ../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif indicates when A is true, the result is false; when A is false, the result is true. In the example of “!(x > 0)”, A is “x > 0”.

In all of these examples, A and B are expressions or Boolean variables.

(A && B)

A=true

A=false

B=true

True

False

B=false

False

False

Summary - Result is true, only when both A and B are true. Otherwise, result is false.

(A || B)

A=true

A=false

B=true

True

True

B=false

True

False

Summary - Result is false, only when both A and B are false. Otherwise, result is true.

A final example of some properties of these operators:
  • (x < 0 || x > 0) ../images/485723_1_En_10_Chapter/485723_1_En_10_Figb_HTML.gif../images/485723_1_En_10_Chapter/485723_1_En_10_Figa_HTML.gif (x != 0)

  • !(x == 0 || y == 0) is equivalent to (x != 0 && y != 0)

  • !(x > 3 && x < 5) is equivalent to (x >= 5 || x <=3)

Using a Venn diagram will help us analyze some type of logical problems.

Math: Analyzing Logical Problems

A Venn diagram is a visualization method to reveal logical relations among data sets.

In Figure 10-3, the overlap area between circle A and circle C is in area B.

if we define A = { x, y | x = 0 }, C = { x, y | y = 0 }, then B = { x=0; y=0 }.
../images/485723_1_En_10_Chapter/485723_1_En_10_Fig3_HTML.png
Figure 10-3

A Venn diagram

Lab Work

  1. 1.
    Figure out the output of the following program.
    public class LogicalOperation {
           public static void main(String args[]) {
                  boolean a = true;
                  boolean b = false;
                  System.out.println("a && b = " + (a&&b));
                  System.out.println("a || b = " + (a||b) );
                  System.out.println("!(a && b) = " + !(a && b));
           }
    }
     
  2. 2.

    Write a static method called quadrant that takes as parameters a pair of integer numbers representing an (x, y) point on the Cartesian coordinate system. It returns the quadrant number (i.e., 0,1,2,3,4, see picture) for that point.

     
Below are sample calls on the method.

Call

Value Returned

quadrant(12, 17)

1

quadrant(-2, 3)

2

quadrant(-15, -3)

3

quadrant(4, -42)

4

quadrant(0, 3)

0

Problems

  1. 1.
    Translate the following English statements into logical expressions:
    1. (a)

      z is odd.

       
    2. (b)

      x is even

       
    3. (c)

      y is positive.

       
    4. (d)

      Either x or y is even.

       
    5. (e)

      y is a multiple of z.

       
    6. (f)

      z is not zero.

       
    7. (g)

      y is a positive number, and y is greater in magnitude than z.

       
    8. (h)

      x and z are of opposite signs.

       
    9. (i)

      y is a nonnegative one-digit number.

       
    10. (j)

      z is nonnegative.

       
     
  2. 2.

    Given the following variable declarations: int x = 4; int y = -3; int z = 4;

    What are the results (True or False) of the following expressions?

    x == 4 x == y

    x == z y == z

    x + y > 0 x - z != 0

    y * y <= z y / y == 1

    x * (y + 2) > y - (y + z) * 2

     
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.156.140