For loop

For Loop
For Loop

For loop:
for loop is an iterative statement.it is used to repeat a set of statement numbers of times. A for loop statement consumes the initialization,condition and increment/decrement, increment and decrement is used for updation for next iteration.the syntax or the method of for loop is

for(initializations;condition;increment/decrement/updating)
{
-
statements;
-
-
}

1.EXAMPLE FOR FOR LOOP

import java.util.*;
class LOOP
{
public static void main(String args[])
{
int i;
for(i=10;i>0;i--)
{
System.out.println("The value of is:"+i);
}
}
}
OUTPUT:
the value is:10
the value is:9
the value is:8
the value is:7
the value is:6
the value is:5
the value is:4
the value is:3
the value is:2
the value is:1

2.Generate random number in java

import java.util.*;
class random
{
public static void main(String args[])
{
int counter;
Random rnum=new Random();
/*Below code would generate 10 random numbers *between 0 to 100*/
System.out.println("Random numbers are:");
for(counter=1;counter<=10;counter++)
{
System.out.printf(rnum.nextInt(100));
}
}
}

OUTPUT:
Random numbers are:
4
47
20
90
69
3
71
27
24
50

3.Display the pattern

*
**
***
****
*****
class pattern
{
public static void main(String args[])
{
int i;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}//end of main
}//end of class
Translate »