x= x - f(x)/df(x);could be written in the file just as easily
x = x - f(x) /df(x);Of course, the first version is easier to read, but this illistrates the freedom you have to format your code in a way that is convenient for you. Unlike the siliness in FORTRAN, there are no restrictions to putting characters in the first 5 columns. With the freedom to format your code as you like comes the burden of making it readable to other people. You should look to see how other people format their code, find a style that you like, and be consistant in your style.
You can also group lines of Java code together using
curly braces, {
and }
to delimit a section.
You will see this, for example, in loops where the code executed during
each loop is grouped together with curly braces. In our
Newton's method code, the main loop calculates our new guess for
x
and then prints out some information for this iteration.
The body of the loop looks like:
{ x= x - f(x)/df(x); System.out.println("Step: " + count + " x:" + x + " Value:" + f(x)); }Notice that there is no need for a semi-colon after the closing brace.
![]() |
5.5 Introduction to Classes |