logo

자바 While 루프

그만큼 자바 while 루프 지정된 부울 조건이 true가 될 때까지 프로그램의 일부를 반복적으로 반복하는 데 사용됩니다. 부울 조건이 거짓이 되는 즉시 루프가 자동으로 중지됩니다.

while 루프는 반복되는 if 문으로 간주됩니다. 반복 횟수가 고정되어 있지 않은 경우 while을 사용하는 것이 좋습니다. 고리 .

통사론:

 while (condition){ //code to be executed I ncrement / decrement statement } 

do-while 루프의 다양한 부분:

1. 조건: 테스트되는 표현식입니다. 조건이 true이면 루프 본문이 실행되고 제어는 표현식 업데이트로 이동합니다. 조건이 거짓이 되면 while 루프를 종료합니다.

:

나<=100< p>

2. 표현식 업데이트: 루프 본문이 실행될 때마다 이 표현식은 루프 변수를 증가시키거나 감소시킵니다.

예:

나++;

Java While 루프의 흐름도

여기서 while 루프의 중요한 점은 때로는 실행되지 않을 수도 있다는 것입니다. 테스트할 조건이 false이면 루프 본문을 건너뛰고 while 루프 다음의 첫 번째 문이 실행됩니다.

Java while 루프의 흐름도

예:

아래 예에서는 1부터 10까지의 정수 값을 인쇄합니다. for 루프와 달리 조건에 사용된 변수(여기서는 i)를 별도로 초기화하고 증가시켜야 합니다. 그렇지 않으면 루프가 무한히 실행됩니다.

WhileExample.java

 public class WhileExample { public static void main(String[] args) { int i=1; while(i<=10){ system.out.println(i); i++; } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> 1 2 3 4 5 6 7 8 9 10 </pre> <h2>Java Infinitive While Loop</h2> <p>If you pass <strong>true</strong> in the while loop, it will be infinitive while loop.</p> <p> <strong>Syntax:</strong> </p> <pre> while(true){ //code to be executed } </pre> <p> <strong>Example:</strong> </p> <p> <strong>WhileExample2.java</strong> </p> <pre> public class WhileExample2 { public static void main(String[] args) { // setting the infinite while loop by passing true to the condition while(true){ System.out.println(&apos;infinitive while loop&apos;); } } } </pre> <p> <strong>Output:</strong> </p> <pre> infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+c </pre> <p>In the above code, we need to enter Ctrl + C command to terminate the infinite loop.</p> <hr></=10){>

Java 부정사 While 루프

합격하면 진실 while 루프에서는 while 루프가 부정사입니다.

통사론:

 while(true){ //code to be executed } 

예:

WhileExample2.java

 public class WhileExample2 { public static void main(String[] args) { // setting the infinite while loop by passing true to the condition while(true){ System.out.println(&apos;infinitive while loop&apos;); } } } 

산출:

 infinitive while loop infinitive while loop infinitive while loop infinitive while loop infinitive while loop ctrl+c 

위 코드에서 무한 루프를 종료하려면 Ctrl + C 명령을 입력해야 합니다.