// Starting on 0:
for(int i = 0; i < 5; i++) {
System.out.println(i + 1);
}
// Starting on 1:
for(int i = 1; i <= 5; i++) {
System.out.println(i);
}
// Both for loops will iterate 5 times,
// printing the numbers 1 - 5.
for(int i = 0; i < 10; i++)
{
//do something
//NOTE: the integer name does not need to be i, and the loop
//doesn't need to start at 0. In addition, the 10 can be replaced
//with any value. In this case, the loop will run 10 times.
//Finally, the incrementation of i can be any value, in this case,
//i increments by one. To increment by 2, for example, you would
//use "i += 2", or "i = i+2"
}
// Java program to illustrate
// for-each loop
class For_Each
{
public static void main(String[] arg)
{
{
int[] marks = { 125, 132, 95, 116, 110 };
int highest_marks = maximum(marks);
System.out.println("The highest score is " + highest_marks);
}
}
public static int maximum(int[] numbers)
{
int maxSoFar = numbers[0];
// for each loop
for (int num : numbers)
{
if (num > maxSoFar)
{
maxSoFar = num;
}
}
return maxSoFar;
}
}
//For Loop in Java
public class ForLoops
{
public static void main(String[] args)
{
for(int x = 1; x <= 100; x++)
{
//Each execution of the loop prints the current value of x
System.out.println(x);
}
}
}
class Main {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
// iterating through an array using a for loop
for (int i = 0; i < vowels.length; ++ i) {
System.out.println(vowels[i]);
}
}
}
// Java program to illustrate for loop
class forLoopDemo {
public static void main(String args[])
{
// Writing a for loop
// to print Hello World 5 times
for (int i = 1; i <= 5; i++)
System.out.println("Hello World");
}
}
public class ForLoop {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
// Will do this 5 times.
System.out.println("Iteration #: " + i);
}
}
}
package com.company;
public class Main {
public static void main (String[] args) {
System.out.println("loop started");
for (int i = 0; i < 10; i++) {
if (i==5){
continue;
}
System.out.println(i);
}
System.out.println("loop over");
}
}