|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-24-2008, 10:16 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 2
|
|
|
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);
}
}
|
|

04-24-2008, 10:32 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
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
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
|
|

04-24-2008, 10:38 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 2
|
|
|
This is not what i am asking. See the question again.
|
|

04-24-2008, 10:40 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-24-2008, 10:42 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
ah, you have to read more about post-prefix incrementing pal.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-24-2008, 10:43 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
|
|
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)
__________________
sanjeev,संजीव
|
|

04-24-2008, 10:44 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-24-2008, 10:45 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 386
|
|
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
|
|

04-24-2008, 12:09 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
Additional detail ( hope this would add )
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......
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-24-2008, 12:21 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
|
|
|
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
__________________
sanjeev,संजीव
|
|

04-24-2008, 12:23 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
If we use (&) in if statement:
Then if left condition is false .... compiler still check for right part
Absolutely.....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-25-2008, 05:58 AM
|
|
Member
|
|
Join Date: Nov 2007
Posts: 20
|
|
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.
|
|

04-25-2008, 06:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|