• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

increment operators

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anybody explain me this code.....

The answer is 1,3,2

I am not able to figure out how ???

Thanks

( tags added and hopefully reformatted more clearly)
[ January 22, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try tracing through the code.


The above code gives a similar sample - for "continue" the loop counter "j" gets incremented before the next iteration.

Hope that makes sense.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The beginning of your for loop displays like a Graemlin. Please space out the symbols before h++. The second part of a for statement should be an expression, not a block.
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anybody explain me my doubt in a simple way
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets start from the beginning, step by step:

1- the code enters label1 and h is incremented. Now its value is 1;
2- the code enters label2 and i is incremented to 1;
3- k = h + i + j = 2. The statement is executed inside the switch block, which causes the code to exit the switch block;
4- The statement is executed, incrementing j to 1;
5- the do-while loop executes again, incrementing i again to 2;
6- k = h + i + j = 1 + 2 + 1 = 4. The statement is executed inside the switch block. This means that the do-while loop will continue. But before it moves to the next iteration, the while(++j < 5) is executed again to check whether the loop should be executed again or not. This causes j to be incremented to 2;
7- the do-while loop executes again, incrementing i to 3;
8- k = h + i + j = 1 + 3 + 2 = 6. The will execute and cause the end of the for loop. The code moves to the System.out.println statement printing the values 1, 3 and 2 for h, i and j.

When you face code samples like this, try writing down the variables and its respective values to help you keep track of what is going on.

Hope this helps,
Stefan
[ January 22, 2005: Message edited by: Stefan Guilhen ]
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stefan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!