This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strange IF condition

 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

class Floozit{
public static void main(String[] args)
{
Integer x = 0;
Integer y = 0;
for(Short z= 0;z<5;z++)
if((++x>2)>||(++y)>2)
x++;
System.out.println(x+" "+y);


}
}

The possible output are all 3-4 digits in length though!
At the very least I would think that it prints:
1 1 2 2 4 3 5 4
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for(Short z= 0;z<5;z++)
if((++x>2)>||(++y)>2)
x++;

for loop boundary is upto x++, not after that.

so output will be 8 2. why?
first x=0, y=0

1. z=0,
++x(x=1) > 2 false || ++y(y=1) false
x++ not executed here as both are false.

2. z=1
++x(x=2) > 2 false || ++y(y=2) false
x++ not executed here as both are false.

3. z=2
++x(x=3) > 2 true here y++ will not executed as || is short circuit operator
x++ executed so (x=4)

3. z=3
++x(x=5) > 2 true here y++ will not executed as || is short circuit operator
x++ executed so (x=6)

4. z=4
++x(x=7) > 2 true here y++ will not executed as || is short circuit operator
x++ executed so (x=8)


and finally
System.out.println(x+" "+y);// 8 2
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the for loop included the System.out.println()!
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no no

for(Short z= 0;z<5;z++)
if((++x>2)>||(++y)>2)
x++;

for loop is not using {} braces, so it is just including if() conditional statement, and this if() statement is including x++ statement as if(){} is also not using {} so it can contain only one statement after it.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi:

Originally posted by Duran Harris:
if((++x>2)>||(++y)>2)



In the (++x>2)> expression, is the last greater-than symbol correct? Cause, if it is in your code, it won't compile, if it is not in your code, the output is as said by Punit.

Regards,
[ January 02, 2009: Message edited by: Alejandro Galvan ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya it is just typing mistake.
 
reply
    Bookmark Topic Watch Topic
  • New Topic