조건문
조건문은 프로그램 명령이 주어진 조건이 참인 경우에만 선택적으로 실행되도록하는 문법 표현으로 if문과 switch문이 있다.
if문
괄호 안에 주어진 조건이 참인 경우 블록{}안을 수행.
if(조건) {
// ... 조건이 참인 경우 실행할 명령 ...
}
조건에 명시할 수 있는 형식
- 비교식 (=, !=, >, >=, <, <=)
- 논리식 (&&, ||)
- 논리값 (true/false)
If1.java 예제
public class If1 {
public static void main(String[] args) {
int myage = 19;
if (myage == 19) {
System.out.println("19살 입니다.");
}
if (myage != 20) {
System.out.println("19살 입니다.");
}
if (myage > 19) {
System.out.println("성인 입니다.");
}
if (myage <= 19) {
System.out.println("학생 입니다.");
}
}
}
If2.java 예제
public class If2 {
public static void main(String[] args) {
int point = 75;
// AND(&&) --> 두 조건이 모두 참이어야 전체가 참
if (point > 70 && point <= 80) {
System.out.println("C학점 입니다.");
}
// OR(||) --> 두 조건중 하나라도 참이면 전체가 참
if (point <= 70 || point > 80) {
System.out.println("범위를 벗어났습니다.");
}
}
}
If3.java 예제
public class If3 {
public static void main(String[] args) {
boolean is_korean = true;
if (is_korean == true) {
System.out.println("한국사람입니다.");
}
if (is_korean == false) {
System.out.println("한국사람이 아닙니다.");
}
if (is_korean) { // 값 자체가 참이므로 성립된다.
System.out.println("한국사람입니다.");
}
if (!is_korean) { // "!"는 값을 부정한다. 참을 부정하므로 거짓이다.
System.out.println("한국사람이 아닙니다.");
}
}
}
2) if ~ else 문
조건이 참인 경우 if 블록이 실행, 그렇지 않은 경우 else 블록이 실행
if(조건) {
// ... 조건이 참인 경우 실행할 명령 ...
}
else {
// ... 조건이 참이 아닌 경우 실행할 명령 ...
}
Ifelse.java 예제
public class Ifelse {
public static void main(String[] args) {
boolean is_korean = false;
if (is_korean == true) {
System.out.println("한국사람입니다.");
}
else {
System.out.println("한국사람이 아닙니다.");
}
}
}
#03. if ~ else if ~ else 문
여러 개의 조건을 나열하여 그 중 가장 처음 만나는 참인 조건의 블록을 수행하고 그 외의 블록은 실행하지 않고 빠져 나간다.
else if
블록은 필요한 만큼 나열 가능하다.
else
블록은 필요하지 않다면 생략 가능하다.
if (1차 조건) {
...
}
else if (2차 조건) {
...
}
else if (3차 조건) {
...
}
else {
...
}
ElseIf.java
public class ElseIf {
public static void main(String[] args) {
int point = 87;
if (point > 90 && point <= 100) {
System.out.println("A");
}
else if (point > 80 && point <= 90) {
System.out.println("B");
}
else if (point > 70 && point <= 80) {
System.out.println("C");
}
else{
System.out.println("f");
}
}
}
#04. switch문
하나의 변수값에 대한 여러가지 경우의 수를 나열하고 그 중 일치하는 case 블록을 break 명령을 만나기 전까지 실행함.
switch (변수) {
case '값1' : // 경우의 수는 필요한 만큼 나열
...
break;
case '값2' :
...
break;
case '값n' :
...
break;
default: // 일치하는 경우가 없을 경우 수행할 블록(생략가능)
...
break;
}
Switch1.java예제
public class Switch1 {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A' :
System.out.println("91~100점 사이 입니다");
break;
case 'B' :
System.out.println("81~90 사이 입니다");
break; // 주석으로 막은 상태로도 실행해 보세요
case 'C' :
System.out.println("71~80 사이 입니다");
break;
default :
System.out.println("70점 이하 입니다.");
break;
}
}
}
의도적으로 break절을 조절하는 경우
조건에 맞는 case가 두 개 이상인 경우
Switch2.java 예제
public class Switch2 {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A' :
case 'B' :
case 'C' :
System.out.println("Pass");
break;
default :
System.out.println("Not-Pass");
break;
}
}
}
'코딩 공부 > Java' 카테고리의 다른 글
[Java] 08_기본문법활용 (0) | 2020.05.08 |
---|---|
[Java] 07_반복문 (0) | 2020.05.08 |
[Java] 05_기본입출력 (0) | 2020.05.08 |
[Java] 04_연산자 (0) | 2020.05.08 |
[Java] 03_변수 (0) | 2020.05.08 |