Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-24-2008, 10:16 AM
Member
 
Join Date: Apr 2008
Posts: 2
mehrotra.chitij is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-24-2008, 10:32 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Quote:
Originally Posted by mehrotra.chitij View Post
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

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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-24-2008, 10:38 AM
Member
 
Join Date: Apr 2008
Posts: 2
mehrotra.chitij is on a distinguished road
This is not what i am asking. See the question again.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-24-2008, 10:40 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-24-2008, 10:42 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-24-2008, 10:43 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by mehrotra.chitij View Post
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,संजीव
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-24-2008, 10:44 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-24-2008, 10:45 AM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 386
Zosden is on a distinguished road
Quote:
Originally Posted by mehrotra.chitij View Post
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-24-2008, 12:09 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
if(i > 0 && ++i < 10)

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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-24-2008, 12:21 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
sanjeevtarar is on a distinguished road
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,संजीव
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 04-24-2008, 12:23 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 04-25-2008, 05:58 AM
Member
 
Join Date: Nov 2007
Posts: 20
SCS17 is on a distinguished road
Quote:
Originally Posted by sukatoa View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 04-25-2008, 06:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing an XML output JThangiah XML 2 03-27-2008 06:15 PM
Output to excel abhiN New To Java 2 03-07-2008 03:19 AM
output Camden New To Java 3 12-02-2007 12:34 AM
How to redirect the output JavaBean Java Tips 0 10-04-2007 11:30 PM
get the output from whoami gary Advanced Java 2 06-12-2007 03:05 PM


All times are GMT +3. The time now is 08:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org