왜 쓸까?
언제 발생하는지 모르는 조건을 사용할려할때 while문이 유용하다.
예제를 보자
while문은 조건이 참일때만 반복문을 진행하고
int sum=0;
int count = 0;
while(count <=10) {
sum=sum+count;
count++;
}
System.out.println(sum);int sum=0;
int count = 0;
for(count=0;count<=10;count++) {
sum=sum+count;
}
System.out.println(sum);
조건이 거짓이면 반복문을 멈춘다.
while문은 조건이 참이되는 동안 실행
for문은 조건이 달성될때까지 실행
'(혼) (공) (자)' 카테고리의 다른 글
do while문 (2) | 2023.12.27 |
---|---|
while-up,down (0) | 2023.12.27 |
다중 for문 - 구구단 (1) | 2023.12.25 |
다중 for문 (0) | 2023.12.25 |
for 반복문 (0) | 2023.12.25 |