// 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.
// 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]);
}
}
}
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);
}
}
}