Instantly share code, notes, and snippets.

@bytecodeman /AndsOrs.java Secret
Last active Oct 10, 2018

Embed
What would you like to do?
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