Multiple conditional if() statements in Java?

multiple in if statement java
best way to format multiple 'or' conditions in an if statement (java)
how to check multiple conditions in if statement in java
java if statement or
c programming if statement multiple conditions
nested if statements java
java 8 if statement multiple conditions
multiple conditions in if statement in selenium

I know this is going to seem like an unforgiveable sin, but I have 150 variables, and I want to create an if function that does the following:

if(a != 0 && b != 0 && c != 0....) {
  //do this
} else {
  //do this instead
}

However, when I set one of the if statement conditions (i.e b!=1), whilst the others are still the same (a!=0, c!=0), the else function is still run, even though it should be the initial //do this instead of the //do this instead (see code snippet above).

My question is:

1. Is there a reason why it is not working, and if there is, how do i fix it? 2. Is there a simpler way to do that without having to list all 150 variables !=0 && !=0 etc..?

Cheers

Surely it would be simpler to do something like this:

List<Integer> values = new ArrayList<Integer>();

values.add(val1);
values.add(val2);
// And so forth...

boolean pass = true;

for (Integer v : values) {
    if (v != 0) {
        pass = false;
        // You could log which variable failed here...
    }
}

if (pass) {
    // Do something
} else {
    // Do something else
}

Best way to format if statement with multiple conditions, Java's if statement is the most basic conditional statement – it test against multiple boolean expressions, so plain if -then- else does not quite  Java has the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

With an array and a loop and a boolean. Review your arrays... chances are... your teacher wouldn't have you create 150 integer variables.

int[] integers = new int[150];
integers[0] = 1; // Set your like 150 variables like this
integers[1] = 2; 

boolean isAllNotZero = false;

for(int i = 0; i < integers.length - 1; i++) {
   if(integers[i] != 0) {
     boolean isAllNotZero = true;
   }
}

if(isAllNotZero) {
  // Do something 
} else {
  // Do something else
}

Java's If Statement in Five Minutes, Nested IF Statements. You can nest IF Statements. (This also applies to IF ELSE and IF ELSE IF statements.) Nesting an  If you change the dollar amounts in Listing 10-1, you can get into trouble. For example, with the statement price = 5.00, the program displays Please pay $5.0. Enjoy the show! This happens because Java doesn’t store the two zeros to the right of the decimal point (and Java doesn’t know or care that 5.00 is a dollar amount).

1) I'm not quite sure what you mean by "set one of the if statement conditions". But if you mean that the b-related test evaluates to true while the others still relate to false, then the behaviour is intended. You're using a logical AND, &&, to join your conditions. Which means that the overall condition is true if and only if all of the individual conditions are true.

If you want the first block to be executed when any of the individual conditions is true, then you use should use an OR (||) instead.

2) An if statement always requires a single boolean condition - which in this case you've formed from a conjunction of 150 individual conditions. If you need to inspect 150 fields to reach the overall decision, then somehow these will all need to be accessed.

However, you might be able to make it easier on yourself. For one thing, if the a and the b etc., are part of an object, then you can provide a nice public method that describes what the condition is, and hides the nasty details away (e.g. public boolean isReadyToExecute()).

Also, if you have 150 variables, chances are they're all instances of the same sort of thing. Say, for example, you're tracking 150 files to see whether they've been generated. Instead of having variables like:

private boolean fileReady1;
private boolean fileReady2;
// ...
private boolean fileReady150;

You can use an array (or some other collection) instead:

private boolean[] filesReady = new boolean[150];

Now you don't have to spell out every variable by hand, in order to come up with your boolean condition:

public boolean isReadyToLoad() {
   for (int i = 0; i < filesReady.length; i++) {
       if (!filesReady[i]) {
           // Found a file that wasn't ready, so we're not ready overall
           return false;
       }
   }
   // Got through all the files without finding an unready one
   return true;
}

The exact approach will depend on the nature of your problem. But I reckon that some combination of information hiding (i.e. wrapping things in nice methods) and loops will cover anything you need to do.

