Java/Iteration statements

From Meshplex

Jump to: navigation, search
Image:Java_programming.jpg
Introduction to Java
Overview of Java
How to Setup Java on your PC
Java Data Types, Variables,and Arrays
Java Operators
Java Packages, Classes, and Interfaces
Java Program
Java Modifiers
Java Control Statements
* Java Selection Statements
* Java Iteration Statements
* Java Jump Statements
Java Exception Handling
Java Object Oriented Programming
Java Wrappers
Java Strings
Java Math
Java Arrays
Java Random Numbers
Java Date and Time
Java Regular Expressions
Java Collections
Java Generics
Java Thread and Multithreading
Java IO and file Handling
Java Network Programming
Java RMI
Java Image, Audio, Video
Java GUI
Java Applets
Java Internationalization
JDBC
Java and XML

Contents

[edit] Java for Loops

For loop is a very powerful iteration statement in Java. It is used in most applications where a block of code needs to be execute for N number of times. N range from 1 to max value permitted by any datatype. If it is java int type then max permitted value is [to be filled]. Here is the general form of the for statement:

  for(initialization; condition; iteration) {
    // body

  }

When the loop starts for the first time, the initialization block of the loop is executed. Generally, this code sets the value of the loop control variable which acts as a counter that controls the loop. It is very important that the initialization expression is only executed only once. Next, condition is evaluated which is after the first semi colon in the for loop statment.

for(int i = 0; i < 5; i++)

This must be a boolean expression. It usually tests the loop control variable against a target value. If this expression is true, then the body of the loop is executed otherwise the loop will be terminated. Next is the iteration portion of the loop that is executed. This portion usually increments or decrements the loop control variable. The loop then iterates, first evaluating the conditional expression, then executing the body of the loop, and then executing the iteration expression with each pass. This process repeats until the controlling expression is false. Once controlling expression is false it will not even execute the increment portion of the for loop. It is also very important to remember that the initialization block is executed only for the first time when the program enters into for loop.

There will be times when you will want to include more than one statement in the initialization and iteration portions of the for loop. This can be achieved using comma. Let us take a very common example

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

As you can see, the loop is controlled by the interaction of two variables. Since the loop is governed by two variables, it would be useful if both could be included in the for statement, itself, instead of b being handled manually. Fortunately, Java provides a way to accomplish this. To allow two or more variables to control a for loop, Java permits you to include multiple statements in both the initialization and iteration portions of the for. Each statement is separated from the next by a comma.

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

[edit] Java Nested for loop

Java also supports nested for loop. For example
class Nested {
     public static void main(String args[]) {
       int i, j;
       for(i=0; i<6; i++) {  
         for(j=i; j<9; j++)  {  //nested for loop
             System.out.print("#");
         }
       } 
     }
   }

At times nested for loops becomes a necessity for a programmer to include it in its code. Without a nested for loop he/she may not be able to find a solution to a problem. One very common problem in computer world is bubble sort. Bubble sort is a technique for sorting of a list or an array. Bubble sort is a very traditional approach and has many performance issues. Our aim is not to discuss bubble sort but to check the usage of nested for loops.


[edit] Java while Loops

While and do while loops are another very common looping statements that can be found in many programming languages. In java also they work the same way as they work in other languages.

 

while(condition) {
  statement1;
  statement2;
  .
  .
  statementN;
}

The condition can be any boolean expression. The body of the loop will be executed as long as the conditional expression is true. When condition becomes false, control passes to the next line of code immediately following the loop. The curly braces are unnecessary if only a single statement is being repeated.

class WhileLoop {
    public static void main(String[] args) {
      int i = 0;
        while(i < 10) {
           System.out.println("tick " + i);
           i = i + 1;
        }
      }
    }
  }

The above program will execute for 10 times and each time the value of variable i will be incremented by 1. Once the value of condition variable reaches to an extent where the expression of the condition become false, it will terminate and pass on the control to the next line in the program. So in the above example it will stop executing the loop when the value of variable i grows to more than 9.

[edit] Java do while

There is only one difference between while loop and do while loop that while loop first evaluates the condition and if it is true then only the program enters into the loop. But in case of do while loop the program will enter into the loop without evaluating the condition for the first time. It will be very much clear once you see the do while loop structure.


  do {

    statement1;
    statement2;
    .
    .
    .
    statementN;

  } while(condition);


Previous Next

Bold text