if문
- 조건을 만족하는 경우에만 실행할 수 있다.
- 형식
int score = 100;
if (score >= 60) {
System.out.println("합격")
}
if (score < 60)
System.out.println("불합격");
else if문
- if문 이후에 조건식이 필요한 경우 사용할 수 있다.
- 원하는 만큼 반복해서 사용할 수 있다.
- 형식
int score = 500;
if(score < 0 || score > 100) {
System.out.println("잘못된 점수");
} else if(score >= 60) {
System.out.println("합격");
} else if(score < 60) {
System.out.println("불합격");
}
else 문
- if문의 마지막에 추가할 수 있다.
- 조건식은 작성하지 않는다.
- 형식
int score = 100;
if (score < 0 || score > 100) {
System.out.println("잘못된 점수");
} else if (score >= 60) {
System.out.println("합격");
} else {
System.out.println("불합격");
}
switch문
- 지정된 표현식의 결과에 따라 분기 처리한다.(true/false에 의한 분기 처리가 아니다.)
- 일반적으로 if문에 비해서 가독성이 좋다.
- 각 분기를 구분하는 case와 default로 구성된다.
- break를 통해서 switch문을 종료시킬 수 있다.
- 형식
int button = 1;
switch(button) {
case 1:
System.out.println("목록보기");
case 2:
System.out.println("상세보기");
break;
case 3:
System.out.println("삽입하기");
break;
case 4:
System.out.println("수정하기");
break;
case 5:
System.out.println("삭제하기");
break;
default:
System.out.println("잘못된 선택");
}