If you want to break/stop the for loop in certain condition then you can use "break":
" continue " in the for loop
If you want to continue the for loop in certain condition then you can use "continue":
The value of i is : 1
The value of i is : 2
The value of i is : 3
The value of i is : 4
The value of i is : 5
//Array
String[] str = {"TX", "NY", "CA"};
System.out.println("-------Array using For Loop--------");
for (int i = 0; i < alist.size(); i++) {
System.out.println(str[i]);
}
// for loop another way
System.out.println("-------Array using For Loop with semicolon--------");
for (String x : str) {
System.out.println(x);
}
//Arraylist
List<Integer> arlist = Arrays.asList(5,6,7);// ArrayList
// 1. for loop
System.out.println("-------ArrayList using For Loop--------");
for (int i = 0; i < alist.size(); i++) {
System.out.println(arlist.get(i));
}
// for loop another way
System.out.println("-------ArrayList using For Loop with semicolon--------");
for (Integer x : arlist) {
System.out.println(x);
}
Output:
-------Array using For Loop--------
TX
NY
CA
-------Array using For Loop with semicolon--------
TX
NY
CA
-------ArrayList using For Loop--------
5
6
7
-------ArrayList using For Loop with semicolon--------
5
6
7
No comments:
Post a Comment