• 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

may be this is very basic question...........

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class TeSet {
public static void main(String args[]) {
int m = 2;
int p = 1;
int t = 0;
for(;p < 5;p++) {
if(t++ > m) {
m = p + t;
}
}
System.out.println("t equals " + t);
}
}
for this output is t equals 4.
Can any body explain how it is?
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Raji Addepalli:
public class TeSet {
public static void main(String args[]) {
int m = 2;
int p = 1;
int t = 0;
for(;p < 5;p++) {
if(t++ > m) {
m = p + t;
}
}
System.out.println("t equals " + t);
}
}
for this output is t equals 4.
Can any body explain how it is?


Raji,
This Qs put some variable statement here & there to blur your sight, actually if you look more closely, you will see, we are asked to predict the value of t.
And inside the code, the only place t will get updated is "t++" in if (t++>m), since this if statement is part a for loop that will repaet 4 times. p=1,2,3,4.
t++ encounted 4 times, so t =4.
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all you are concerned with is t simplify the problem and just put in some System.out.prints to see what is going on:


You'll see the following output:
 
Raji Addepalli
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much
 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what will be the initialization value for p ?
for(;p < 5;p++)
is the initialization value optional ?
there are three things needed for for( )

initialization
condition
increment
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mark,
initialization is not mandatory in a for-loop
provided it has been done already, which is the case here with the statement
int p=1;
above the for-loop...
HIH
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic