-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.cs
More file actions
312 lines (249 loc) · 12.7 KB
/
Program.cs
File metadata and controls
312 lines (249 loc) · 12.7 KB
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
using System;
namespace _4Operators
{
class Program
{
static void Main(string[] args)
{
//OPERATORS
//Operators in C# are often symbols or groups of symbols
//that operate on one or more values.
int total = 5 * 5; //This line has two operators;
//the * (multiplication) operator
//which takes 5 and 5 as inputs
//and the = (assignment) operator
//which assigns the result of 5*5 to the variable total
Console.WriteLine(total);
//ASSIGNMENT AND EQUALITY OPERATORS
Console.WriteLine("-----------------Assignment and Equality Operators-------------------");
//ASSIGNMENT (=)
Console.WriteLine("---------------------Assignment (=)-----------------------");
//The assignment operator = assigns a value to a variable, property, etc.
int year = 2020;
Console.WriteLine(year); //2020
//Unlike other operators, the assignment operator is right-associative
int year1;
int year2;
int year3 = 2020;
year1 = year2 = year3; //Will be evaluated as year1 = (year2 = year3)
Console.WriteLine("year2 is " + year2);
//EQUALITY (==)
Console.WriteLine("---------------------Equality (==)-----------------------");
//The equality operator == returns a boolean value stating whether or not
//two values are equal.
int five = 5;
int otherFive = 5;
bool areEqual = five == otherFive;
Console.WriteLine(areEqual);
//Note that, for reference types, the equality operator == only
//returns true if the two reference types are pointing
//at the same object.
var myClass = new MyClass();
var myOtherClass = new MyClass();
var myThirdClass = myClass;
Console.WriteLine(myClass == myOtherClass); //false
Console.WriteLine(myOtherClass == myThirdClass); //true
//INEQUALITY (!=)
Console.WriteLine("---------------------Inequality (!=)-----------------------");
//We can check if two values are not equal using the inequality operator !=
var myMoney = 6.54M;
var theirMoney = 4.65M;
Console.WriteLine(myMoney != theirMoney); //true
//INCREMENT ASSIGNMENT (+=) AND DECREMENT ASSIGNMENT (-=)
//When working with number types, we can increment or decrement
//their value using the increment assignment operators += and -=
Console.WriteLine("---------------------Increment Assignment (+=)-----------------------");
var i = 16;
i += 5;
Console.WriteLine(i); //21
Console.WriteLine("---------------------Decrement Assignment (-=)-----------------------");
var j = 61;
j -= 15;
Console.WriteLine(j); //46
//NULL-COALESCING ASSIGNMENT (??=)
Console.WriteLine("---------------------Null-Coalescing Assignment (??=)-----------------------");
//There's also the null-coalescing assignment operator ??=, which assigns
//the value to the left-hand operand if and only if the left-hand operand is null
int? startNum = 5;
startNum ??= 3; //startNum is 5
Console.WriteLine(startNum);
startNum = null;
startNum ??= 3; //startNum is 3
Console.WriteLine(startNum);
//MATH OPERATORS
//BASIC MATH
//ADDITION (+)
//SUBTRACTION (-)
//MULTIPLICATION (*)
//DIVISION (/)
//The basic math operators +, -, *, / perform simple math operations
Console.WriteLine("---------------------Basic Math (+, -, *, /)-----------------------");
var sum = 5 + 9; //14
Console.WriteLine("Sum: " + sum);
var difference = 56 - 14; //42
Console.WriteLine("Difference: " + difference);
var product = 6 * 6; //36
Console.WriteLine("Product: " + product);
var quotient = 42 / 7; //6
Console.WriteLine("Quotient: " + quotient);
//REMAINDER (%)
Console.WriteLine("---------------------Remainder (%)-----------------------");
//The remainder operator % (AKA modulus) will output
//the remainder from a division operation.
var modulus = 43 % 5; //3
Console.WriteLine(modulus);
//INCREMENT (++) AND DECREMENT (--)
//The increment ++ and decrement -- operators change the value of
//a numeric object by 1.
Console.WriteLine("-----------Increment (++) and Decrement (--)-------------");
var value = 3;
value++;
Console.WriteLine(value); //4
value--;
Console.WriteLine(value--); //3
//Postfixed increment operators show the value *before* the operation occurs
value = 3;
Console.WriteLine("-----------Postfixed Increment-------------");
Console.WriteLine(value); //3
Console.WriteLine(value++); //3
Console.WriteLine(value); //4
//Prefixed increment operators show the value *after* the operation occurs
value = 3;
Console.WriteLine("-----------Prefixed Increment-------------");
Console.WriteLine(value); //3
Console.WriteLine(++value); //4
Console.WriteLine(value); //4
//ORDER OF OPERATIONS
Console.WriteLine("---------------------Order of Operations-----------------------");
//Math operators follow an order of operations.
//First, increment and decrement
//Then, multiply, divide, and remainder
//Then addition or subtraction
var output = 5 + 2 * 9; //Evaluated as 5 + (2 * 9) = 19
Console.WriteLine(output);
var output2 = (5 + 2) * 9; //63
Console.WriteLine(output2);
var output3 = 15 / 5 * 3; //Evaluated as (15 / 5) * 3 = 9
Console.WriteLine(output3);
var output4 = 15 / (5 * 3); //1
Console.WriteLine(output4);
//BOOLEAN LOGIC OPERATORS
Console.WriteLine("-----------------Boolean Logic Operators-------------------");
//When dealing with boolean values, we use a set of operators
//design to work on boolean logic.
//Negation (!)
Console.WriteLine("---------------------Negation (!)-----------------------");
var isTrue = true;
Console.WriteLine(!isTrue); //false
var isFalse = false;
if (!isFalse)
{
Console.WriteLine("The expression is false!");
}
//Conditional Logical AND (&&)
Console.WriteLine("---------------Conditional Logical AND (&&)-----------------");
//AND operations return TRUE if both operands are TRUE; otherwise, they return FALSE
//The conditional logical AND will evaluate the first operand; if it is FALSE, it returns FALSE
//WITHOUT evaluating the second operand.
var hasName = true;
var hasAddress = true;
var isValidCustomer = hasName && hasAddress; //true
Console.WriteLine(isValidCustomer);
hasName = false;
var isStillValidCustomer = hasName && hasAddress; //false
Console.WriteLine(isStillValidCustomer);
//Conditional Logical OR (||)
Console.WriteLine("---------------Conditional Logical OR (||)-----------------");
//Logical OR operations return TRUE if one or more of the operands is TRUE.
//If all operands are FALSE, logical OR will return FALSE.
var hasPhone = false;
var hasEmail = true;
var hasValidContactInfo = hasPhone && hasEmail; //true
Console.WriteLine(hasValidContactInfo);
var areYouSure = hasEmail && hasPhone; //true, hasPhone not evaluated
Console.WriteLine(areYouSure);
//Logical AND (&)
Console.WriteLine("---------------------Logical AND (&)-----------------------");
//This operator will always evaluate both operands.
var hasLastName = true;
var hasFirstName = false;
var hasCompleteName = hasFirstName & hasLastName; //false
Console.WriteLine(hasCompleteName);
//Logical OR (|)
Console.WriteLine("---------------------Logical OR (|)-----------------------");
//This operator will always evaluate both operands.
hasLastName = true;
hasFirstName = false;
var hasAnyName = hasFirstName | hasLastName; //true
//Logical Exclusive OR (^)
Console.WriteLine("------------------Logical Exclusive-OR (^)--------------------");
//An exclusive-OR operation returns true if and only if one operand is true and one operand is false.
//If both operands are true or both operands are false, exclusive-OR returns false.
var isXOR = true ^ true; //false
Console.WriteLine(isXOR);
isXOR = true ^ false; //true
Console.WriteLine(isXOR);
isXOR = false ^ true; //true
Console.WriteLine(isXOR);
isXOR = false ^ false; //false
Console.WriteLine(isXOR);
//Boolean logic operators execute in this order: !, &, ^, |, &&, ||
Console.WriteLine("-----------Boolean Logic Order of Operations-------------");
var isTest = true ^ false & true; //Same as true ^ (false & true), result true
Console.WriteLine(isTest);
var isOtherTest = false || (false ^ true && true); //true
Console.WriteLine(isOtherTest);
//COMPARISON OPERATORS
Console.WriteLine("---------------------Comparison Operators-----------------------");
//Less Than (<) and Less Than Or Equal To (<=)
Console.WriteLine("---------Less Than (<) and Less Than Or Equal To (<=)-----------");
bool isLessThan = 7 < 9; //true
Console.WriteLine(isLessThan);
isLessThan = 9 < 7; //false
Console.WriteLine(isLessThan);
int otherValue = 6;
otherValue++;
bool isLessThanOrEqual = otherValue <= 7; //true
Console.WriteLine(isLessThanOrEqual);
//Greater Than (>) and Greater Than Or Equal (>=)
Console.WriteLine("---------Greater Than (>) and Greater Than Or Equal To (>=)-----------");
bool isGreaterThan = 18 > 15; //true
Console.WriteLine(isGreaterThan);
isGreaterThan = -7 > -10; //true
Console.WriteLine(isGreaterThan);
int testValue = 88;
bool isLarge = testValue >= 50; //true
Console.WriteLine(isLarge);
//CONDITIONAL OPERATOR (?:)
Console.WriteLine("-------------Conditional (?:)---------------");
//The conditional operator ?: returns one of two values based on
//whether a given expression is true or false.
isTrue = true;
string message = isTrue ? "Yay!" : "Boo..."; //message is "Yay!"
Console.WriteLine(message);
isFalse = false;
message = isFalse ? "Yay!" : "Boo..."; //message is "Boo..."
Console.WriteLine(message);
//It is possible to combine this operator multiple times with itself,
//though this often results in hard-to-read code.
hasName = false;
hasAddress = false;
hasPhone = true;
message = hasName ? "Welcome!" :
hasAddress ? "Hiya!" :
hasPhone ? "Thanks for calling!" : "Whoops.";
Console.WriteLine(message); //"Thanks for calling!"
//NULL-COALESCING OPERATOR (??)
Console.WriteLine("-------------Null-Coalescing (??)---------------");
//The null coalescing operator ?? returns the value of the
//left-hand operand if that value is not null; otherwise,
//it evaluates the right-hand operand and returns that.
int? val = null;
int counter = val ?? 1; //counter is 1
Console.WriteLine(counter);
int? otherVal = 5;
counter = otherVal ?? 1; //counter is 5
Console.WriteLine(counter);
}
}
}