class A
{
public static void main(String args[])
{
int z=5;
if(++z>5||++z>6)
{
z++;
}
System.out.println(z);
}
}

- 4,991
- 7
- 36
- 56

- 29
- 2
-
1Because if condition1 is true then `consition1 || condition2` will always be true. – sarveshseri Jan 30 '15 at 07:03
-
What is your question? – user253751 Jan 30 '15 at 07:07
4 Answers
In OR
logical operator,both conditions don't have to be true
.If the first condition is true it will execute the statement
without checking the second condition.If you want to check both conditions,use AND
logic

- 62
- 8
-
-
-
people asking questions because they don't know the answer ,"immibis ".You may be a pro.but there are student that still studying.so help them to learn. – Corrupted_S.K Jan 30 '15 at 07:15
-
You are trying to use the short-circuit operator in this case it is OR
In which, once first condition is is true it whole experssion gets evaulated to true and hence no need to run second condition
This is due to Short-Circuit Logical Operators
- The OR operator results in true when one operand is true, no matter what the second operand is.
The AND operator results in false when one operand is false, no matter what the second operand is.
If you use the || and &&, Java will not evaluate the right-hand operand when the outcome can be determined by the left operand alone.
Since in your case as 1st condition evaluates to be true So 2nd condition will not executed
class A
{
public static void main(String args[])
{
int z=5;
if(++z>5||++z>6)//Condition 1 is true so the final result is true
{
z++;
}
System.out.println(z);
}
}

- 7,643
- 6
- 34
- 62
This will fix you problem
class A {
public static void main(String args[]) {
int z=5;
if(++z>5 && ++z>6) { //Short-Circuit AND
z++;
}
System.out.println(z);
}
}

- 62
- 8