Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Java for Loop

A loop is a block of statements that are repeated. A loop may be repeated a fixed number of times, repeat until some condition is met, or can loop indefinitely while the program is running. There are three main loop control structures available in Java: for, do and while. The most basic loop structure is the for loop. Shown below is an example of a for loop.

String outData = "";

for(int i = 1; i <= 200; i++)
{
   int multiple = 9 * i;
   String strI = String.valueOf(multiple);
   outData += strI + " ";
}

In the above code, the for statement declares an integer variable i, and then tests i to see if it's less-than or equal-to 200 (i <= 200). Then it increases the value of i by 1 (increments i). If the test part of the statement where to resolve to true, program flow would exit the loop.

In the for loop block of statements, each time the loop executes the value of the integer variable multiple is multiplied by 9. In the next line, the String object method valueOf changes the variable type from integer to string and stores it in the String object strI. In the next line the value of strI is concatenated onto the string outData.

Java for loop example output

So, the resulting function of this for loop example is to create a string containing the first 200 multiples of 9, from 9 to 1,800 (9 x 200). Shown below is the complete code for an application that displays these multiples in a program window.

import javax.swing.*;

public class forLoop 
{
   public static void main(String s[])
   {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      frame.setTitle("Java for Loop Test");


      String outData = "";

      for(int i = 1; i <= 200; i++)
      {
         int multiple = 9 * i;
         String strI = String.valueOf(multiple);
         outData += strI + " ";
      }

      JTextArea myText = new JTextArea();
      myText.setLineWrap(true);
      myText.append(outData);
      frame.add(myText);

      frame.setVisible(true);
   }
}

In the code above, a JTextArea object named myTextis created, and the line myText.append(outData) places the outData string in the JTextArea for display in the program window.

Note: I generally hate it when people add a bunch of extra code to an example. It confuses things. But in this case I've included the javax.swing library and added code to create a JFrame and a JTextArea. If I didn't add this extra code, you would need to use the System.out.print command to output text, and I don't think that's an interesting or realistic way to learn Java programming.

If you're a beginner, for this example, I recommend downloading and installing the Java Development Kit from Oracle's web site. Type the code into a text file using a basic text editor like Window's Notepad (Do not use a word processor because they insert formatting codes into the file), saving the file with the .java file extension. Then use a batch file to compile the program. The batch file needs to contain the path to the compiler and the path to the source file, an example of compile.bat is shown below.

c:\Program Files\java\jdk1.8.0_121\bin\javac c:\Users\Stephen\Desktop\program_name.java
pause

The pause command on the second line will cause the DOS shell to stay open until you press any key, allowing you to read any errors. If successful, the compiler will create an executable file with the .class file extension. Then use a batch file to run the program. The batch file needs to contain the start command, the name of the java program launcher, and the name of the compiled java class file (without the .class extension). An example of run.bat is shown below.

start javaw program_name

The javaw launcher displays the result of your program, or error information if it fails, in a window.

RSS Feed RSS Feed


Follow Stephen Bucaro Follow @Stephen Bucaro


Java

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2019 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268