Product developed to Web
Medical DiagMedical
Video Bitrate Calculator
Yaarq - Java3D Unleashed
GetId
Java Loops (while, do-while and for loops)
Diposting oleh Source Code Program | 02.31 | Java | 0 komentar »For loops typically iterate a fixed number of times and then exit. While loops iterate continuously until a particular condition is met. You usually do not know in advance how many times a while loop will loop.
In this case we want to write a loop that will print each of the command line arguments in succession, starting with the first one. We don't know in advance how many arguments there will be, but we can easily find this out before the loop starts using the args.length. Therefore we will write this with a for loop. Here's the code:
Source Code
// This is the Hello program in Java
class Hello {
public static void main (String args[]) {
int i;
/* Now let's say hello */
System.out.print("Hello ");
for (i=0; i < args.length; i = i++) {
System.out.print(args[i]);
System.out.print(" ");
}
System.out.println();
}
} We begin the code by declaring our variables. In this case we have exactly one variable, the integer i. i Then we begin the program by saying "Hello" just like before.
Next comes the for loop. The loop begins by initializing the counter variable i to be zero. This happens exactly once at the beginning of the loop. Programming tradition that dates back to Fortran insists that loop indices be named i, j, k, l, m and n in that order.
Next is the test condition. In this case we test that i is less than the number of arguments. When i becomes equal to the number of arguments, (args.length) we exit the loop and go to the first statement after the loop's closing brace. You might think that we should test for i being less than or equal to the number of arguments; but remember that we began counting at zero, not one.
Finally we have the increment step, i++ (i=i+1). This is executed at the end of each iteration of the loop. Without this we'd continue to loop forever since i would always be less than args.length.
The Java programming language supports three kinds of comments:
/* text */
The compiler ignores everything from /* to */.
/** documentation */
This indicates a documentation comment (doc comment, for short). The compiler ignores this kind of comment, just like it ignores comments that use /* and */. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
// text
The compiler ignores everything from // to the end of the line.
Example
Java denotes comments in three ways:
1. Double slashes in front of a single line comment:
int i=5; // Set the integer to 5
2. Matching slash-asterisk (/*) and asterisk-slash (*/) to bracket multi-line comments:
/*
Set the integer to 5
*/
int i=5;
3. Matching slash-double asterisk (/**) & asterisk-slash(*/) for Javadoc automatic hypertext documentation, as in
/**
This applet tests graphics.
*/
public class testApplet extends applet{...
or
/**
* Asterisks inside the comment are ignored by javadoc so they
* can be used to make nice line markers.
**/
The SDK tool javadoc uses the latter /** ..*/ comment style when it produces hypertext pages to describe a class.
A java program is defined by a public class that takes the form:
public class program-name {
optional variable declarations and methods
public static void main(String[] args) {
statements
}
optional variable declarations and methods
}Source Code
In your favorite editor, create a file called HelloWorld.java with the following contents:
/** Comment
* Displays "Hello World!" to the standard output.
*/
class HelloWorld {
public static void main (String args[]) {
System.out.println("Hello World!"); //Displays the enclosed String on the Screen Console
}
}
To compile Java code, we need to use the 'javac' tool. From a command line, the command to compile this program is:
javac HelloWorld.java
For this to work, the javac must be in your shell's path or you must explicitly specify the path to the program (such as c:\j2se\bin\javac HelloWork.java). If the compilation is successful, javac will quietly end and return you to a command prompt. If you look in the directory, there will now be a HelloWorld.class file. This file is the compiled version of your program. Once your program is in this form, its ready to run. Check to see that a class file has been created. If not, or you receive an error message, check for typographical errors in your source code.
You're ready to run your first Java application. To run the program, you just run it with the java command:
java HelloWorld
Sample Run
Hello world!
HelloWorld.java
Note: It is important to note that you use the full name with extension when compiling (javac HelloWorld.java) but only the class name when running (java HelloWorld).