While 문

양혜정·2024년 1월 28일
0

Begin_java

목록 보기
30/71

While 문 형식

 while(조건식) {
	조건식이 참(true)이라면 반복해서 실행할 명령문을 실행하고,
	조건식이 거짓(false)이라면 while{ } 이부분을 빠져나간다. 
	반복해서 실행할 명령문;
    증감식;
  }

- 구구단 (방법 1)

2×1 = 2 3×1=3 ... 9×1=9

int jul=0, dan=1;
	while(!(++jul>9)) {	// 9행
		while(!(++dan>9)) {	// 8열
			String add = (dan<9)?"\t":"\n";
			System.out.print(dan+"*"+jul+"="+dan*jul+add);
		}	// end of while--------------
			// 위의 while 문 빠져나올때 dan 의 값은 10 이다.
			// 그러므로 아래와 같이 dan 의 값을 1로 초기화 해주어야 한다.
		dan=1;
	}	// end of while----------------------

- 구구단 (방법2)

-> 2×1=2 2×2=4 ... 2×9=18

jul=0; dan=1;
	while(!(++dan>9)) {
		while(!(++jul>9)) {
			String add = (jul<9)?"\t":"\n";
			System.out.print(dan+"*"+jul+"="+dan*jul+add);
		}	// end of while----------
		jul=0;	// 위의 while 문 빠져나올때 jul 의 값은 9 이다. (초기화 필요)
	}	// end of while-----------

정리

my.day07.a.While -> Main_while

0개의 댓글

관련 채용 정보