PowerJava [2판] Theory Chaper 06 반복문
Programming/Java / Flex
2013. 1. 29. 11:05
▣ while문
public class Ch6_Ex { public static void main (String[] args) { int i = 0 ; while ( i < 10 ) { System.out.println(" i의 값은 " + i) ; i ++ ; } } }
while문은 루프구조의 상단에 조건식이 존재하므로 조건을 만족하면 내부 로직이 한번도 실행되지 않을 수 있다.
▣ do-while문
public class Ch6_Ex { public static void main (String[] args) { int i = 10 ; do { System.out.println(" i의 값은 " + i) ; i ++ ; } while ( i < 10 ) ; } }
do-while문은 루프구조의 하단에 조건식이 존재하므로 조건을 만족하더라도 내부 로직이 한번은 실행된다.
▣ do-while문
public class Ch6_Ex { public static void main (String[] args) { for ( int i = 0; i < 10; i++ ) { System.out.println(" i의 값은 " + i) ; } } }
for문은 설정된 조건식과 증감식에 의한 횟수만큼 내부 로직이 실행된다.
▣ break
import java.util.* ; public class Ch6_Ex { public static void main (String[] args) { int i = 0 ; int total = 0 ; int counting = 0 ; int cost = 0 ; Scanner scan = new Scanner(System.in) ; while ( true ) { System.out.println(" 금액을 입력하시오 : ") ; int cost = scan.nextInt() ; if ( cost < 100 ) break ; total += cost ; counting++ ; } System.out.println( "평균금액은 " + total / counting + "원입니다." } }
break문은 해당 루프문을 종료시킨다.
check_loop : while ( true ) { while (true) { if ( cost < 100 ) break check_loop ; } }
중첩 루프문에 레이블을 부여하여 break문을 사용하는 경우 그 레이블에 해당하는 중첩 루프문을 모두 종료시킨다.
▣ continue
public class Ch6_Ex { public static void main (String[] args) { int counting = 0 ; for ( int i = 0; i < 10; i++ ) { if ( i % 2 = 0 ) continue ; counting++ ; } System.out.println("2의 배수의 갯수는 " + counting + "개입니다.") ; } }
if문 안의 조건을 만족할는 경우에만 전체 for문을 루프하며, 만족하지 않는 경우 다시 for문 상단으로 돌아가 로직을 반복한다.
'Programming > Java / Flex' 카테고리의 다른 글
[Flex] Popup창에서 부모창으로 데이터 호출하기 (0) | 2013.11.27 |
---|---|
[Eclipse] 단축키 (0) | 2013.05.28 |
[Flex] Datagrid / Arraycollection addItem(At) (0) | 2013.05.05 |
PowerJava [2판] Theory Chaper 06 조건문 (0) | 2013.01.29 |
PowerJava [2판] Theory Chaper 01~05 (0) | 2013.01.28 |