(And don't forget that wrapping things in methods can be nested. Perhaps the first 20 fields represent one set of things, the next 30 represent another, etc. Then you could wrap each of these in a method, and implement isReadyToLoad() as something like allFilesLoaded() && status.isSignedOff() && userDao.allUsersHavePasswords()...)

java for complete beginners - if else, (for example, by returning from the method or breaking from a loop). conditional statements let a Java program make simple decisions about what to do next. They work in the same logical way as we do when making decisions in real life. They work in the same logical way as we do when making decisions in real life.

You can do it by stream.

List<Integer> vars = new ArrayList<>();
vars.add(a);
vars.add(b);

if (vars.stream().noneMatch(v -> v == 0)) {
   // No variable = 0. Do something
} else {
   // Do something
}

Are multiple 'if' statements and 'if-else-if' statements the same , that computer programming language used to decide which code has to be run when the true condition is met or which code has not to be run when the true condition is not met. Nested if statement in Java When there is an if statement inside another if statement then it is called the nested if statement . The structure of nested if looks like this:

It is hard to say without knowing more details about your solution. A Karnaugh map may help you.

Java tutorial- conditional statements, output a letter grade based on a score out of 100. Java if else statement, if else statement in java, java if statement, java multiple if, java if-else, java if-else-if, java if else if ladder statement, and java nested if with concepts and examples, java control statements.

If Statements, If Else Statements, Nested If, Ternary Operators , In the past, I used to use nested conditional statements, like so. You don't care which condition fails, you only want to know if they all succeed or not. as I now need to query errors.size() to know whether I have failed anything): If we only need the first error, in Java I'd do fail-fast while in Scala I might jump: Java supports three jump statement: break, continue and return. These three statements transfer control to other part of the program. Break: In Java, break is majorly used for: Terminate a sequence in a switch statement (discussed above). To exit a loop. Used as a “civilized” form of goto.

Approaches to checking multiple conditions?, When we need to execute a set of statements based on a condition then we need to if-else-if statement is used when we need to check multiple conditions. The statement that goes in the if or else part of an if-else statement can be any kind of Java statement, including another if or if-else statement. This arrangement is nesting, and an if or if-else statement that includes another if or if-else statement is a nested if statement. The general

If, If..else Statement in Java with Examples, Learn various ways of replacing complex nested if statements. Also, the switch statements do not fit well when there are complex conditions. the String value to the Operator by using the Operator#valueOf() method: ? nested if statement in java - It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement.

Comments
  • If you use an array or a collection to store the data instead of 150 variables, then yes, it is way easier :).
  • You have to wrap your data structure.. Having 150 variables in your code is an indication of coming disaster, I don't want to live to witness the results..
  • That just means that the b condition isn't the one that made the statement false. Perhaps you want "or" instead of "and"?
  • As for your error, are you saying that when one of your variables is not 0, the else statement runs? That is because you use &&. ALL of them must be true for it to pass. If you want it to pass when only one is true, then use ||.
  • I see cobertura complaining about branch coverage...
  • If only this was a school project :)- I'm doing this in my own time out of my own interest. However, you're solution is interesting
  • kudos to you. make sure you understand arrays!
  • Your logic is backwards (given your variable name). and, if someone is testing for not allZero it's best to leave the loop when that condition is reached (why look for the keys after you've found them)
  • next suggestion, don't use negative logic - you'll thank me later (boolean isAllZero = true; set to false when an integer[x] isn't zero then break from loop - still no need to keep processing when you've found the exception)
  • -1 from my side---Ahh,here comes the man to implement K-map for 150 variables... Have you even done that for more than 6 variables???
  • just 22500 fields to fill out ^_^
  • Maybe 150 variables would not be needed if K-map was used. It was a suggestion based upon vague problem description.
  • @JeffMiller-So,you wrote a new research paper for implementing K-map for function of 150 variables into something less,HUH??? Seems more tough than solving NP vs P problem!