1. 리버스 루프

Java에는 while루프라는 또 다른 종류의 루프가 있습니다 do-while. 일반 while루프와 매우 유사하며 "조건"과 "루프 본문"의 두 부분으로만 구성됩니다. 루프 본문은 조건이 인 한 반복해서 실행됩니다 true. 일반적으로 do-while루프는 다음과 같습니다.

do
   statement;
while (condition);

또는

do
{
   block of statements
}
while (condition);

루프 의 경우 while실행 순서는 condition , loop body , condition , loop body , condition , loop body , ... 입니다.

그러나 do-while루프의 경우 약간 다릅니다: 루프 본문 , 조건 , 루프 본문 , 조건 , 루프 본문 , ...

실제로 while루프와 루프의 유일한 차이점은 루프 본문이 루프 에 대해 적어도 한 번 실행된다는 do-while사실입니다 .do-while


do-while2. 루프 사용의 이점

do-while기본적으로 루프와 루프 의 유일한 차이점은 루프 의 본문이 적어도 한 번 실행된다는 while것입니다 .do-while

일반적으로 do-while루프 본문이 실행되지 않은 경우 루프 조건을 확인하는 것이 의미가 없을 때 루프를 사용합니다. 예를 들어 특정 계산이 루프 본문 에서 수행된 다음 조건 에서 사용되는 경우입니다 .

예:

exit프로그램은 단어가 입력될 때까지 키보드에서 줄을 읽습니다.

~하는 동안 하는 동안
String s;
while (true)
{
   s = console.nextLine();
   if (s.equals("exit"))
      break;
}
String s;
do
{
   s = console.nextLine();
}
while (!s.equals("exit"));

루프 의 및 문은 루프 에서와 동일한 방식으로 작동 break합니다 .continuedo-whilewhile


do-while3. 루프 비교 : Java와 Pascal

다시 한 번 Pascal은 루프와 유사 do-while하지만 이를 repeat-until루프라고 합니다. 또한 루프와는 약간 다릅니다 do-while. 루프 에서 repeat-until조건은 루프를 계속할 시기가 아니라 루프를 종료할 시기를 나타냅니다.

예:

파스칼 자바
Repeat
   ReadLn(s);
Until s = 'exit';
String s;
do {
   s = console.nextLine();
}
while ( !s.equals("exit") );

Java와 비교할 때 Pascal이 이것을 표현하는 방식은 정말 아름답습니다. 파스칼의 예부터 시작해야 합니다. 그렇지 않으면 웃을 것입니다.


2
과제
Java Syntax,  레벨 6레슨 5
잠금
Cat's finalize method
It's difficult to accidentally lose an object: as long as you have even one reference to an object, it remains alive. But if not, then the object is approached by the finalize method, an unpredictable assassin that works for the Java machine. Let's create this method ourselves: protected void finalize() throws Throwable. The last two words will become clear a little later.