컴퓨터/Java

조건문, 반복문

수제녹차 2019. 9. 5. 11:14
728x90
반응형

- 조건문

1. 양자택일 : if

if

else if

else

 

2. 다자택일 : switch

System.out.print("점수를 입력하세요 : ");
Scanner inputNum = new Scanner(System.in);
int score = inputNum.nextInt(); // 숫자를 입력받을 때

switch(score) { // 비교할 대상의 값
    case 100:
    case 90:
       System.out.println("수"); // 100점, 90점일 때 모두
    break;
    
    case 80:
       System.out.println("우");
    break;
    
    case 70:
       System.out.println("미");
    break;
    
    default:
       System.out.println("try again!!");
    break;
}
inputNum.close()

 

- 반복문

특정 조건에 따라 프로그램을 반복적으로 실행

 

- for문

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

 

- while문

while (rNum < 10) {}

- do ~ while문

while문 비슷, 차이점은 조건 결과에 상관없이 무조건 최초 한 번은 do의 프로그램을 수행한다.

do {...} while (rNum < 10);

do {

   System.out.println("hi");

   i++

} while (i <13);

 

반응형