-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.FlowControl.cs
371 lines (340 loc) · 9.44 KB
/
6.FlowControl.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using System;
using static System.Console;
namespace CSharp
{
class FlowControl
{
static public void IfElse()
{
Write("숫자를 입력하세요. : ");
string input = ReadLine();
int number = Int32.Parse(input);
if (number < 0)
WriteLine("음수");
else if (number > 0)
WriteLine("양수");
else
WriteLine("0");
if (number % 2 == 0)
WriteLine("짝수");
else
WriteLine("홀수");
}
static public void IfIf()
{
Write("숫자를 입력하세요 : ");
string input = ReadLine();
int number = Int32.Parse(input);
if (number > 0)
{
if (number % 2 == 0)
{
WriteLine("0보다 큰 짝수");
}
else
{
WriteLine("0보다 큰 홀수");
}
}
else
{
WriteLine("0보다 작거나 같은 수");
}
}
static public void Switch()
{
Write("요일을 입력하세요.(일, 월, 화, 수, 목, 금, 토) : ");
string day = ReadLine();
switch (day)
{
case "일":
WriteLine("Sunday");
break;
case "월":
WriteLine("Monday");
break;
case "화":
WriteLine("Tuesday");
break;
case "수":
WriteLine("Wednesday");
break;
case "목":
WriteLine("Thursday");
break;
case "금":
WriteLine("Friday");
break;
case "토":
WriteLine("Saturday");
break;
default:
WriteLine($"{day}는(은) 요일이 아닙니다.");
break;
}
}
static public void Switch2()
{
object obj = null;
string s = Console.ReadLine();
if (int.TryParse(s, out int out_i)) // TryParse : 변환의 성공 여부를 반환하기 때문에 코드의 흐름 유지 가능
obj = out_i;
else if (float.TryParse(s, out float out_f)) // out : 출력 전용 매개 변수임을 나타내는 용도로 쓰인다고 알아 놓자.
obj = out_f;
else
obj = s;
switch (obj)
{
case int i:
WriteLine($"{i}는 int 형식입니다.");
break;
case float f when f >= 0: // when : if와 비슷함, 추가적인 조건 검사 수행
WriteLine($"{f}는 양의 float 형식입니다.");
break;
case float f:
WriteLine($"{f}는 음의 float 형식입니다.");
break;
default:
WriteLine($"{obj}는 모르는 형식입니다.");
break;
}
}
static public void While()
{
int i = 10;
while (i > 0)
WriteLine($"i : {i--}");
}
static public void DoWhile()
{
int i = 10;
do
{
WriteLine("a) i : {0}", i--);
}
while (i > 0);
do
{
WriteLine("b) i : {0}", i--);
}
while (i > 0);
}
static public void For()
{
for (int i = 0; i < 5; i++)
{
WriteLine(i);
}
}
static public void ForFor()
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= i; j++)
{
Write("*");
}
WriteLine();
}
}
static public void ForEach()
{
int[] arr = new int[] { 0, 1, 2, 3, 4 };
foreach (int a in arr)
{
WriteLine(a);
}
}
static public void InfiniteFor()
{
int i = 0;
for (; ; )
{
WriteLine(i++);
}
}
static public void InfiniteWhile()
{
int i = 0;
while (true)
{
WriteLine(i++);
}
}
static public void Break()
{
while (true)
{
Write("계속할까요?(예/아니오) : ");
string answer = ReadLine();
if (answer == "아니오")
{
break;
}
}
}
static public void Continue()
{
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
continue;
}
WriteLine($"{i} : 홀수");
}
}
static public void Goto()
{
Write("종료 조건(숫자)를 입력하세요 : ");
string input = ReadLine();
int input_number = Convert.ToInt32(input);
int exit_number = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (int k = 0; k < 3; k++)
{
if (exit_number++ == input_number)
{
goto EXIT_FOR;
}
WriteLine(exit_number);
}
}
}
goto EXIT_PROGRAM;
EXIT_FOR:
WriteLine("\nExit nested for...");
EXIT_PROGRAM:
WriteLine("Exit program...");
}
private void Practice()
{
/*
1. 다음과 같은 결과를 출력하는 프로그램 for문으로 작성
*
**
***
****
*****
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < i; j++)
{
Write("*");
}
WriteLine();
}
2. 다음과 같은 결과를 출력하는 프로그램 for문으로 작성
*****
****
***
**
*
for(int i = 0; i < 5; i++)
{
for(int j = 5; j > i; j--)
{
Write("*");
}
WriteLine();
}
3. 1번과 2번을 for문 대신 do~while 문으로 각각 작성
(1)
int i = 1;
int j = 0;
do
{
do
{
Write("*");
j++;
} while(j < i);
j = 0;
WriteLine();
i++;
} while(i < 6);
(2)
int i = 0;
int j = 5;
do
{
do
{
Write("*");
j--;
} while(i < j);
j = 5;
WriteLine();
i++;
} while(i < 5);
4. 사용자로부터 입력받은 횟수만큼 별 반복 출력. 단, 입력 수 0보다 작거나 같을 경우 "0보다 같거나 작은 숫자는 사용할 수 없습니다"
라는 메세지 띄우고 프로그램 종료
Console.Write("반복 횟수를 입력하세요 : ");
string input = Console.ReadLine();
int ChangeInt = Convert.ToInt32(input);
for (int i = 0; i <= ChangeInt; i++)
{
if (ChangeInt <= 0)
{
Console.WriteLine("0보다 작거나 같은 수는 입력할 수 없습니다.");
}
for (int j = 0; j < i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
*/
}
}
}
/* 메모장
I. 분기문 : 프로그램의 흐름을 조건에 따라 여러 갈래로 나누는 흐름 제어 구문
1. if ~ else 문
+ 중첩 if 문
2. switch 문
II. 반복문 : 특정 조건을 만족하는 동안 코드를 계속 반복해서 실행하도록 하는 문장
1. while 문
while(조건식)
{
실행할 문장
}
-> 조건식이 true인 동안 실행
2. do ~ while 문
do
{
실행할 문장
}while(조건식);
-> 무조건 처음 한번은 실행
3. for 문
for(초기식; 조건식; 증감식;)
{
실행할 문장
}
4. 중첩 for 문
for(초기식; 초건식; 증감식;)
{
for(초기식; 초건식; 증감식;)
{
실행할 문장
}
실행할 문장
}
5. foreach 문
foreach(자료형 변수명 in 배열 또는 컬렉션)
{
실행할 문장
}
III. 점프문
1. break
- 현재 실행중인 반복문이나 switch문의 실행 중단
2. continue
- 한 회 건너 뛰어 반복을 계속 수행함
3. goto
goto 레이블;
레이블 :
이어지는 코드
*/