While, do statements
Peponi │ 11/14/2024 │ 2m
C#
SyntaxStatement
While, do statements
11/14/2024
2m
Peponi
C#
SyntaxStatement
1. Introduction
while
, do
문은 지정된 블록을 반복 실행한다. while
, do
의 실행 차이점은 아래와 같다.
while
: 주어진 조건식이true
일 때 실행do
: 주어진 조건식이false
일지라도 반드시 한 번 이상 실행
반복문에서는 break 문, continue 문을 통해 반복 중단 또는 다음 반복 실행을 할 수 있다.
2. while
while
문은 조건식이 true
일 때 이하 블록을 반복 실행한다. while
문의 구성은 아래와 같다.
while (condition)
{
}
condition
: 루프 실행을 위한 조건을 지정한다. 조건식은boolean 식
으로 설정하며,true
일 때 실행된다.
while
문은 다음과 같이 사용할 수 있다.
var index = 0;
while (index < 5)
{
Console.WriteLine(index++);
}
/* output:
0
1
2
3
4
*/
3. do
do
문은 조건식이 true
일 때 지정 블록을 반복 실행한다. 블록 실행 후 조건을 판단하기 때문에 1회 이상 실행
을 보장한다. do
문은 다음과 같이 사용할 수 있다.
var index = 0;
do
{
Console.WriteLine(index++);
}
while (index < 5);
/* output:
0
1
2
3
4
*/