For Loops

Submitted by: 
Language: 

Introduction:
This page will teach you about the For loop in Java.

What is the for loop?
The for loop is very useful in many programming languages. It is a simple way to re-run a certain script or part of code multiple times depending on integer values.

How do I create a for loop?
First you will need to decide five things to create your for loop:
1. The variable name, this will be the reference of integer within the loop.
2. The starting value.
3. The ending value.
4. What will happen for each time the script loops.
5. The script to run each time the loop occurs.

Once you have all the answers you are ready to create your for loop.

The next thing you need to know is that the for loop takes three different arguments which are separated by a line terminator (";")

First we write "for(" then our variable name followed by "=" and our starting value. We then add a ";" to separate our three arguments. After the ";" we add the variable name again and decide when we want our loop to loop (Example; While our variable name is less than 5). Finally we add another ";" followed by what happens each time the loop is ran. It is then finished with "){" {script} "}".

That above paragraph must be confusing so take a look at some examples...

Examples:
NOTE: For all the below examples, our variable name is "i".

First we have an example which uses our pre-defined start and end integer variables as set points within our loop. Each time the loop occurs it adds one the our variable i through the argument ("i++").

  1. int start = 0;
  2. int end = 5;
  3. for (int i=start;i<end;i++){
  4. System.out.println("Outputting... " + i);
  5. }

Which gives the output...

Outputting... 0
Outputting... 1
Outputting... 2
Outputting... 3
Outputting... 4

We then have the same example but without using pre-defined variables...

  1. for (int i=0;i<5;i++){
  2. System.out.println("Outputting... " + i);
  3. }

Which gives the output...

Outputting... 0
Outputting... 1
Outputting... 2
Outputting... 3
Outputting... 4

Finally we have an example which doesn't use pre-defined variables but it also goes in reverse...

  1. for (int i=5;i>0;i--){
  2. System.out.println("Reverse Outputting... " + i);
  3. }

Which gives the output...

Reverse Outputting... 5
Reverse Outputting... 4
Reverse Outputting... 3
Reverse Outputting... 2
Reverse Outputting... 1

The final thing to note is that the reason the output number values of "i" is one lower than you probably expected is because Java starts counting at 0, not 1. It also begins on the start value instead of performing the variable value change before running.

Finished!


Comments

i like your site i learn to every day. i am happy for your site.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.