• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

post and pre increment operators

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got a doubt in the following code

int z = 5;
if(++z > 5 || ++z > 6) z++; // z = 7 after this code


how does the output comes out to be z=7
in the if condition z becomes 6
but as z++ is post increment it should print(return) 6 and get incremented to 7..
so the output should be 6...??
it must be a silly doubt..but please help me..
thanks in advance..
 
Ranch Hand
Posts: 67
Eclipse IDE Fedora Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you see || is a short circuit operator.
So it first it test first condition and if it found it true it didn't execute next one which is the case here.
In ++z>5 ,z first incremented and then compared so it becomes
6>5 which is true and the controls comes to z++ which increment z to 7 and return it.
If you print z like this System.out.print(z) //z=7
If you print z like this System.out.print(z++) //z=6
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roshan Rai wrote:i got a doubt in the following code

int z = 5;
if(++z > 5 || ++z > 6) z++; // z = 7 after this code


how does the output comes out to be z=7
in the if condition z becomes 6
but as z++ is post increment it should print(return) 6 and get incremented to 7..
so the output should be 6...??
it must be a silly doubt..but please help me..
thanks in advance..



Where in the code is this print() that you are referring to?

Henry
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roshan,

if after the following code you use the value of z then it will be definitely 7 because after Line 1 z is incremented to 7.



But if you place return statement at Line 1 like this:

then the value of z returned will be 6 because the post increment operator will increment it after using it in the return statement.

Hope that helps.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic