- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java for Loop
Beginning with the JDK 5, there are two forms of the for loop. The first is the traditional form that has been in use since the original version of Java. And the second is the newer "for-each" (you will learn about this loop in detail in other chapter) form.
We will discuss both types of the for loop one by one, i.e., one in this chapter and the newer one in the next chapter.
Java for Loop Syntax
Following is the general form of the traditional for statement :
for(initialization; condition; iteration) { // body of the loop }
If only one statement is being repeated, then there is no need for the curly braces. But you can use curly braces (since it makes easier to add some other statements later).
Java for Loop Working
The working of for loop is explained here:
When the loop starts, then first, the initialization portion of the loop is executed (only once). In general, this is an expression which sets
the value of the loop control variable, which acts as a counter which controls the loop.
It is important to understand that, as already told, the initialization expression is executed first but only once. Next, the condition is evaluated.
This (the condition expression) must be a Boolean expression. It generally tests the loop control variable against a target value. If this expression
becomes true, then the body of the loop is executed. If it is false, then the loop terminates. And next, the iteration portion of the loop is
executed. This is generally an expression that increments or decrements the variable which controls the loop. The loop then iterates, first
evaluating the conditional expression, then executing the loop's body, and then executing the iteration expression with each and every pass.
This process repeats until the controlling expression becomes false.
Java for Loop Example
Following is a version of the "tick" program that uses a for loop :
/* Java Program Example - Java for Loop * This program demonstrate the for loop */ public class JavaProgram { public static void main(String args[]) { int i; for(i=10; i>0; i--) { System.out.println("tick " + i); } } }
When the above Java program is compile and executed, it will produce the following output:
Let's look at one more example, also demonstrates the for loop:
/* Java Program Example - Java for Loops * This program demonstrates for loop */ public class JavaProgram { public static void main(String args[]) { int i; for(i=1; i<=15; i++) { System.out.println(i + " "); } System.out.println("Fire....!!!"); } }
The sample run is shown here :
More Examples
Here are some list of example programs that uses for loop :
- Check Prime or Not
- Print Pascal Triangle
- One Dimensional Array Program
- Linear Search
- Binary Search
- Check Anagram or Not
- Generate Random Numbers
- Write to File
« Previous Tutorial Next Tutorial »