Over 500 Magazines for FREE!

Yes! You can subscribe to ALL of them. They will be shipped to your home FREE of cost!
Kindly click here to apply!

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

please help me and give solutions for these twoo figure

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

pla help me.......

A
BC
DEF
GHIJ
KLMNO
PQRSTU

hele me to solve this

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

Post new comment

To combat spam, please enter the code in the image.




Do you have a better example?
We're sure you have hundreds of Java program examples.

Spare some time and submit your java example here even if you think it's too small to contribute.

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




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