Skip to content

Instantly share code, notes, and snippets.

@bytecodeman
Last active October 10, 2018 17:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bytecodeman/268f949ddd493782bf2686ebc72a575c to your computer and use it in GitHub Desktop.
Save bytecodeman/268f949ddd493782bf2686ebc72a575c to your computer and use it in GitHub Desktop.
CSC-111 Ands and Ors Operators Demo
// 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