0% found this document useful (0 votes)
5 views

The For Statement (The Java™ Tutorials - Learning The Java Language - Language Basics)

Uploaded by

justininokiumu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

The For Statement (The Java™ Tutorials - Learning The Java Language - Language Basics)

Uploaded by

justininokiumu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Documentation

Search
The Java™ Tutorials Hide TOC

Language Basics
Variables « Previous • Trail • Next » Home Page > Learning the Java Language > Language Basics
Primitive Data Types
Arrays
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no
Summary of Variables
longer available.
Questions and Exercises
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
Operators
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
Assignment, Arithmetic,
and Unary Operators
Equality, Relational, and The for Statement
Conditional Operators
Bitwise and Bit Shift The for statement provides a compact way to iterate over a range of values. Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular
Operators condition is satisfied. The general form of the for statement can be expressed as follows:
Summary of Operators
for (initialization; termination;
Questions and Exercises
increment) {
Expressions, Statements,
statement(s)
and Blocks
}
Questions and Exercises
Control Flow Statements When using this version of the for statement, keep in mind that:
The if-then and if-then-
else Statements The initialization expression initializes the loop; it's executed once, as the loop begins.
The switch Statement When the termination expression evaluates to false, the loop terminates.
The while and do-while The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
Statements
The following program, ForDemo, uses the general form of the for statement to print the numbers 1 through 10 to standard output:
The for Statement
Branching Statements class ForDemo {
Summary of Control public static void main(String[] args){
Flow Statements for(int i=1; i<11; i++){
Questions and Exercises System.out.println("Count is: " + i);
}
}
}

The output of this program is:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

Notice how the code declares a variable within the initialization expression. The scope of this variable extends from its declaration to the end of the block governed by the for statement, so it
can be used in the termination and increment expressions as well. If the variable that controls a for statement is not needed outside of the loop, it's best to declare the variable in the
initialization expression. The names i, j, and k are often used to control for loops; declaring them within the initialization expression limits their life span and reduces errors.

The three expressions of the for loop are optional; an infinite loop can be created as follows:

// infinite loop
for ( ; ; ) {

// your code goes here


}

The for statement also has another form designed for iteration through Collections and arrays This form is sometimes referred to as the enhanced for statement, and can be used to make your
loops more compact and easy to read. To demonstrate, consider the following array, which holds the numbers 1 through 10:

int[] numbers = {1,2,3,4,5,6,7,8,9,10};

The following program, EnhancedForDemo, uses the enhanced for to loop through the array:

class EnhancedForDemo {
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}
}
}

In this example, the variable item holds the current value from the numbers array. The output from this program is the same as before:

Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10

We recommend using this form of the for statement instead of the general form whenever possible.

« Previous • Trail • Next »

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.
Préférences en matière de cookies | Ad Choices

You might also like