While Loop
Coddy Java 여정의 기초 섹션에 포함된 레슨 — 73개 중 43번째.
while 루프는 for 루프와 다릅니다. for 루프는 특정 range를 반복할 수 있게 하지만, while 루프는 특정 조건이 충족되는 한 계속 반복할 수 있게 합니다.
while 루프를 사용하려면 다음과 같이 작성하세요:
while (condition) {
code
}조건이 true인 경우에만 코드가 실행됩니다.
while이 문제를 해결할 수 있지만 for 루프는 그렇지 않은 많은 사용 사례가 있습니다.
챌린지
초급하나의 입력인 double number를 받는 프로그램을 작성하세요.
number가 3.5보다 크거나 같은 동안 while 루프를 사용하여 입력을 2로 나누세요.
3.5보다 작은 첫 번째 숫자를 출력하세요.
치트 시트
while 루프는 조건이 true인 한 코드를 실행합니다:
while (condition) {
code
}for 루프가 특정 범위를 반복하는 것과 달리, while 루프는 조건이 충족되는 한 계속됩니다.
직접 해보기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 아래에 코드를 작성하세요
scanner.close();
}
}이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
기초의 모든 레슨
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else