11.2 Conditional statements using the if and else keywordsThe use of these keywords in conditional statements is identical in Java and C#. There follows some examples for completeness. Feel free to skip this section if you are already familiar with writing conditional statements using these keywords in Java. Below is an example usage of if and else in conditional statements. It is self-explanatory. 30: public static void ConditionalDemo(int i){ 31: if (i==0){ 32: Console.WriteLine("is a zero"); 33: } 34: else if (i==1){ 35: Console.WriteLine("is a one"); 36: } 37: else { 38: Console.WriteLine("is not a zero or one"); 39: } 40: } Like Java
You may have seen the use of #if and #else in C# (or C/C++ codes) “ these special keywords which start with a hex sign are called preprocessor directives, and are used only by the compiler during compilation. Preprocessor directives are not converted into IL code during compilation (see Chapter 24). |