So far we have covered few basic operators and now would be good time to expand our knowledge of them. All of the operators will be tested using if statements:
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 |
public static void Main() { // ! - The logical negation operator bool boolValue = false; if (!boolValue) Console.WriteLine("boolValue is equal to false"); Console.WriteLine(""); // ++ - The increment operator // increments valuue by 1 // result depends on wich side of // variable is ++ operator placed double firstDoubleValue = 2.3; double secondDoubleValue = 7.6; Console.WriteLine(++firstDoubleValue); Console.WriteLine(firstDoubleValue); Console.WriteLine(""); Console.WriteLine(secondDoubleValue++); Console.WriteLine(secondDoubleValue); Console.WriteLine(""); // -- - the decrement operator // same as the increment operator // but this one subtracts value Console.WriteLine(--firstDoubleValue); Console.WriteLine(firstDoubleValue); Console.WriteLine(""); Console.WriteLine(secondDoubleValue--); Console.WriteLine(secondDoubleValue); Console.WriteLine(""); // < - less than operator if (firstDoubleValue < secondDoubleValue) Console.WriteLine("firstDoubleValue is lesser value"); Console.WriteLine(""); // > - more than operator if (firstDoubleValue < secondDoubleValue) Console.WriteLine("secondDoubleValue is lesser value"); Console.WriteLine(""); // > - more than operator int firstInt = 5; int secoundInt = 5; if (firstInt <= secoundInt) Console.WriteLine("both values are the same"); Console.WriteLine(""); if (firstInt >= secondDoubleValue) { } // no value will be printed // equal operator if (firstInt == secoundInt) Console.WriteLine("both values are the same"); Console.WriteLine(""); // not equal operator if (firstDoubleValue == secondDoubleValue) Console.WriteLine("both values are not the same"); Console.WriteLine(""); // & - logical And operator // checks if both values are true bool firstBool = true; bool secondBool = false; if (firstBool == true & secondBool == false) Console.WriteLine("both values have expected values"); Console.WriteLine(""); // && - conditional And operator // checks first value and proceeds to second only if first is false if (firstBool == false & secondBool == false) { } // no output will be written because first value is not as expected //and second value was not even inspected // | - logical or operator // checks all variables if atleast one is true if (firstBool == false | secondBool == false) Console.WriteLine("at least one variable has met our expectations"); Console.WriteLine(""); // || - conditional Or operator // checks first value and proceeds to second only if first is false if (firstBool == true || secondBool == false) Console.WriteLine("at-least one variable has met our expectations "); Console.WriteLine(""); // ?? null-coalescing operator // returns right value if left is null // or returns left value if left value is not null string leftValue1 = null; string rightValue1 = "right value"; string result1 = leftValue1 ?? rightValue1; Console.WriteLine("{0} is a result",result1); Console.WriteLine(""); string leftValue2 = "left value"; string rightValue2 = null; string result2 = leftValue2 ?? rightValue2; Console.WriteLine("{0} is a result", result2); Console.WriteLine(""); // x ? a : b // if x is true, return a // if x is false, return b int x = 88; int intResult = (x != 58) ? 10 : 11; Console.WriteLine("{0} is a result", intResult.ToString()); Console.WriteLine(""); } |
Certainly there are some more operators, but we will explain them as we go!