for each 반복문

최이선·2021년 11월 19일
0
  • for each 문

for each문은 ( 변수 : 배열 ) --> 변수와 배열의 타입은 같아야함

  1. 배열을 사용할 수 있는 반복문
  2. 인덱스 번호가 필요한 경우 for문을 사용
String[] fruits = {"사과","복숭아","딸기"};
int[] n = {1,2,3};
boolean[] b = {true,false,true};
double[] d = {1.1, 2.2, 3.3, 4.4};

for(String x : fruits) {
	System.out.print(x + " ");	// " " 하는 이유는 배열간의 간격 띄우기위해
}
System.out.println();			// 다음 줄로 넘어가기
for(int x : n) {
	System.out.print(x + " ");
}
System.out.println();
for(boolean x : b) {
	System.out.print(x + " ");
}
System.out.println();
for(double x : d) {
	System.out.print(x + " ");
}
 

for each 반복문


결과값
결과값

0개의 댓글