Monday, 12 June 2017

Compare C# and Scalar Programming Languages - Part 1

This is an assignment given to MSc. Students of the University of Port Harcourt as well as the Rivers State University of Science and Technology here in Nigeria.

We are going to make the comparison based on the sub-topics listed below and then we would also look at the similarities and differences.

Also to be examined, in the second part is Object Oriented Programing Concepts.
  1. Background
  2. Assignment Statements and Expressions
  3. Data Types
  4. Relational Operators
  5. Conditional Control Structure
  6. Looping Constructs
  7. String Manipulation
  8. Functions and Procedures
  9. Array Declaration and Manipulation
  10. Lists and Pointers
  11. Sets
  12. Polymorphism
  13. Summary and Final Notes

1. Background

Here we would Examine the background and history of both programming languages

Background of C#
The C# Programing Language was designed and developed by Microsoft  with the development team led by a Computer Programmer named Anders Hejlsberg. The first release of C# appeared in the year 2000 but as of then was not very stable.
C# is a multi-paradigm programming language. This means that the language can be has features of an Object Oriented Programming Language while at the same time can be used as a Functional or Structural Language as well as Event-Driven language.
The development of C# was influenced by Languages such as Java, C and C++.
The latest version of C# as of the time of this article is version 6 with was released in 2015.
C# files have the extension .cs.

Background of Scala
The Scala Programing Language just like C# is also a multi-paradigm programming language and therefore exhibit features of both OOP languages and functional languages.
Scala was designed by a Computer Scientist named Odersky and developed by Programming Methods Lab.
The Scala programming language source code was designed to be compiled into Java Bytecodes and therefore can be run on a Java Virtual Machine(JVM). This is why Scala has a strong similarity with Java.
The first release of Scala appeared in the year 2004. Although having similarity with Java, it is influenced by such languages as Eiffel, Lisp and SmallTalk.
The file extension for Scala is .sc or .scala.


2 Assignment Statement and Expressions

Here we would examine assignment statement in both languages and also look at code fragments that illustrate this

Assignment Statements in C#
Before we discuss assignment statements in C#, it is necessary to note that  the C# language implements strong and static data types. This means that the variables must be declared before they can be assigned values.
Remember that in the compilation process, data types (for languages that support strong data typing) are checked during the semantic analysis phase.

In C#, variables are declared with the syntax:

<datatype> variable;

So if we want to declare a variable called total as integer, we would say:

int total;

Don't forget that lines of codes in C# is terminated by a semicolon(;).
After a variable is declared,  a value can be assigned using the assignment operator(=). The syntax is as follows:

<variable_name> = <value>

This reads as, "variable_name gets value"

Not "variable equals value" (this is important to know!)

An example would be when we assign a value of 15 to a variable named sum, the assignment statement would be:

sum = 15;

We can also declare a variable and assign it a value in the same line, thus:

int sum = 15;


Assignment Statement in Scala
The Scala Language has different syntax fro declaring and assigning variables. In Scala, the var keyword is used to declare a new variable. The format is:

var MyName: String;

This declares a variable MyName as s string type. Also note that the line is terminated with a semicolon just like in C#.
You can assign a value to a variable using the assignment operator (=). YOu can also declare a variable and assign a value to it in the same line. The examples below are valid assignment statements in Scala:

var MyName: String = "The Genius";

var Number : int;
Number = 56;

The first line declares a variable MyName as a string type and assigns it a value of "The Genius"
The second line declares a variable Number as integer
The third line, assigns a value of 56 to the Number variable


3. Data Types

Data types are part of every programming language, so we are going to examine the data types available in both C# and Scala.

Data Types in C#
The data stored in the computer memory can be of different types. For example, someone's age is stored as a numeric value and his address stored as alphanumeric characters made up of both numbers and alphabets. C# has various standard data types that are used to define the operations possible on them and the storage method for each of them.
Just as I mentioned before, C# supports strong data types, which means that the variables must be declared  as the appropriate type before they can be assigned a value. Let's outline a few of the most common data types in C#.

The table below shows the data types in C#


We also have Date data type and String data type that would be discussed under String Manipulation

Data Types in Scala
The Scala Programming Language shares the same data types with Java as well as the same precision. All data types in Scala are objects as opposed to basic types. This means that you cal actually call methods on them. More of this under Functions and Procedures.
The most common Scala data types are given below
  • Byte: 8 bits signed value. From -128 to 127
  • Short: 16 bit signed value. From -32768 to 32767
  • Int: 32 bit signed value
  • Long: 64 bit signed value
  • Float: 32 bit
  • Double: 64 bit
  • Char:16 bit
  • String: Sequence of chars
  • Boolean: True or False value



4. Relational Operators

Relational Operators in C#
These are operators that compare the value on either sides of an expression and determine the relation  between them. Below is a list of relational operators supported by C#

= = (Equal)
Checks if the values of the two operands are equal. If yes, the condition evaluates to True, else False

!= ( Not Equal)
Checks if the values of the two operands are equal or not equal, if the values are not equal, then the expression evaluates to true

> (Greater Than)
Checks if the value of the left variable is greater than that of the right, if yes, then the condition becomes True

< (Less Than)
Checks if the value of the left variable is less than that of the right, if yes, then the condition becomes True

>= (Greater Than or Equal)
Checks if the value of the left variable is greater than or equal to the value of the right operand, if yes, the condition evaluates to True

<= (Less Than or Equal)
Checks if the value of the left variable is less than or equal to the value of the right operand, if yes, the condition evaluates to True

