본문 바로가기

조건문4

13.조건문 - for문 for문의 수행 순서 for(초기화식; 조건식; 증감식) { 수행문; } 예제) public class ForTest { public static void main(String[] args) { // TODO Auto-generated method stub int count = 1; int sum = 0; for(int i=0; i 2022. 5. 17.
12.조건문 - do while 문 조건과 상관 없이 한번은 수행문을 수행 - while 문은 조건을 먼저 체크하고 반복 수행이 된다면, do-while은 조건과 상관 없이 수행을 한 번 하고나서 조건을 체크 - 조건이 맞지 않으면(true가 아니면) 더 이상 수행하지 않음 예제) - 입력받는 모든 숫자의 합을 구하는 예제 단, 입력이 0이 되면 반복을 그만하고 합을 출력 public class DoWhileTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int input; int sum = 0; input = scanner.nextInt(); do{ sum += input; input = scanner.nextInt(); }w.. 2022. 5. 17.
11.조건문 - while문 조건이 참(true)인 동안 반복수행하기 - 주어진 조건에 맞는 동안(true) 지정된 수행문을 반복적으로 수행하는 제어문 - 조건이 맞지 않으면 반복하던 수행을 멈추게 됨 - 조건은 주로 반복 횟수나 값의 비교의 결과에 따라 true, false 판단 됨 While문 - 수행문을 수행하기 전 조건을 체크하고 그 조건의 결과가 true인 동안 반복 수행 예제) public class WhileTest { public static void main(String[] args) { int num = 1; int sum = 0; while(num 2022. 5. 17.
10.조건문 - switch case문 Switch - case 문 - if elseif else 문을 사용할 때 복잡하고 번거로운 부분을 가독성 좋게 구현 - 비교 조건이 특정 값이나 문자열인 경우 사용 - break 문을 사용하여 각 조건이 만족되면 switch 블럭을 빠져나오도록 함 - 자바 14부터 좀 더 간결해진 표현식이 지원 됨(break 사용하지 않음) 예제) public class SwitchCaseTest { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int month = scanner.nextInt(); int day; switch(month) { case 1: day = 31; break; case 2: day = 28.. 2022. 5. 17.