Java - For Statement

Java Conceptuel Diagram

About

The for statement is a part of the control flow possibility of Java.

The Iterable interface must be implemented in order to use it. See for example: Java - Iterator Implementation

Property

Index

During a for loop, this is no shortcut syntax to get the index. You need to create it

int index = 0;
for (String s: strings){
  System.out.println("The current index is "+index);
  assert s == strings.get(index): "Error";
  // Increment
  index++
}

Syntax

General

for (initialization; termination; increment) {
    statement(s)
}

Enhanced

for (datatype variable : collections or array) {
    statement(s)
}

Infinite

for ( ; ; ) {
    
    // your code goes here
}

Skip

The continue statement skips the current iteration of a for loop.

Example

Counter

for(int i=1; i<11; i++){
    System.out.println("Count is: " + i);
}

Array

Java - Array

int[] anArray= {1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < anArray.length; i++) {
	System.out.println(i+" : "+anArray[i]);
}

Documentation / Reference





Discover More
Java Conceptuel Diagram
Java - Array

in Java. Arrays implement most of the List operations, but not remove and add. They are “fixed-size” Lists. An array is a container object that holds : a fixed number of values of a single...
Java Conceptuel Diagram
Java - Iterator Implementation

The iterator implementation in Java is just an inner class that implements the iterator interface. In order to be able to use it in a for loop construction, the iterable interface must be implemented....
Java Conceptuel Diagram
Java - Loop

in java The continue statement skips the current iteration of a for, while , or do-while loop. The break statement has two forms: unlabeled. and labeled An unlabeled break will...



Share this page:
Follow us:
Task Runner