Relational Operators in Scala
The relational operators in Scala is exactly the same as in C#


5. Conditional Control Structure

Conditional Control Structure are programming constructs used for decision-making in a program.
This requires that the programmer should specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statement or statements to be executed if the condition is determined to be false.

This is illustrated by the figure below:



In the flowchart, the first step in a conditional control structure is to test if a condition is true. If the condition is true, a code is executed, if false, that program continues execution in a different path.

Conditional Control Structure in C#
In C#, conditional control/decision making is achieve by the use of if/else statement and switch statement
if statement
This is make up of an expression that evaluates to either True or False, followed by statement to be executed when the expression is true. If the expression evaluates to true, the statement is executed, if not, the statement is skipped.

if/else statement
An if/else statement contains an if part and an else part as well as a conditional expression that evaluates to True or False. If the expression evaluates to True, the if part is executed, if it evaluates to false, the else part is evaluated.

nested if statement
This is a situation where an if statement is used inside another if statement.
The code below illustrates the format of if/else statement in C#

 if(conditional_expression)
{
    //code to be executed is expression is true 
}
else
{
   //code to be executed if expression is false
}


switch statement
A switch statement contains a variable that is examined and bases on the value of the variable, the program execution switches to a particular piece of code to be executed.

nested switch statements
This is used to nest a switch statement inside another switch statement


Conditional Control Structure in Scala
This is achieved using the following code:

if(conditional_expression)
{
    //Statements will execute if expression is true
}

In the above code, the statement inside the curly braces would be executed only if the conditional_expression evaluates to true. If not the code is skipped and the execution continues with the lines of code after the curly braces.
The program fragment below illustrates this further


object Example{
  def main(args: Array[String])
  {
      var x = 50;
      if( x < 100) 
      {
           println("This is the Genius");
       }
    }
}

The result of the above program when run would print out "This is the Genius". The conditional expression would evaluate to True because the variable x which have a value of 50 is less than 100


6. Looping Constructs

Looping provides a way to allow a piece of code to execute a number of times bases on a specified condition.



Looping Constructs in C#
C# Programming language provides the following types of loops to handle looping requirements. So we now examine the various types of loops and their implementation in C#

While Loop
This repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the body of the loop. The syntax for the while loop is a given below:

while (conditional_statement)
{
   //statements to be executed
}


For Loop
This type of loop executes a group of statements multiple times and abbreviates the code that manages the variable is a certain condition is reached based on a loop variable. The syntax for the for loop is given below:

for(init; condition; increment)
{
   //Statements to be executed
}

Do...While Loop
This is similar to the while loop. The difference is that this loop tests the condition at the end of the body of the loop. The format is given below.

do
{
    //statements to be executed

} while(condition);


Nested Loops
This is a loop inside another loop. This is shown below for the while loop:

while (conditional_statement)//outside loop
{
    while (conditional_statement) // nested loop
    {
       //statements to be executed
    }
   //statements to be executed
}

Looping Construct in Scala
This is similar to the looping constructs for C#


7. String Manipulation

A string is a sequence of one or more characters enclosed in double quotes. Examples of string are:

"Kindson Munonye"
"The Tech Pro"
"Akokwa"

String Manipulation in C#
C# provides a number of ways to manipulate strings. The operations that can be used for string manipulation in C# includes the following:
  • Joining of two strings (called Concatenation)
  • Getting the length of a string
  • Getting substring from another string
  • Finding the index of a string inside another string


String Manipulation in Scala
Strings in Scala are similar to strings in Java. A Scala string is an object that cannot be modified. The codes below can be used to create a string in Scala.

var greetings = "Hello World!";
var greetings : String = "Hello World!";

The codes above creates a string object, and in this case, "Hello World".

Scala provides methods known as ancestor methods which are used to obtain information about a string. To get the length of a string (that is the number of characters in the string), we use the length() method.
The code fragment below shows how we can obtain the length of a string, that is the number of characters making up the string.

object Demo
{
   def main(args: Array[String]) 
   {
       var my name = "Kindson The Tech Pro";
       var len = m├íname.length();
       println("The length of the string is " + len);
   }
}

The output of the code would be:
The length of the string is 20

Concatenation is the joining of two string to yield a larger string. The accessor function to achieve this is concat(). The code fragment below illustrates its use

...
String1 = "Kindson";
String2 = "Munonye";
String3 = String1.concat(String2);
...

After this codes executes, String3 would contain KindsonMunonye


8. Functions and Procedures

A function is a group of statements that perform a task. You can divide up you code into separate functions. This functions come together to make up the complete program.

Functions and Procedures in C#
A function in C# allows you to put together a piece of code and call it from other parts of your program. You may encounter a situation where you may have to repeat a piece of code fragment, in multiple parts of your program and this is where functions would be very useful. In C#, functions are basically declared like this:

<visibility> <return_type> <function_name>
(<parameters>)
{
     body_of_function
}

An example of a typical function is given below.

public in Max(int a, int b)
{
       if(a > b)
            return a:
       else
            return b;     
}

Functions and Procedures in Scala
In Scala, there is both functions as well as methods. Methods may also be functions with a little difference. A method in Scala is part of a class that has a name, a definition and some anotations which may be optional
Function on the other hand, is a complete object which can be assigned to a variable. Therefore if a function is defined as a member of some class, it can be called a method of that class.

To declare a function in Scala, use the syntax:

def <function_name> ([argument_list]) : [return_type] =
    function body
    return [expression]
}