Moving on to C# 3: Conditions

Conditions in C#

This tutorial will be fairly short because there isn't much to say about conditions that you don't already know if you've worked through the Small Basic tutorial. It's really just a matter of syntax.

I won't be talking about the goto statement, though, because there isn't one in C#.

So before you get coding, open a new project, select the new project type that you created and give a new name.

if... else...

The concept of the if statement in C# is the same as in Small Basic but it looks like this:

   if (expression){
    statement
   }  
   else
   {
    statement
   }


As with SB the else part is optional. You'll also notice that C# uses all lower case characters. Here's the usual example converted from the SB tutorial.

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using Microsoft.SmallBasic.Library;

   namespace CSconditions
   {
       class Program
       {
           static void Main()
           {
               if (Clock.Hour < 12)
               {
                   TextWindow.WriteLine("Good Morning World");
               }
               else
               {
                   TextWindow.WriteLine("Good Evening World");
               }
               TextWindow.Read();
           }
       }
   }
 

Let's compare the C# version and SB. First, the expression in the brackets after the 'if' is the same; it's a boolean expression that has the value true or false. If it's true the code in the if section is executed and if it's false the the code in the else part is executed.

Now, note that there is no EndIf statement in C#. The sections of code are enclosed in braces ({})*. The first set of braces contain the code for the true condition and the second ones contain the code for the false condition.

Here's another example which highlights another difference.

  TextWindow.Write("Enter a number: ");
  int num = TextWindow.ReadNumber();
  int remainder = Microsoft.SmallBasic.Library.Math.Remainder(num, 2);

  if (remainder == 0)
  {
     TextWindow.WriteLine("The number is Even");
  }
  else
  {
     TextWindow.WriteLine("The number is Odd");
  }
  TextWindow.Read();

Look at the conditional expression. The operator that is used in C# to test for equality is a double equals (==) not a single one as in SB (I'll give a list of all operators below). Another thing to note, is the assignment of the remainder; I've had to use the full name for the Maths library because it clashes with a library of the same name in C#. This is a nuisance and we'll see it in further examples but it not really a big problem.

 OK, that's about it for this tutorial. We'll move on to loops next time.

Conditional operator list

< less then
> more than
<= less than or equal to
>= more than or equal to
== equal
!= not equal


* Strictly speaking the if there is only one line of code, the braces are not required, but it does no harm to always put them in.

Comments

Popular posts from this blog

Just Enough Python

Learn to code with C

Introduction to Python by Andrew Ng