• 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

Need Explanation

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,

I am new to java and new to this forum as well. I am desperate to learn java and due to some advises I bought the book "Head first java". In this book there are some exercises that I don't really understand. One of which is code snipts. I was give an output and all the code lines but they were not in the proper way. So my job was to arrange all the lines of code so that they the program gives the given output. I write arranged the code myself and got the same output as required by the exercise, but the solution given at the end of the chapter is different. I wonder about this difference and would love to have explanation if anyone can help. Both the codes are as under:

=================================================================================================================

Code in the book
--------------------




OUTPUT

a-b c-d

===========================================

My Code
-----------



=======================================================

This program gives the same output : a _ b c _ d

but I need some explanation that how it works in this kind of loops especially in the book code when printing the first letter "a " and closing curly bracket the line x = x - 1; is really confusing me . Please help me to understand the whole process of branching in this example.

Thanks in advance.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

You will have to go through your code and the book's with a pencil and paper, and at each line write down the value for x, and what the output is.

You find that, more by luck than good management, you can have two apparently different programs which actually give the same output.
I have edited your code to tidy it up and the code button makes it look a lot better.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several solutions to achieve a same goal, I think it is a lesson we learn from the book.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic