• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Looping Statements In java


Looping Statements are used to execute a statement several number of times based on the condition.In java language these are classified into floowing types.
1.For Loop
2.While Loop
3.Do While Loop

for:
'for' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
Syntax

for(initilization;condition;increment/decrement)
{
    ...
    ...
}

In the above syntax :
initilization is a starting value
condition is a ending value
increment/decrement is incrementation(++) or decrementation(--)

Display 1 to 10 Values Using For Loop


class Main
{
public static void main(String args[])
{
int x;
for(x=1; x<=10; x++)
{
System.out.println(x);
}
}
}


Output:
1
2
3
4
5
6
7
8
9
10

Display 10 to 1 Values Using For Loop


class Main
{
public static void main(String args[])
{
int x;
for(x=10; x>=1; x- -)
{
System.out.println(x);
}
}
}


Output:
10
9
8
7
6
5
4
3
2
1

Nested For Loop In java


Nested For Loop :
One for loop within another for loop is knows as Nested For Loop Syntax

for(initilization ; condition ; increment/decrement)
{
     for(initilization ; condition ; increment/decrement)
    {
        ...
        ...
    }
...
..
}

In the above syntax :
Outer loop is Row
Inner loop is Column

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


class Main
{
public static void main(String args[])
{
int x,y;
for(x=1; x<=6; x++)

{
for(y=1;y<=3;y++)
{
System.out.print("*");
}
System.out.println();
}
}
}


Output:
***
***
***
***
***
***

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


class Main
{
public static void main(String args[])
{
int x,y;
for(x=1; x<=6; x++)

{
for(y=1;y<=x;y++)
{
System.out.print("*");
}
System.out.println();
}
}
}


Output:
*
**
***
****
*****
******

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


class Main
{
public static void main(String args[])
{
int x,y;
for(x=1; x<=6; x++)
{
for(y=6;y>=x;y--)
{
System.out.print("*");
}
System.out.println();
}
}
}


Output:
******
*****
****
***
**
*