For Loop

For loop executes group of Java statements as long as the boolean condition evaluates to true.

For loop combines three elements which we generally use: initialization statement, boolean expression and increment or decrement statement.

For loop syntax

  1. for( <initialization> ; <condition> ; <statement> ){
  2.  
  3. <Block of statements>;
  4.  
  5. }

The initialization statement is executed before the loop starts. It is generally used to initialize the loop variable.

Condition statement is evaluated before each time the block of statements are executed. Block of statements are executed only if the boolean condition evaluates to true.

Statement is executed after the loop body is done. Generally it is being used to increment or decrement the loop variable.

Following example shows use of simple for loop.

  1. for(int i=0 ; i < 5 ; i++)
  2. {
  3. System.out.println(“i is :+ i);
  4. }

It is possible to initialize multiple variable in the initialization block of the for loop by separating it by comma as given in the below example.
  1. for(int i=0, j =5 ; i < 5 ; i++)

It is also possible to have more than one increment or decrement section as well as given below.
  1. for(int i=0; i < 5 ; i++, j--)

However it is not possible to include declaration and initialization in the initialization block of the for loop.

Also, having multiple conditions separated by comma also generates the compiler error. However, we can include multiple condition with && and || logical operators.

For Loop Examples

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

please help me and give solutions for these twoo figure

* *
*$* *$*
*$*$* *$*$*
*$* *$*$*$*
* *$*$*
*$*
*

pla help me.......

A
BC
DEF
GHIJ
KLMNO
PQRSTU

hele me to solve this

***********
***** *****
**** ****
*** ***
** **
* *

help me to solve this please

a. Create a program using the following method:

i. Name, Age, Height and Weight
ii. Class name is SecretInfo
iii. Provide parameters to each method.

plz help me to find solution of this pattern

* * * * * * * * *
* * * * * * * *
* * * * * *
* * * *
* *

From Aman

  1. class Print
  2. {
  3. void cal()
  4. {
  5. int a=97;
  6. for(int i=1;i<=5;i++)
  7. {
  8. for(int j=1;j<=i;j++)
  9. {
  10. char ch=(char)a;
  11. system.out.print(ch);
  12. a=a+1;
  13. }
  14. System.out.println();
  15. }
  16. }


Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!