Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions? | Software Development
 

For Loop in Java

In this section you will learn how to use the For-Loop statement in java.

For Loop in Java - Java For Loop Examples & Syntax

                         

The for loop is the type of  looping construct. It also works as while loop construct but it provide the initialization, condition and the increment  is same written in the for construct. All the statements which has to be executed written in the for block. We can use the multiple for loop in a program or a for loop also. When we write a loop under another loop then the second loop is called nested loop.

The Syntax for the For Loop:  

        for( initialization; termination; increment)
        {
            statements;
        }

The given example illustrates that how to use the for loop for developing a application or a program. In this program we will see that the initialization of the variable, condition and increment used for the for loop. Initialization, Condition and Incrementation, all are optional but two semicolons are compulsory which separates Initialization, Condition and Incrementation.

Initialization: It allows the variable to be initialize. Such as: 
               
int i = 1;
                int j = 1;
Termination (or condition): It allows to check the certain condition. If condition is true then all statements and processes written in the for block will be executed otherwise ignored. Condition such as:
               
i <= 5;
                j <= i;
Increment: It allows the how much increase the given variable. Such as:
               
i++;
                j++; 

Code of the program : 

public class  ForLoop{
  
public static void main(String[] args){
    for(int i = 1;i <= 5;i++){
      for(int j = 1;j <= i;j++){
        System.out.print(i);
      }
      System.out.println();
    }
  }
}

Output of the program : 

1
22
333
4444
55555

Download For Loop Example

                         

» View all related tutorials
Related Tags: c com class date comparison io methods method dates using equals this for ssi e iso section not ls first

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

12 comments so far (
post your own) View All Comments Latest 10 Comments:

i need java programs

Posted by hannah nirubha on Tuesday, 01.19.10 @ 15:29pm | #94149

This code is very importent for biggner I want to say thaks to rose india.

Posted by Rishikesh on Thursday, 01.7.10 @ 14:29pm | #93854

can you please help for making a java program
example is this situation

1
21
321
4321
54321
654321

Posted by kenny on Tuesday, 01.5.10 @ 12:34pm | #93786

Can You please help me do this program which states the condition:
Write A program that will accept two numbers.The first Number will be the base and the second number is the exponent.
Example:
Enter Base number(1-9): 2
Enter Exponent: (1-9): 3
The Result is: 8

Posted by Saldy on Wednesday, 12.10.08 @ 22:21pm | #82616

I want more example on how to use looping in java program.

Posted by Ariel on Tuesday, 12.9.08 @ 01:54am | #82540

hello again,

can you help me out in doing a java program using loops that will display 10 names in a column then display on the opposite of each name the number of characters.
then at the bottom will display the same entries of names but this time in two columns, 1st name on the 1st column and the 10th being the 1st on the 2nd column.

ex:
Name: No of Char
Norman 6
Dandi 5
Olga 4
Danilo 6
Orlando 7
Manny 5
Rosalinda 9
Armand 6
George 6
Obama 5

Norman Obama
Dandi George
Olga Armand
Danilo Rosalinda
Orlando Manny

Thank you in advance.

Norman

Posted by Norman on Saturday, 11.15.08 @ 01:57am | #81692

Hey Deepak,

Thank you very much for responding to my request. You have been very helpful.

hope to keep in touch with you again.

Thanks again.

Norman

Posted by Norman on Friday, 09.26.08 @ 08:44am | #80711

_____1
____232
___34543
__4567654
_567898765

Posted by Lester on Saturday, 09.20.08 @ 17:24pm | #80570

can somebody please help me how to make a java program that will give the following results?

***
***
***

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

i will really, really appreciate it...

thanks a lot in advance...

Norman

Posted by Norman on Monday, 09.8.08 @ 13:29pm | #77967

Can anyone help me with creating a Java output like this please:

1 2 3
4 5 6

Posted by harvey13 on Wednesday, 08.13.08 @ 22:35pm | #72697

 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.