標籤:bre == junit simple example 死迴圈 port using http
Java For LoopSimple For Loop
For-each or Enhanced For Loop
Labeled For Loop
Example
import java.util.Arrays;import java.util.List;import org.junit.Test;public class ForExample { @Test public void test() { // Simple For Loop for (int i = 1; i <= 10; i++) { System.out.println(i); } System.out.println("---------------------"); } @Test public void test1() { int arr[] = { 12, 23, 44, 56, 78 }; // Printing array using for-each loop for (int i : arr) { System.out.println(i); } System.out.println("---------------------"); } @Test public void test2() { List<Integer> list = Arrays.asList(12, 23, 44, 56, 78); list.stream().forEach((i) -> System.out.println(i)); System.out.println("---------------------"); } @Test public void test3() { for (int i = 1; i <= 3; i++) { bb: for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { break bb; } System.out.println(i + " " + j); } } System.out.println("---------------------"); } @Test public void test4() { aa: for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == 2 && j == 2) { break aa; } System.out.println(i + " " + j); } } } @Test public void test5() { // 死迴圈 for (;;) { System.out.println("infinitive loop"); } }}
Java For Loop