자바가 제공하는 제어문을 학습하세요.
분류 | |
---|---|
선택문(조건문)(the decision-making statements) | if-then, if-then-else, switch |
반복문(the looping statements) | for, while, do-while |
분기문(the branching statements) | break, continue, return |
if ( 조건식 ) {
//statement(s)
}
true
일 때 { }
안의 statement가 실행한다. statement 실행 후 if-then
구문 빠져 나온다. false
일 때 if-then
구문 빠져나온다. { }
는 생략할 수 있다. if ( 조건식 ) {
//statement(s)1
} else {
//statement(s)2
}
true
면 statement1 실행. statement1 실행 후 if-then-else
구문 빠져나온다.false
면 statement2 실행. statement2 실행 후 if-then-else
구문 빠져나온다. switch (조건식) {
case 값1:
.. break;
case 값2:
... break;
...
default:
... break;
}
조건식의 결과에 해당하는 case
문으로 이동해서 해당 statement 실행. break
문을 만나면 switch
문 전체를 빠져 나온다.
break
를 쓰지 않아도 되는 향상된 case
문은 전 주 과제 참고 default
는 case
문에 해당하는 값 외에 모든 값 (else
같은)
조건식에는 char
, byte
, short
, int
, Character
, Byte
, Short
, Integer
, String
, enum
이 가능하다.
마지막 분기의 break
는 필요하지 않다. (작성 안 해도 잘 작동된다) 그래도 쓰는 것을 추천한다. 코드 수정이 쉽고 에러가 적을 수 있다.
언제사용? 처리해야하는 경우의 수가 많다면 if
보다 switch
를 사용한다. switch
문은 가능할 수 있는 실행 경로를 여러 개 가질 수 있기 때문.
switch
문은 한 번만 조건식을 확인해주면 되지만 if문
을 여러 개 쓸 경우 매 번 조건식을 확인하게 된다. if(x=a).. if(x=b)...
주의 switch
문의 조건절의 값이 null
이면 NullPointException
발생 가능성 있으므로 돌리기 전에 이를 체크하도록 하자
향상된 switch
문은 지난 시간에 했던 것 참고
while ( 조건식) {
statement(s)
}
{ }
이 한 번 끝난 후 체크한다는 말 public static void breakWhile() {
int a = 0;
while( a <= 3) {
a += 4;
System.out.println(a); // 결과값 4
}
}
public static void breakWhile2() {
int a = 0;
while( a <= 3) {
a += 4;
a -= 1;
System.out.println(a); // 결과값 3, 6
}
}
do {
statement(s)
} while (조건식);
do
안에 statments를 먼저 실행하고 조건식을 체크한다. 따라서 do
안의 statements는 무조건 적어도 한 번은 실행된다. public static void doWhile(){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 1);
}
// 결과값 Count is: 1
기본 for문
과 for each문(향상된 for문)
이 있다. for( 초기화, 조건식, 증감식) {
statement(s)
}
false
가 되면 루프가 중지된다. for ( ; ; ) {
}
false
가 되면 for문
을 빠져나온다. 단, while문
과 마찬가지로 for문
안의 한 번의 iteration안의 모든 statement가 끝나고 조건식을 확인한다. public static void breakFor() {
for (int i = 0; i <= 2; i++) {
i += 3;
System.out.println(i); //결과값 3
}
}
public static void breakFor2() {
for (int i = 0; i <= 2; i++) {
i += 3;
i -= 3;
System.out.println(i); //결과값 0, 1, 2
}
}
for문
보다 가독성이 좋다. for ( 타입 var : 식) {
statement(s)
}
switch
, for
, while
, do-while을
빠져나오게 한다. public static void labeledForStatement() {
int x = 0;
int y = 0;
loop1: for (int outLoop=0; outLoop<=10; outLoop++){
x = outLoop;
for (int inLoop=0; inLoop<=5; inLoop++){
y = inLoop;
if(inLoop==3) {
break loop1;
}
}
}
System.out.println(x+","+y); //결과값 0,3
}
continue
는 for
,while
,do-while
의 iteration을 skip하게 해준다. (for문
에서 continue
가 중간에 있다고 하면 continue
다음의 statement를 실행 안하고 다음 iteration 부터 시작) public static void main(String[] args) {
String searchMe = "peter piper picked a " + "peck of pickled peppers";
int max = searchMe.length();
int numPs = 0;
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;
// process p's
numPs++;
}
System.out.println("Found " + numPs + " p's in the string.");
}
break
처럼 label이 있는 것과 없는 것이 있다. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html