Backend/JAVA2 멘토시리즈

JAVA Eclipse 09 제어문 : 반복문 for

쏠솔랄라 2023. 3. 28. 18:13

 

 

제어문: 조건문, 반복문

 

 

반복문

: 특정 작업을 반복적으로 수행하고 싶을 때 사용하는 문법

 

 

반복문의 종류

for

while

do-while

 

 

반복문 사용 목적

: 반복되는 명령의 처리를 위해

 

 

반복문의 구조

반복문 (얼마나 반복할 것인지를 결정) {
반복해서 수행하려는 명령;
}

 

for (초기식; 조건식; 증감식) {
초기식에 선언된 변수가 조건식이 참일 동안에 반복할 명령;
}

* 초기식: 변수 생성
* 조건식: 변수의 마지노선 생성
* 증감식: 변수의 변화량 설정

 for 문의 실행순서 : 초기식 > 비교식 > 명령 > 증감식

* 이 때, 초기식은 반복문이 시작될 때 한번만 실행

 

특정한 횟수 동안 반복하고 싶을 때 사용

 

 

ex.

public class for1 {

public static void main(String[] args) {

 

for (int i=1; i<=10; i++) {

if (i%2==0) {

System.out.println(i);

}

}

}

}

반복문 안에서 if 조건문을 사용할 수 있다

 

public class for2 {

public static void main(String[] args) {

 

int sum=0;

 

for (int i=1; i<=10; i++) {

sum+=i;

}

System.out.println(sum);

}

}

 

 


 

 

JAVA Eclipse 01 프로그램, 프로그래밍, 기계어, JAVA
https://developernew.tistory.com/63

JAVA Eclipse 02 자바 출력메서드와 입력메서드
https://developernew.tistory.com/71

JAVA Eclipse 03 변수, 자료형, 형변환, 변수의 상수화
https://developernew.tistory.com/74

JAVA Eclipse 04 연산자 정의, 연산자 종류, 연산자 우선순위
https://developernew.tistory.com/78

JAVA Eclipse 05 논리연산자, 비트연산자
https://developernew.tistory.com/80

JAVA Eclipse 06 기타연산자 - 삼항연산자, 대입연산자, 복합대입연산자, instanceof
https://developernew.tistory.com/84

JAVA Eclipse 07 제어문 : 조건문
https://developernew.tistory.com/88

JAVA Eclipse 08 제어문 : 조건문 switch + Random 클래스
https://developernew.tistory.com/93