Why is the condition of j never satisfied with "||", and why is i satisfied with "&&"?

2
package aulas;
   public class Aula{
     public static void main(String[] args){
       for(int i = 0, j = 0; i < 100 || j < 50; ++i);
          //Quando troco o operador lógico para && é impresso até 99, porém
          //quando uso || é impresso até o infinito(ou estouro da variável)
  }
}
    
asked by anonymous 20.08.2017 / 03:54

2 answers

6

The question is as follows, you are incrementing only the value of the i variable. This way it will vary from 0 to 99 .

These are the true tables for the logical connectives:

E (& &)

A | B | A && B
--+---+-------
V | V | V
V | F | F
F | V | F
F | F | F

OU (||)

A | B | A || B
--+---+-------
V | V | V
V | F | V
F | V | V
F | F | F

Thus, as can be seen in the OU (||) operator, both conditions must be false for the loop to stop. Because the value of the j variable is never incremented. The loop is infinite causing the overflow.

In the E (&&) operator, only one of the evaluations must be false to make the Boolean expression false, and thus break the loop, a fact that will occur only when the value of i is 100.

    
20.08.2017 / 04:04
1

Because the value of j is static.

You only have i++ so j will always meet the proposed condition

    
20.08.2017 / 04:02