728x90
첫 번째 - While문
using System;
using System.Collections.Generic;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
int a = 1;
while(true)
{
a++;
}
Console.WriteLine(a);
}
}
}
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
//while 반복문
int count = 0;
while (count < 5)
{
Console.WriteLine("Hello World");
count++;
}
}
}
}
두 번째 - do while문
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
//거울아 거울아 ~
do
{
Console.WriteLine("강사님은 잘생기셨나요? (y/n) : ");
string answer = Console.ReadLine();
} while (answer != "y"); //answer에 빨간줄이 뜬다.
}
}
}
현재 컨텍스트에 없다는 오류는
string answer로 정의 한 코드가
do
{
}
사이에 있기 때문이다
그리고 while(answer != "y");는 그 바깥에 있기 때문에
컨텍스트에 없다는 오류가 뜬다.
그러므로
네 번째 껄로 만들어보자.
using System;
namespace Csharp
{
class Program
{
static void Main(string[] args)
{
//거울아 거울아 ~
string answer = null;
//일단 do안에 들어와서 한 번은 실행, 그리고나서, while문을 체크해서 반복할지 빠져나갈지 체크
do
{
//출력
Console.WriteLine("강사님은 잘생기셨나요? (y/n) : ");
//입력
answer = Console.ReadLine(); //여기 적혀있던 string은 지워줬다.
} while (answer != "y");
}
}
}
n을 적으면 절대 빠져나오지 못한다.
y를 적으면 종료된다.
728x90
'● > 섹션 2. 코드의 흐름 제어' 카테고리의 다른 글
08. 함수(ref) (0) | 2021.07.10 |
---|---|
07.break, continue(흐름 제어) (0) | 2021.07.10 |
06. for (0) | 2021.07.10 |
04. 상수와 열거형(하드코딩 교정) (0) | 2021.07.10 |
03. 가위-바위-보 게임 (0) | 2021.07.10 |