for반복문은 변수초기화, 조건식, 증감식이 한줄에 모두 있다.
for(초기화식; 조건식; 증감식){
실행문;
실행문;
}
for문을 이용하여 1부터 100까지의 합을 구하는 프로그램
int total = 0; *//1부터 100까지 합한 결과값을 담기위한 변수 선언*
for(int i = 1; i <= 100; i++){ *//1부터 100까지 반복하기 위한 for 반복문* total = total + i; *// 1부터 100까지 반복해서 total 변수에 값을 누적* }
System.out.println(total); *//결과값 출력*
for문을 이용하여 1부터 100까지의 짝수의 합을 구하는 프로그램
public class ForExam {
public static void main(String[] args) {
int total = 0;
for(int i = 1; i <= 100; i++){
if(i % 2 != 0){ *// 2로 나눈 나머지가 0이 아니라는것은 홀수를 의미한다.* continue; *// 홀수일 경우 total = total + i; 문장이 실행되지 않으므로, 결과적으로 짝수만 더해준다.* }
total = total + i;
}
System.out.println(total);
}
}
같은 데이터 타입을 가진 연속된 메모리 공간으로 이루어진 자료조이다
정수를 4개 저장 할 수 있는 배열 생성.
int[] array1 = new int[4];
배열에 값을 저장
array1[0] = 1;
array1[1] = 2;
array1[2] = 3;
array1[3] = 4;