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

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.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...