Opening Hours :7AM to 9PM
Looping Statements are used to execute a statement several number of times based on the condition.In java language these are classified into following types.
1.For Loop
2.While Loop
3.Do While Loop
'for' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
for(initilization;condition;increment/decrement) { Statement 1; Statement 2; .. .. }
In the above syntax :
initilization is a starting value
condition is a ending value
increment/decrement is incrementation(++) or decrementation(--)
public class Main { public static void main(String args[]) { int x; for(x=1; x<=10; x++) { System.out.println(x); } } }
public class Main { public static void main(String args[]) { int x; for(x=10; x>=1; x- -) { System.out.println(x); } } }
Nested For Loop :
One for loop within another for loop is knows as Nested For Loop
for(initilization ; condition ; increment/decrement) { for(initilization ; condition ; increment/decrement) { Statement 1; Statement 2; .. } Statement 3; Statement 4; .. .. }In the above syntax :
public 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(); } } }
public 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(); } } }
public class Main { public static void main(String args[]) { int x,y; for(x=1; x<=6; x++)> { for(y=6;y>=3;y--) { System.out.print("*"); } System.out.println(); } } }