// 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"
}
for(int i=0; i<10; i++){ //creates a counting vatiable i set to 0
//as long as i is < 10 (as long the condition is true)
// i is increased by one every cycle
//do some stuff
}
// 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);
}
}
}
for (int i = 0; i < 5; i++) {
System.out.println(i);
// 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"
}
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 name_of_java_app {
public static void main(String[] args) {
int value = 4;
string whatever_value = "whatever_val";
// The value can be whatever you want.
for(int name_of_int; name_of_int < value; name_of_int++) {
System.out.println(whatever_value + name_of_int);
}
// The output will be 0 1 2 3 4 whatever_val
// You can put any data value such as char or short but not boolean
}
}
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");
}
}