-
Notifications
You must be signed in to change notification settings - Fork 8
/
FlowControl.cs
208 lines (181 loc) · 4.96 KB
/
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
namespace BasicCoding;
internal static class FlowControl
{
internal static void SimpleIf(int age)
{
if (age < 18)
{
Console.WriteLine("You are too young to buy alcohol in a bar in the UK.");
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Style",
"IDE0011:Add braces",
Justification = "This example deliberately shows the wrong way to do it")]
internal static void BadIf(bool authenticationCodesCorrect)
{
if (authenticationCodesCorrect)
SendTransferConfirmation();
TransferFunds();
}
internal static void IfAndElse(bool optimistic)
{
if (optimistic)
{
Console.WriteLine("Glass half full");
}
else
{
Console.WriteLine("Glass half empty");
}
}
internal static void MultipleElses(double temperatureInCelsius)
{
if (temperatureInCelsius < 52)
{
Console.WriteLine("Too cold");
}
else if (temperatureInCelsius > 58)
{
Console.WriteLine("Too hot");
}
else
{
Console.WriteLine("Just right");
}
}
internal static void MultipleElsesLotsOfBlocks(double temperatureInCelsius)
{
if (temperatureInCelsius < 52)
{
Console.WriteLine("Too cold");
}
else
{
if (temperatureInCelsius > 58)
{
Console.WriteLine("Too hot");
}
else
{
Console.WriteLine("Just right");
}
}
}
internal static void SwitchWithStrings(string workStatus)
{
switch (workStatus)
{
case "ManagerInRoom":
WorkDiligently();
break;
case "HaveNonUrgentDeadline":
case "HaveImminentDeadline":
CheckTwitter();
CheckEmail();
CheckTwitter();
ContemplateGettingOnWithSomeWork();
CheckTwitter();
CheckTwitter();
break;
case "DeadlineOvershot":
WorkFuriously();
break;
default:
CheckTwitter();
CheckEmail();
break;
}
}
// To see the compiler error for Example 70, change this:
#if false
// to:
// #if true
internal static void IllegalSwitchFallThrough(string x)
{
switch (x)
{
case "One":
Console.WriteLine("One");
case "Two": // This line will not compile
Console.WriteLine("One or two");
break;
}
}
#endif
internal static void SwitchFallThrough(string x)
{
switch (x)
{
case "One":
Console.WriteLine("One");
goto case "Two";
case "Two":
Console.WriteLine("One or two");
break;
}
}
internal static void WhileLoop(StreamReader reader)
{
while (!reader.EndOfStream)
{
Console.WriteLine(reader.ReadLine());
}
}
internal static void DoLoop()
{
char k;
do
{
Console.WriteLine("Press x to exit");
k = Console.ReadKey().KeyChar;
}
while (k != 'x');
}
internal static void ForLoops(int[] myArray)
{
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] *= 2;
}
for (int i = 0, j = 0; i < myArray.Length; i++, j++)
{
myArray[i] = myArray[j] / 2;
}
}
internal static void NestedLoop(int[,] myArray)
{
int height = myArray.GetLength(0);
int width = myArray.GetLength(1);
for (int j = 0; j < height; ++j)
{
for (int i = 0; i < width; ++i)
{
Console.WriteLine($"{i},{j}: {myArray[i, j]}");
}
}
}
internal static void ForeachLoops()
{
string[] messages = GetMessagesFromSomewhere();
foreach (string message in messages)
{
Console.WriteLine(message);
}
static string[] GetMessagesFromSomewhere() => new[] { "Hello", "world" };
}
public static void ShowMessages(IEnumerable<string> messages)
{
foreach (string message in messages)
{
Console.WriteLine(message);
}
}
private static void SendTransferConfirmation() => Console.WriteLine("Sending transfer confirmation");
private static void TransferFunds() => Console.WriteLine("Transferring funds");
private static void WorkDiligently() => Console.WriteLine("Makes Jack a dull boy");
private static void CheckTwitter() => Console.WriteLine("Refresh");
private static void CheckEmail() => Console.WriteLine("Scroll");
private static void ContemplateGettingOnWithSomeWork() => Console.WriteLine("Sigh");
private static void WorkFuriously() => Console.WriteLine("Here's Johnny!");
}