-
-
Save bytecodeman/268f949ddd493782bf2686ebc72a575c to your computer and use it in GitHub Desktop.
CSC-111 Ands and Ors Operators Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Prof. A.C.Silvestri | |
// Ands or Ors Demo | |
// 10/06/2018 | |
package chapter3; | |
public class AndsOrs { | |
public static void main(String[] args) { | |
int x = 5; | |
int y = 6; | |
//******************************************************************************************** | |
System.out.println("Testing AND Operator"); | |
// With And | |
if (x >= 0 && y >= 0) { | |
System.out.println("Both x and y are greater than 0"); | |
} | |
else { | |
System.out.println("Both x and y are not greater than 0"); | |
} | |
// No And | |
if (x >= 0) { | |
if (y >= 0) { | |
System.out.println("Both x and y are greater than 0"); | |
} | |
else { | |
System.out.println("Both x and y are not greater than 0"); | |
} | |
} | |
else { | |
System.out.println("Both x and y are not greater than 0"); | |
} | |
// Using Boolean Switches | |
boolean flag; | |
if (x >= 0) { | |
if (y >= 0) { | |
flag = true; | |
} | |
else { | |
flag = false; | |
} | |
} | |
else { | |
flag = false; | |
} | |
if (flag) { | |
System.out.println("Both x and y are greater than 0"); | |
} | |
else { | |
System.out.println("Both x and y are not greater than 0"); | |
} | |
// Using ?: operator | |
System.out.println(flag ? "Both x and y are greater than 0" : "Both x and y are not greater than 0"); | |
//******************************************************************************************** | |
System.out.println("Testing Or Operator"); | |
// With Or | |
if (x >= 0 || y >= 0) { | |
System.out.println("Either x or y are greater than 0"); | |
} | |
else { | |
System.out.println("Neither x or y are greater than 0"); | |
} | |
// No Or | |
if (x >= 0) { | |
System.out.println("Either x or y are greater than 0"); | |
} | |
else if (y >= 0) { | |
System.out.println("Either x or y are greater than 0"); | |
} | |
else { | |
System.out.println("Neither x or y are greater than 0"); | |
} | |
// Using Boolean Switches | |
if (x >= 0) { | |
flag = true; | |
} | |
else if (y >= 0) { | |
flag = true; | |
} | |
else { | |
flag = false; | |
} | |
if (flag) { | |
System.out.println("Either x or y are greater than 0"); | |
} | |
else { | |
System.out.println("Neither x or y are greater than 0"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment