AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL
HTML Tutorial

Java For Loop


Java Tutorial » Java For Loop

for loop is used to run a block of code for a certain number of times.
For loop is used when you know the exact number of code repetitions.

Syntax:
for (initialExpression; testExpression; updateExpression) {
// code block to be executed
}

initialExpression is executed (one time) before the execution of the code block.
testExpression defines the condition for executing the code block.
updateExpression is executed (every time) after the code block has been executed.


Example: will print the numbers 1 to 5
public class Main {
public static void main(String[] args) {
for (int x = 1; x < 6; x++) {
System.out.println(x);
}
}
}
1
2
3
4
5


Java For-Each Loop, example


"for-each" loop, is used exclusively to loop through elements in an array:

Syntax: Do / While Loop.
for(Type varName : arrayname){
//code or block to be executed
}

example outputs all elements in the color array, using "for-each" loop:

Example: for-each Loop.
public class Main {
public static void main(String[] args) {
String[] colors = {"red", "blue", "black", "white"};
for (String i : colors) {
System.out.println(i);
}
}
}
Output:
red
blue
black
white


Java Nested For Loop

If we have "for" loop inside to another loop, it is known as nested for loop.

Example: Nested For Loop Pyramid 1.
public class Main {
public static void main(String[] args) {
for(int x=1; x<=5; x++){
for(int y=1; y<=i; y++){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *


Nested For Loop Pyramid 2.

Example: Nested For Loop Pyramid 2.
public class Main {
public static void main(String[] args) {
int term=6;
for(int x=1;x<=term;x++){
for(int y=term;y>=x;y--){
System.out.print("* ");
}
System.out.println();//new line
}
}
}
Output:
* * * * * *
* * * * *
* * * *
* * *
* *
*




Java For Loop array, arraylist, example, break, list, exit, types, variants, iterator, for string
Java For Loop - java

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


HTML Image tag, code...
How to code in HTML...
HTML text tag...
HTML text, how to display...
Container responsive styles...
HTML container tag...
HTML container...
Send forgot password by mail or message ...
PHP MySQL simple login system...
Check for duplicate username live or ema...
How to create simple search engine using...
Diseño de sitio web CSS Layout...
Diseño de caja flexible CSS3...
Botones en CSS...
ID y Clase, Diferencias entre Class e Id...
Esquina redondeada CSS...
Tamaño de caja css box-sizing: border-b...
Alineación CSS...
Altura y Anchura CSS...
Bordes CSS...