# Thread: Why the output is always zero

1. Member
Join Date
Apr 2008
Posts
2
Rep Power
0

## Why the output is always zero

why in the below example the value of i is not incrementing. Output is always zero o.
The ++ operator have higher precedence. So this should first increment the value of i and then use is the rule.

class Demo
{
public static void main(String args[])
{
int i = 0;
if(i > 0 && ++i < 10)
System.out.println("Inside if" );
System.out.println(i);
}
}

2. Originally Posted by mehrotra.chitij
why in the below example the value of i is not incrementing. Output is always zero o.
The ++ operator have higher precedence. So this should first increment the value of i and then use is the rule.

class Demo
{
public static void main(String args[])
{
int i = 0;
if(i > 0 && ++i < 10)
System.out.println("Inside if" );
System.out.println(i);
}
}
it should be

Java Code:
```class Demo
{
public static void main(String args[])
{
for(int i = 0; i < 10; i++)
{
System.out.println(i);
}
}
}```
output:
0
1
2
3
4
5
6
7
8
9

3. Member
Join Date
Apr 2008
Posts
2
Rep Power
0
This is not what i am asking. See the question again.

4. Hi mehrotra.chitij,

Welcome to our community. :)

You just check a condition, and do some processing. Doesn't loop your process there.

What actually you want to do, is that Zosden says?

5. Originally Posted by mehrotra.chitij
The ++ operator have higher precedence. So this should first increment the value of i and then use is the rule.

if(i > 0 && ++i < 10)

Hello Pal,

In if condition : you used logical AND operator and it checks like that..

If left condition returns false it never execute right condition
since your right condition is never executed (i is not greater than 0 if you put i >= 0 then it will be executed) and that's why it always prints ZERO.

If you use bitwise AND (&)......It always executes both left and right.

Use it and see the difference.......

if(i > 0 & ++i < 10)

6. And you are make the condition in wrong way pal. There you don't have use {} for if clause. In such a case only the first line enclosed with if clause.

7. Originally Posted by mehrotra.chitij
class Demo
{
public static void main(String args[])
{
int i = 0;
if(i > 0 && ++i < 10)
System.out.println("Inside if" );
System.out.println(i);
}
}
What this is doing line by line

int i = 0; // is setting the variable i equal to 0;

if(i > 0 && ++i < 10) // is checking if i > 0 and i + 1 < 10

System.out.println("Inside if" ); // if the above line is true then it prints out "inside if", which it is not because i is not greater than zero it is zero.

System.out.println(i); // prints out i which is zero.

this is not a loop but just a check if you give me more details on what you want this to do then be more specific

8. if(i > 0 && ++i < 10)

the "()" of a if-statement should be true in order to proceed on its subtask....else, dont....

In logic, here is the truth table...

Let say, 0 is false and 1 is true.

@ AND table
A B Output
0 0 0
0 1 0
1 0 0
1 1 1

@ OR table,
A B Output
0 0 0
0 1 1
1 0 1
1 1 1

Now, since i = 0;
(i > 0) is false, ( ++i < 10 ) is true... bec, @ that moment, i is 1 when compare to 10 and it is always true.....unless it will be looped for about 9 times....

so now, the if statement is like,

if ( 0 AND 1 )... by looking at the table, 0 and 1 is 0 means false....

It will not proceed on its subtask(just my term)....
Since no statements are inclosed under the if statement, the JVM assumes that the first statement that will be encountered should be part of the if-statement's subtask......

9. There are tow types of AND operator in Java.

bitwise AND (&)
logical AND (&&)

Both use the truth table given by sukatoa. but here the difference between is

If we use (&&) in if statement:
Then if left condition is false ....compiler will not check for right part

If we use (&) in if statement:
Then if left condition is false .... compiler still check for right part

10. If we use (&) in if statement:
Then if left condition is false .... compiler still check for right part
Absolutely.....

11. Member
Join Date
Nov 2007
Posts
20
Rep Power
0
Originally Posted by sukatoa
Absolutely.....
I see what you guys mean here.. I studied those things in Discrete Math some time ago but in the case of the bit-wise &.. whats the use of checking the right side if the left side is false..???
The statement will evaluate to false either ways.

Thanks.

12. Logically what you say is right. If the right side is false checking the left side is useless, because result is false whatsoever.

Ok, say right side is true. Defiantly you have to check the left side. Because the result depends on that.

In compiler generating such a logic is wasting resources. I mean it deal with all the scenarios meet in the processing.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•