while 문

인철·2023년 10월 13일
0

Java

목록 보기
11/52

while 문

public static void main(String[] args) {

 

//while문으로 1부터 10까지 합 출력하기

//while문 구조는 변수선언, 조건, 증감식으로 for문과 같지만 다른점은 for문은 같은 장소에 한번에 쓰고 while문은 차례대로 쓴다

 

int sum = 0;//int sum 선언하고 0으로 초기화하기

int i = 1; // int i선언하기

while(i<=10) { //조건설정하기

sum = sum + i;

i++; //증감식 설정하기

}

System.out.println(" sum : " + sum); //출력문 설정하기

}

//while문은 반복횟수가 정해져 있지 안은 만큼 조건식이 true일경우
//계속 반복이 된 

출력내용 

sum : 55
profile
같은글이있어도양해부탁드려요(킁킁)

0개의 댓글