Java Loop statements

Java Loop statements
Whenever nosotros desire to execute a block of statements several times together with then nosotros role Loop structures.

There are 4 types of loop construction inwards Java.
1) for Loop

2) spell Loop

3) do...while Loop

4) Enhanced for Loop
-------------------------
1) for Loop
It repeats a block of statements for a specified discover of times.

Syntax:
for(stratvalue; endValue; increment/decrement) {
Statements
---------
--------
}

Example:
for (int i=1; i<=10; i++)
        {
            System.out.println(i);
        }
Example 2:
// impress ten to i Numbers using For Loop.
       
        for (int i=10; i>=1; i--)
        {
            System.out.println(i);
        }
Example 3:// impress every tenth Number upwardly to 100.
       
        for (int i=10; i<=100; i=i+10)
        {
            System.out.println(i);
        }
--------------------------------------
2) spell Loop
It repeats a block of statements spell status is true.

Syntax:
Initialization
while (condition) {
statements
------
-------
increment/decrement
}

Example:// impress i to ten Numbers using While Loop.
       
        int i = 1;
        spell (i <=10){
            System.out.println(i);
            i++;
        }
Example 2:// impress ten to i Numbers using While Loop.
       
        int i = 10;
        spell (i >= 1){
            System.out.println(i);
            i--;
        }
Example 3:// impress every tenth discover upwardly to 100 using While Loop.
       
        int i = 10;
        spell (i <= 100){
            System.out.println(i);
            i= i+10;
        }
--------------------------------------

3) produce ...while Loop---------------------------------------
it repeats a block of statements spell status is truthful together with Irrespective of the condition, it executes a block of statements at to the lowest degree once.

Syntax:
do
{
Statements
----------
----------
Increment/Decrement
} while(Condition);
------------
Example:int i = 20;
        produce {
         System.out.println("i value is : " + i);
                i++;
        } while(i <= 10);
---------------------------------------

4) Enhanced for Loop---------------------------------------
It executes all elements inwards an Array

Syntax:
for (declaration: expression) {
statements
----------
-----------
}

Example:String languages [] = {"C", "C++", "Java", "COBOL"};
        for (String lang: languages)
        {
            System.out.println(lang);
        }
------------------------

Also Read:


Java Variables

Java Operators 

Java Conditional Statements 

Java Arrays 

Java Methods

Sumber http://www.gcreddy.com/
Post a Comment (0)
Previous Post Next Post