Java for loop
A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to execute a specific number of times.
A for loop is useful when you know how many times a task is to be
repeated.
For Loop syntax
for ( initialization ; condition ; increment / decrement ) {
// statements
}
The initialization part is used to declare and initialize variables
that will be used by the loop. For example, we can write the initialization
part as
int i = 1
The condition is the loop condition similar to the one in while and do-while loops. The increment/decrement part is commonly used to include statements like i++, i– etc.
int i = 1
The condition is the loop condition similar to the one in while and do-while loops. The increment/decrement part is commonly used to include statements like i++, i– etc.
The for loop is executed in the following way: (Algorithm)
Step 1. The statements in initialization are executed.
Step 2. The loop condition is evaluated. If it is true, go to step 10 else move out of the loop.
Step 3. Execute the statement in the loop body.
Step 4. Execute the increment /decrement part.
Step 5. Go to step 2.
Step 1. The statements in initialization are executed.
Step 2. The loop condition is evaluated. If it is true, go to step 10 else move out of the loop.
Step 3. Execute the statement in the loop body.
Step 4. Execute the increment /decrement part.
Step 5. Go to step 2.
Here is a loop that prints numbers from 1 to 10.
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
Here is a complete program.
public class Test {
public static void main(String
args[]) {
for(int x = 10; x < 20;
x++) {
System.out.println("value
of x : " + x );
}
}
}
Few more Examples of for loop
// Java program for Factorial
public class Factorial
{
public static void main(String
args[])
{
int i, f; // Local Variable
f = 1; // Initialize f with 1
// Start the Loop for
calculating factorial of 5
for(i=1; i<=5; i++)
{
f = f * i;
} // end of Loop
// Printing the value
System.out.println("Factorial=" + f);
}
}
Output:
Factorial = 120
// Java program for Power
public class MyPower
{
public static void
main(String args[])
{
int b = 10; // For Base
int e = 3; // For Exponent
int i; // For Loop
int p = 1; // For Power
// Start the Loop
for(i=0; i<e; i++)
{
p=p*b; // Calculating power
}
// Print the value
System.out.println("Power of " + b + " and " + e +
" is " + p);
}
}
Output:
Power of 10 and 3 is 1000
class ForLoop
{
public static void
main(String args[])
{
int i;
int j;
for(i=1 ;i<=5; i++)
{
for(j=1; j<=i; j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Output:
1
12
123
1234
12345
public class Pattern
{
public static void main(String args[])
{
int i,j,n=1,k,m=0,p=1;
for(i=1;i<=5;i++)
{
for(k=1;k<=5-i;k++)
System.out.print(" ");
for(j=i;j<=n;j++)
{
System.out.print(j);
}
n+=2;
for(k=m;k>=i;k--)
System.out.print(k);
m+=2;
p++;
System.out.println();
}
}
}
output:
1
232
34543
4567654
567898765
Series in Java
public class Pattern
{
public static void main(String args[])
{
int i,j,n=1,k,m=0,p=1;
for(i=1;i<=5;i++)
{
for(k=1;k<=5-i;k++)
System.out.print(" ");
for(j=i;j<=n;j++)
{
System.out.print(j);
}
n+=2;
for(k=m;k>=i;k--)
System.out.print(k);
m+=2;
p++;
System.out.println();
}
}
}
output:
1
232
34543
4567654
567898765
public class PatternPlusMinus
{
public static void main(String args[])
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=i;j<=5;j++)
{
if(j%2==0)
System.out.print("+"+" ");
else
System.out.print("-"+" ");
}
System.out.println();
}
}
}
Output:
- + - + -
+ - + -
- + -
+ -
-
Series in Java
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 =55
public class Series1
{
public static void main()
{
int n=10, i, s=0;
for(i=1;i<=n;i++)
{
if(i<10)
{
System.out.print(i + " + ");
s=s+i;
}
else
{
System.out.print(i + " ");
s=s+i;
}
}
System.out.println("="+s);
}
}