int i = 1;
for(; i<=100; i++){...}
for(int i=0, j=100; i<=50 && j>=50; i++,j--){...}
향상된 for문
public class AdvancedForExample{
public static void main(Sting[] args){
int[] scores = {95,71,84,93,87}; // int 배열
int sum = 0;
// 배열의 각 원소에 접근
for(int score : scores){
sum = sum+score;
}
System.out.println("점수총합="+sum);
}
}
while문
while문은 조건식이 true일 경우에 계속해서 반복한다.
while(조건식){
실행문;
}
while(i<=10){
System.out.println(i);
i++;
}
do-while문
while문과의 차이점은 while문은 조건을 먼저 검사한 후 실행하고, do-while문은 실행문을 실행한 후에 조건을 검사한다.
do{
실행문;
}while(조건식);
break문
break문은 반목문인 for, while, do-whlie문을 실행 중지할 때 사용된다.(+switch문)
while(true){
int num = (int)(Math.randon*6)+1;
System.out.println(num);
if(num == 6){
break;
}
}
이때, break문은 가장 가까운 반복문만 종료한다. 만약 반복문이 중첩되어 있을 경우 Lavel을 붙이고 break Lavel이름;을 하면 라벨이 붙은 반복문까지 종료된다.