O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.2. Conditional statements using the if and else keywords

The 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 can omit the curly braces if there is only one statement within your conditional block:

     31: if (i==0) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required