GNU/Linux Programmer's Solver - Conditionals in C#

Guess program in C#. Conditionals, if, switch and constants
PROGRAMMING GUESS C#

Condicionales en C#

using System;

class Program {
    static void Main () {
        const int n = 42;
    
        Console.Write ("Guess the number ? ");
        int v = Int32.Parse(Console.ReadLine ());
    
        if (v < 0) {
            Console.WriteLine ("Only positive numbers");
            return;
        }
        if (v == n) Console.WriteLine ("Well done!!!");
        else {
            Console.WriteLine ("Number is " +  ((v < n) ? "HIGHER" : "LOWER"));
            Console.Write ("Hint : ");
            int h = (v > n) ? v - n : n - v;
            if      (h < 5)  Console.WriteLine (" TOO HOT!");
            else if (h < 10) Console.WriteLine (" HOT");
            else if (h < 15) Console.WriteLine (" WARM");
            else             Console.WriteLine (" COLD");
        }
    }
}

C# won’t surprise us in the realm of conditional expressions. As expected, it offers, more or less, the same possibilities as Java and a model based on the one introduced by the C language, including the ternary operator ?: and if-else blocks without using the then keyword.

DID YOU KNOW

C# is very similar to Java, but it’s full of small silly differences, perhaps to give the impression that the languages don’t look so similar. One of these differences has to do with the standard way to name methods. Java follows the C++ style in which camel case is used for names, but the first letter is uppercase for classes and lowercase for methods. C# uses an uppercase letter also for methods… which makes it uncomfortable to switch languages once you get used to each identifier format.

C# offers a switch statement with the same syntax as C, but with some extensions such as allowing defining cases with different types. Additionally, it offers a when clause with which to add additional conditions on the cases. This is especially useful for code that works with heterogeneous collections (e.g., arrays of objects of different types).

The following code shows a classic example of this usage.


Shape sh = new Circle (10);

switch (sh) {
    case null:
        Console.WriteLine ("Non Initialized Object");
        break;
    case Shape shape when sh.Area == 0:
        Console.WriteLine ("Wrong Shape");
        break;
    case Circle c when c.radious == 1:
        Console.WriteLine ("Unit Circle....");
        break;
    default:
        Console.WriteLine ("Default");
        break;
}

As of writing this text, C# does not support the use of switch or if as an expression, although it appears that this is considered a proposal for future versions. Similarly, the use of relational expressions or patterns in the cases of switch is also likely to be seen very soon. Perhaps when you’re reading this, these improvements have already been integrated.

C# offers boolean types (type bool) that can take values true and false, which cannot be automatically converted to other types as happens in C or C++. The equivalent of that conversion, which C/C++ performs based on the convention that the value 0 is false and any other value is true, can be simulated in C# with a comparison with the value 0.

C# also supports the declaration of constants using the keyword const similarly to other languages, although this construction can only be used with basic types. Again, it is very likely that we will see support for declaring constants using complex types in future versions of the language, but as of writing this text, it is not yet possible.

Summary

  • Supports if ... [else if] ...[else] ...
  • Doesn’t offer a reduced form for if
  • Supports the ternary operator cond ? val1 : val2
  • Supports booleans: bool (offers the Boolean class)
  • Supports switch improved with support for using types and adding additional conditions with when
  • Supports constants natively (const Tipo CONSTANT = val) only with basic types. readonly can be used with complex types (classes, structures, or vectors).

Related Posts

GNU/Linux Programmer's Solver - Introduction

Programming exercises in 18 Programming Languages

Read

linuxprogramming

GNU/Linux Programmer's Solver - Hello World in C#

Hello World program in C#. Basic structure, comments and string literals

Read

PROGRAMMING C# HELLO WORLD

GNU/Linux Programmer's Solver
Chapter 2 - Conditionals

Conditionals are the may way to add logic to your programs.

Read

PROGRAMMING HELLO WORLD

Return to Home Page