1for(int i = 0; i < 10; i++)
2{
3 //do something
4
5 //NOTE: the integer name does not need to be i, and the loop
6 //doesn't need to start at 0. In addition, the 10 can be replaced
7 //with any value. In this case, the loop will run 10 times.
8 //Finally, the incrementation of i can be any value, in this case,
9 //i increments by one. To increment by 2, for example, you would
10 //use "i += 2", or "i = i+2"
11}
1int values[] = {1,2,3,4};
2for(int i = 0; i < values.length; i++)
3{
4 System.out.println(values[i]);
5}
1class for_loop
2{
3 public static void main(String args[])
4 {
5 for(int i=0;i<10;i++)
6 {
7 System.out.print(" " + i);
8 }
9 /*
10 Output: 0 1 2 3 4 5 6 7 8 9
11 */
12 }
13}
1// Example of a for loop in java that will iterate the number of times entered by the user
2import java.util.Scanner; // import Scanner class
3public class grepperjava {
4 public static void main(String[] ARGS)
5 {
6 System.out.println("\nPlease enter a number, and the loop will iterate input times.");
7 int input;
8 Scanner var = new Scanner(System.in); // Create scanner object, so user-input may be read
9 input = var.nextInt();
10 var.close();
11
12 for(int i = 0; i < input; i++)
13 {
14 System.out.println("This will print " + input + " times");
15 }
16 }
17}