Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java provides equality, relational, and logical operators to evaluate and test w

ID: 3802310 • Letter: J

Question

Java provides equality, relational, and logical operators to evaluate and test whether an expression is true or false. It also provides selection statements to transfer control to a different part of the program depending on the result of that test: •What are the issues that may arise while using the IF statement? •In what circumstances would IF … ELSE be a preferred statement over SWITCH and vice versa? Please use real-world examples to support your arguments. •Use specific, real-world examples to describe the uses of the logical AND operator (&&), the logical OR operator (||), the logical NOT operator (!) and the combination of those operators in making decisions.

Explanation / Answer

We should use "if else" statement or switch statement according to the problem. Switch statement is more suitable when we actually know all the possible values a variable can take. Switch statement basically says "Pick one of these according to the value of variable."   

Real life example of switch statement to print value of digit in string form:


package javaapplication1;

import java.util.Scanner;

public class code {

public static void main(String[] args)
{
int digit = 2;
switch(digit) {
case 1 :
System.out.println("one!");
break;
case 2 :
System.out.println("Two!");
break;
case 3 :
System.out.println("Three");
break;
case 4 :
System.out.println("Four");
case 5 :
System.out.println("Five");
break;
default :
System.out.println("Digit does not belong in range 1 to 5");
}
}
  
}

Real life example of switch statement to check if a variable is odd/even:


package javaapplication1;

import java.util.Scanner;

public class code {

public static void main(String[] args)
{
int digit = 1;
if(digit%2 == 0)
{
System.out.println("Even number!");
}
else
{
System.out.println("Odd number!") ;   
}
}
  
}

Use of logical AND operator (&&), the logical OR operator (||), the logical NOT operator (!) :


package javaapplication1;

import java.util.Scanner;

public class code {

public static void main(String[] args)
{
int digit = 1;
if(digit > 0 && digit < 6)
{
System.out.println("The number is in range 0 < number < 6");
if(digit%2 != 0 )
{
System.out.println("The number is not even");
}
else
{
System.out.println("The number is even");
}
}
else
{
System.out.println("The number is not in range 0 < number < 6");
if(digit%2 != 0 )
{
System.out.println("The number is not even");
}
else
{
System.out.println("The number is even");
}
}
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote