상세 컨텐츠

본문 제목

반복문 do-while문

JAVA

by 본투비곰손 2022. 7. 6. 23:06

본문

728x90

while문은 조건을 먼저 체크하고 반복 수행이 되었다면 do-while문은 조건과 상관 업이 수행을 하고 조건을 체크

do{

  수행문1;

 

}while(조건식);

  수행문2;

 

while문을 사용한 반복문

package ch18;

import java.util.Scanner;

public class DoWhileTest {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		int input;
		int sum=0;
		
		input = scanner.nextInt();
		
		while(input != 0) {
			sum += input;
			input = scanner.nextInt();
		}
		System.out.println(sum);
	}

}

do-while문을 사용하여 표현 할 수 있다.

package ch18;

import java.util.Scanner;

public class DoWhileTest {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		int input;
		int sum=0;
		
		do {
			input = scanner.nextInt();
			sum += input;
		}
		while(input != 0);
		System.out.println(sum);
	}

}
728x90

'JAVA' 카테고리의 다른 글

중첩 반복문  (0) 2022.07.08
반복문 - for문  (0) 2022.07.06
반복문 - while문  (0) 2022.07.06
switch-case문  (0) 2022.07.06
조건이 여러 개 일 때의 if문  (0) 2022.07.05

관련글 더보기