Java 반복문

개요[ | ]

반복문으로는 while문과 for 문이 있다
  • While 문 예제
Java
CPU
1.2s
MEM
70M
0.8s
Copy
public class helloj {
    public static void main(String[] args){
        int i = 0;
        while (i < 5) {
            System.out.println(i);
            i++;
        }
    }
}
0
1
2
3
4

Java
Copy
public class helloj {
    public static void main(String[] args){
        int i = 0;
        do {
            System.out.println(i);
            i++;
        }
        while (i < 5);
    }
}
Loading


  • for 문 예제
Java
Copy
public class helloj {
    public static void main(String[] args){
        for (int i = 0; i < 5; i++) {
            System.out.println(i);
        }
    }
}
Loading
Java
Copy
public class helloj {
    public static void main(String[] args){
        String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
        for (String i : cars) {
            System.out.println(i);
        }
    }
}
Loading
편집자 218.153.224.21