欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

JScript .NET Fundamentals

最编程 2024-04-23 10:52:42
...
 

JScript .NET Fundamentals

 

Overview of JScrip .NET

 

Introduction

JScript .NET is a computer programming language developed by Microsoft based on early or previous standards of the JavaScript, then JScript language. The primary and strongest difference, what sets JScript .NET apart is that it is not a simple or regular scripting language. JScript .NET is actually at the same level of, and enjoys the same respect of popular languages like Java, C/C++, or C#. While the previous version of JScript and JavaScript were scripting languages that needed a browser to show their results, JScript .NET is a fully functional and independent language that can be used to create regular executables that can be run on client computers.

JScript .NET can be as loose as JavaScript, that is, it can let you write code that don't rely on known data types. At the same time, you can take advantage of techniques used in strongly typed languages like C/C++, Pascal, C#, etc, making the language "less forgiving". This makes the code easier to read, maintain, and faster to compile.

Types of Applications

The acronym .NET suggests that JScript .NET uses the .NET Framework to take full advantage of the operating system on which it is run. This means that you can create as much a complete and fully functional application using JScript .NET the same way you would with Visual Basic or Visual C#. This also means that, using JScript .NET, you can use the same libraries, the same classes, and the same functions of Visual Basic, Visual C#, and other languages of the .NET Framework family.

With JScript .NET, you can create console applications, graphical (GUI) Windows application, web-based applications (ASP.NET), and many more. This is because well written code in a JScript .NET has direct access to the .NET Framework. A JScript .NET application also enjoys support of the Common Language Specification (CLS), the bottom language standard that other .NET Framework languages follow. This specification provides the use of data types and the interoperability that languages need in order to "exchange" code. This means that code written in JScript .NET can use classes, libraries, and other objects including the contents of namespaces, created from the other CLS compliant languages even if the JScript .NET programmer doesn't know those languages.

Applications Fundamentals

 

The Compiler

To create a JScript .NET application, you start by writing the necessary code. Code for a JScript .NET application is create as normal text in a regular computer file. After writing the necessary code, you save the file in a directory of your choice. The file must have the extension .js.

Code of a JScript .NET application is written in plain English with some words you are familiar with and some other words you will encounter as you move. If you write a regular JScript script, you can open it in a browser or a dialog box (for a Windows Script Host application). If you are creating a console or a graphical user interface (GUI) application that would result in an execute that can be used on other languages without caring for the browser, then you must "translate" your code in a language the computer can understand. As mentioned already, your code is written in English. Unfortunately, the computer doesn't understand it.

To perform this "translation", you must use another program that understands both languages. This program is called a compiler. Fortunately, Microsoft provides a free compiler you can use. The compiler provided by Microsoft for JScript .NET applications is called jsc.exe. To get it, you must download the Microsoft .NET Framework from the Microsoft web site. If you are using Microsoft Windows XP or 2003, the library should be installed already on your computer. If you are using earlier versions of Microsoft Windows, you make need to check or download it, free, from Microsoft. After downloading and installing the library, you are ready to use it.

After installing the .NET Framework, you can create an application in the same directory where the jsc application is installed. If you create a JScript .NET file and save it in another directory, when you attempt to compile your application, you may receive an error. If you want to be able to create a JScript .NET application from any folder of your choice, you can modify the Autoexec.bat or the Environment Path of your application.

 

Practical LearningPractical Learning: Adding the Compiler to the Path

  1. Start Windows Explorer and locate the folder where your jsc application is installed. Here is an example:
     
  2. Click Start -> Control Panel
  3. Double-click System
  4. In the System Properties dialog box, click the Advanced tab and click Environment Variables
  5. In the System Variables section, click Path and click Edit
  6. Press End or get to the end of the string
  7. Type ; followed by the complete path to the compiler. Mine appears as:
     
    %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
  8. Click OK on each dialog box

Application Creation

To create a JScript .NET application, you start with a text file and write your code in it. One of the types of applications you can create is called a console application. A console application is one that displays its result(s) in a DOS window, which is a window with a black background.

In our lessons, we will mostly use the free jsc compiler provided by Microsoft. To write our code, we will use Notepad. To compile our applications, we will use the command prompt.

There are two main operations you will regularly perform as a programmer vis-à-vis your application. The primary operation consists of displaying something on the screen. This task is taken care of by using print(). Based on this, to display a word or sentence on the DOS window, include it in the parentheses of print. If it is a word or a group of words, include it between double-quotes.

 

Practical LearningPractical Learning: Introducing JScript .NET Code

  1. Start Notepad and in it, type print("Welcome to the Wonderful World of JScript .NET");
  2. To save the file, on the main menu, click File -> Save
  3. Select and display the C: drive in the Save In combo box
  4. Click the Create New Folder button Create New Folder
  5. Type JScript Lessons
  6. Press Enter twice or display the new folder in the Save In combo box
  7. Click the Create New Folder button Create New Folder again
  8. Type Exercise1 and press Enter twice or display the new folder in the Save In combo box
  9. Save the file as exercise.js
     
    Save As
  10. Click Save

Application Compilation

After creating a file that holds the code of a JScript .NET application, you can prepare it for distribution. A regular script is a simple text-base file that can be interpreted by a browser. If you create such a script, you can save it to display and make it available to other people who would simply open it in their browser.

If you have created a JScript .NET application and want to distribute but you are not concerned whether your clients have a browser or not, you must create an executable. To do this, you must compile your application. The only way you can create an executable from a JScript .NET application is through the command prompt. The steps used to open the command prompt depend on your operating system. In Windows XP and Windows Server 2003, to open a command prompt, on the Taskbar, you can click Start -> All Programs -> Accessories -> Command Prompt.

To compile a program on the Command Prompt, first change to the directory that contains your script file. To proceed, invoke the name of your compiler, in this case jsc, followed by the name of the script file wit its extension, and press Enter. If there are errors, the compiler will let you know. You can then return to the file, fix the problem, and compile again, until all problems have been corrected.

After compiling the program, an executable application, that has the same name as the script that was compiled, would be created.

Practical LearningPractical Learning: Compiling an Application

  1. To test the application, open the Command Prompt and change to the folder in which you created the JScript .NET file:
     
    DOS Window
  2. Type jsc exercise.js and press Enter

Application Execution

After creating an executable application, you can distribute it to other people. An executable of a JScript .NET application is a fairly small application. You can save it to disc and then, either place if where people can first download it or send it in a disk or other portable medium (never email an executable).

Executing your application is equivalent to using it. To execute an application that you have just created, at the Command Prompt where you compiled it, you can type the name of the executable (your clients can double click the executable to open the application.

Practical LearningPractical Learning: Executing an Application

  1. When the program has finished compiling, type exercise and press Enter
     
    DOS Window
  2. Type Exit to close the DOS window

A Custom Name for an Executable

As mentioned above, if you compile your application by simply invoking the name of the script file that contains the code, an executable with the same name as the file would be created. Fortunately, when compiling the file, you can specify the name you want for the executable. The formula to follow is:

jsc /out:ApplicationName File.js

In this formula, you must first call the compiler as done previously. It must be followed by /out: which indicates that you are going to specify the name of the executable. After the /out: factor, type the desired name that will hold the name of the new application instead of the name of the file. Lastly, type the name of the file that contains the script and make sure you append its extension.

 

Custom Libraries

A library is a program that contains additional information that other programs can use. The information in the library can be almost anything. Experience will guide you as to what you can put in it.

A library is created with the same approach as the programs we have used so far. This means that you start from a script file that has the .js extension and that contains the code you decided to put in it. To actually create the library, you compile it using the following formula:

jsc /target:library file.js

In this case, the only thing different in the formula is the file.js, which is the name of the file that contains the necessary code. After compiling the code, a new application (yes, it is an application) is created but it has the .dll extension instead of .exe as do the regular applications we have created so far.

Accessories for Coding

 

Comments

A comment is a line or paragraph of text that the compiler would not consider when examining the code of a program. There are two types of comments recognized by C#.

To display a comment on a line of text, start the line with two forward slashes //. Anything on the right side of // would be ignored. Here is an example:

// This line will be ignored. I can write in it anything I want

The above type of comment is used on only one line. You can also start a comment with /*. This type of comment ends with */. Anything between this combination of /* and */ would not be read by the compiler. Therefore, you can use this technique to span a comment on more than one line.

Escape Sequences

An escape sequence is a special character that displays non-visibly. For example, you can use this type of character to indicate the end of line, that is, to ask the program to continue on the next line. An escape sequence is represented by a backslash character, \, followed by another character or symbol. For example, the escape sequence that moves to the next line is \n.

An escape can be included in single-quotes as in '\n'. It can also be provided in double-quotes as "\n".

JScript.NET recognizes other escape sequences.


Variables

 

Overview of Variables

 

Introduction

A typical program uses various and different values. Some are used for display to the user. Some are requested from the user. To manage these various values, the compiler reserves some sections of the computer memory to store them. Because the compiler cannot predict the types of values that a program would need, it leaves it up to the programmer to decide.

One of the instructions you will give to the compiler when creating a program is to specify how much memory space a certain value would need. This type of instruction is formulated as a declaration.

Variable Declaration

To declare a variable, type var, followed by a name for the variable, followed by a colon, and followed by a word that defines the type of value that would be stored in this memory space. The formula used is:

var VariableName : DataType;

This means that, to declare a variable, you must provide a name and specify the type of information the variable will carry.

Identifiers

An identifier is a word that is used to identify an entity in a program. There are rules you must follow when naming your variables. They are:

  • The first character must be a letter (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z) or an underscore (_). Examples are _pages, country, or sound
  • After the first character, the name can contain letters, underscores, or digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9). Examples are number1, _page6, complete_sound
  • The complete name must not be one of the words reserved for the compilers own use

JScript is case-sensitive. This means that case, CASE, and Case are three different words. Based on these rules, avoid using the following reserved words to name your variables:

abstract  boolean break byte case catch
char class const continue debugger decimal
default delete do double else enum
export extends false final finally float
for function get if implements import
in instanceof int interface internal long
new null package  private  protected public
return sbyte  set  short static  
super switch this throw true try
typeof uint ulong ushort  var void 
while with        

Besides these official keywords, avoid the following keywords when naming your variable because these words would make your code difficult to read or troubleshoot:

Array Boolean Double Number Object String
           

 

Data Types

 

Introduction

A data type is a word that indicates the amount of memory needed to store the value of a variable. As there are various types of values used in a program, there are also different data types. Some data types are meant to hold numbers. Some others are form particular values such as one being true or false. There are other data types designed to handle almost any type of data, also called an object. One of the rules you should make for yourself is to always use the right data type for a variable. This makes your code easier to read and maintain.

 

Variable Initialization

After declaring a variable, the compiler reserves an area of the computer memory for it. In a JScript .NET application, each declared variable receives a value, also called a default value. When a variable is declared receives a value, the variable is said to having been initialized. You also, when declaring a value, can give it an initial value of your choice. The value that a variable receives depends on its data type.

Characters

A character can be one of the following symbols: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z. Besides these readable characters, the following symbols are called digits and they are used to represent numbers: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In addition, some symbols on the (US QWERTY) keyboard are also called characters or symbols. They are ` ~ ! @ # $ % ^ & * ( ) - _ = + [ { ] } \ | ; : ' < ? . / , > "

Besides the English language, other languages use other or additional characters that represent verbal or written expressions.

To declare a variable whose value would be a character, use the char keyword. Here is an example:

char Gender;

To initialize a character variable, include its value in single quotes. Here is an example:

var Gender : char = 'M';
 

Integers

An integer is a natural number defined as follows:

  • The first or most left character can optionally be a + or - sign
  • Whether the + or - character is omitted or not, the actually characters that specify the number are digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9) only, in any combination
  • Between the digits that compose the number, there cannot be any letter or other special characters

Examples of natural numbers are 25, -4284, or 64485663. The .NET Framework provides various types of natural numbers.

An integer can be initialized with a decimal or a hexadecimal value. A hexadecimal is a number that starts with 0x or 0X and includes an combination of digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9) and a, b, c, d, e, f, A, B, C, D, E, or F.

Byte: A byte is a small natural positive number that ranges from 0 to 255. To declare a variable for a small number, use the byte keyword.

Signed Byte: To declare a variable that can carry small numbers from -128 to 127, use the sbyte data type.

Short: A short integer is a natural number larger than the byte. To declare a variable that can hold natural numbers in the range of -32768 to 32767, you can use the short data type. Alternative, the .NET Framework provides the Int16 data type used for the same purpose.

Unsigned Short: To declare a variable that would carry positive natural numbers that range from 0 to 65535, you can use the ushort data type.

Integer: To declare a variable that can hold natural numbers, you can use the int data type. Here is an example:

var Natural: int;

Such a variable is initialized with a 0 value. This variable can hold values that range from -2147483648 to 2147483647.

Unsigned Integer: If you want a variable that can handle positive number from 0 to 4294967295, declare it using the uint data type.

Long: A natural number is referred to as long when its range is very large, from -1019 to 1019. To declare a variable for such a range of numbers, use the long data type.

Unsigned Long: To declare a positive long integer that can handle numbers from 0 to 1020, use the ulong data type.

 

 

Floating-Point Numbers

A number is referred to as floating when it can include a decimal portion, specified by a special character that depends on the language. In US English, this character is the period. Examples of floating numbers are 24.33 or 2684.6862

A floating number can also be represented in a scientific format. In this case, it uses the letter e or E followed by a sign as - or +, followed by a natural number called an exponent.

Single: A floating number is referred to as Single if it is meant to handle small numbers.

double: A double-precision number is one that can handle very large values from -10308 to 10308 with an accuracy of 15 digits. To declare a variable that can hold numbers in that range, you can use the Number or double data types.

decimal: To declare a decimal variable that can perform more precision than the double or number data type would allow, use the decimal data type.

 

Boolean Variables

A variable is referred to as Boolean if it can hold only one of two values: true or false. To declare a Boolean variable, use the boolean or the Boolean data type. Here is an example:

var IsReady : Boolean;

Once the variable has been declared, you can use it as you see fit. For example, you can pass it to print() to display its value to the user. Here is an example:

var IsReady : Boolean;
            print(IsReady);

When you declare a Boolean variable, the compiler initializes it with a false value. At any time, you can either initialize it with, or change its value to, true or false.

Strings

A string is an empty space, a character, a word, or a group of words that you want the compiler to consider "as is".
Primarily, the value of a string starts with a double quote and ends with a double-quote. An example of a string is "Welcome to the World of JScript .NET Programming!". You can include such a string in print method to display it on the console. Here is an example:

print("Welcome to the World of JScript .NET Programming!");

This would produce:

Welcome to the World of JScript .NET Programming!

Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the String keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is:

var Welcome : String;

After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using print. Here is an example:

var Welcome : String;
            Welcome = "Welcome to the World of JScript .NET Programming!";
            print(Welcome);

 

Operators and Operands

 

Fundamental C# Operators

 

Introduction

An operation is an action performed on one or more values either to modify the value held by one or both of the variables, or to produce a new value by combining existing values. Therefore, an operation is performed using at least one symbol and at least one value. The symbol used in an operation is called an operator. A value involved in an operation is called an operand.

A unary operator is an operator that performs its operation on only one operand. An operator is referred to as binary if it operates on two operands.

Semi-Colon;

The semi-colon is used to indicate the end of an expression or a declaration. Here is an example:

var sentence : String;

As we will learn when studying the for conditional statement, there are other uses of the semi-colon.

 

Curly Brackets { }

Curly brackets are probably the most used and the most tolerant operators of C#. Fundamentally, curly brackets are used to create a section of code. As such they are required to delimit the bodies of namespaces, classes, structures, exceptions, etc. They are also optionally used in conditional statements. Curly brackets are also used to create variable scope.

Parentheses ( )

Like most computer languages, C# uses parentheses to isolate a group of items that must be considered as belonging to one entity. For example, parentheses are used to differentiate a method such as Main from a regular variable.

Parentheses can also be used to isolate an operation or an expression with regard to another operation or expression.

 

The Comma ,

The comma is used to separate variables used in a group.

The Assignment =

When you declare a variable, a memory space is reserved for it. That memory space may be empty until you fill it with a value. To "put" a value in the memory space allocated to a variable, you can use the assignment operator represented as =. Based on this, the assignment operation gives a value to a variable. Its syntax is:

VariableName = Value

The VariableName factor must be a valid variable name. It cannot be a value such as a numeric value or a (double-quoted) string. Here is an example that assigns a numeric value to a variable:

var salary : double;
            // Using the assignment operator
            salary = 12.55;

Once a variable has been declared and assigned a value, you can call print() to display its value.  Here is an example:

var salary : double;
            // Using the assignment operator
            salary = 12.55;
            print("Employee's Hourly Salary: ");
            print(salary);

This would produce:

Employee's Hourly Salary: $12.55

The above code declares a variable before assigning it a value. You will usually perform this assignment when you want to change the value held by a variable. Providing a starting value to a variable when the variable is declared is referred to as initializing the variable. Here is an example:

var salary : double = 12.55;
            // Using the assignment operator
            print("Employee's Hourly Salary: ");
            print(salary);

We saw that you can declare various variables at once by using the same data type but separating their names with commas. When doing this, you can also initialize each variable by assigning it the desired value before the comma or the semi-colon. Here is an example:

var Value1, Value2 : double;
Value1 = 224.58;
Value2 = 1548.26;
print("Value 1 = ");
print(Value1);
print("Value 2 = ");
print(Value2);

This would produce:

Value 1 =
224.58
Value 2 =
1548.26
 

Single-Quote '

The single quote is used to include one character to initialize, or assign a symbol to, a variable declared as char. A single is usually combined with another and a character is included between them. Here is an example:

var Gender : char;
            var FirstName, LastName, FullName : String ;
            Gender = 'M';
            print(Gender);

You can include only one character between single-quotes except if the combination of symbols can be evaluated to one character. This is the case for escape sequences. Here is an example:

print('\n');

Double-Quotes "

The double-quote " is used to delimit a string. Like the single-quote, the double-quote is usually combined with another. Between the combination of double-quotes, you can include an empty space, a character, a word, or a group of words, making it a string. Here is an example:

print("The Wonderful World of JScript.NET!!!");

A double-quoted string can also be declared and then assigned to a variable.

 

Square Brackets [ ]

Square brackets are mostly used to control the dimension or index of an array. We will learn how to use them when we study arrays. Here is an example from a future lesson:

var number : double[] = new double[5];
            number[0] = 12.44;
            number[1] = 525.38;
            number[2] = 6.28;
            number[3] = 2448.32;
            number[4] = 632.04;

The Positive Operator +

Algebra uses a type of ruler to classify numbers. This ruler has a middle position of zero. The numbers on the left side of the 0 are referred to as negative while the numbers on the right side of the rulers are considered positive:

-∞   -6 -5 -4 -3 -2 -1   1 2 3 4 5 6   +∞
   0
-∞   -6 -5 -4 -3 -2 -1   1 2 3 4 5 6   +∞

A value on the right side of 0 is considered positive. To express that a number is positive, you can write a + sign on its left. Examples are +4, +228, +90335. In this case the + symbol is called a unary operator because it acts on only one operand. The positive unary operator, when used, must be positioned on the left side of its operand, never on the right side.

As a mathematical convention, when a value is positive, you don't need to express it with the + operator. Just writing the number without any symbol signifies that the number is positive. Therefore, the numbers +4, +228, and +90335 can be, and are better, expressed as 4, 228, 90335. Because the value doesn't display a sign, it is referred as unsigned.

To express a variable as positive or unsigned, you can just type it. here is an example:

print(+802);

This would produce:

Number = 802

The Negative Operator -

As you can see on the above ruler, in order to express any number on the left side of 0, it must be appended with a sign, namely the - symbol. Examples are -12, -448, -32706. A value accompanied by - is referred to as negative. The - sign must be typed on the left side of the number it is used to negate. Remember that if a number does not have a sign, it is considered positive. Therefore, whenever a number is negative, it MUST have a - sign. In the same way, if you want to change a value from positive to negative, you can just add a - sign to its left.

Here is an example that uses two variables. One has a positive value while the other has a negative value:

print(-802);

This would produce:

-802
 

Algebraic Operators

In algebra, operations are performed on numeric values. Algebraic operators are represented with the following symbols:

Operations
 

The Addition

The addition is an operation used to add things of the same nature one to another, as many as necessary. Sometimes, the items are added one group to another. The concept is still the same, except that this last example is faster. The addition is performed in mathematics using the + sign. The same sign is used in C#.

To get the addition of two values, you add the first one to the other. After the addition of two values has been performed, you get a new value. This means that if you add Value1 to Value2, you would write Value1 + Value2. The result is another value we could call Value3. You can also add more than two values, like a + b + c.

With numbers, the order you use to add two or more values doesn't matter. This means that Value1 + Value2 is the same as Value2 + Value1. In the same way a + b + c is the same as a + c + b the same as b + a + c and the same as c + b + a

Here is an example that adds two numbers:

print("244 + 835 = ");
            print(244 + 835);

Here is the result:

244 + 835 =
                        1079

You can also add some values already declared and initialized in your program. You can also get the values from the user.

In C#, you can also add string variables to add a new string. The operation is performed as if you were using numbers. For example, "Pie" + "Chart" would produce "PieChart". You can also add add as many strings as possible by including the + operator between them. Here is an example:

var FirstName : String;
            var LastName  : String;
            var FullName  : String;
            FirstName = "Alexander";
            LastName  = "Kallack";
            FullName  = FirstName + " " + LastName;
            print(FullName);

This would produce:

Alexander Kallack
 

The Multiplication

The multiplication allows adding one value to itself a certain number of times, set by a second value. As an example, instead of adding a value to itself in this manner: A + A + A + A, since the variable a is repeated over and over again, you could simply find out how many times A is added to itself, then multiply a by that number which, is this case, is 4. This would mean adding a to itself 4 times, and you would get the same result.

Just like the addition, the multiplication is associative: a * b * c = c * b * a. When it comes to programming syntax, the rules we learned with the addition operation also apply to the multiplication.

Here is an example:

var value1 : double;
var value2 : double;
var result : double;
value1 = 224.58;
value2 = 1548.26;
result = value1 * value2;
print(value1 + " * " + value2 + " = " + result);

This would produce:

224.58 * 1548.26 = 347708.2308
 

The Subtraction

The subtraction operation is used to take out or subtract a value from another value. It is essentially the opposite of the addition. The subtraction is performed with the - sign.

Here is an example:

var value1 : double;
            var value2 : double;
            var result : double;
            value1 = 224.58;
            value2 = 1548.26;
            result = value1 - value2;
            print(value1 + " * " + value2 + " = " + result);

This would produce:

224.58 - 1548.26 = -1323.68

Unlike the addition, the subtraction is not associative. In other words, a - b - c is not the same as c - b - a. Consider the following program that illustrates this:

// This tests whether the addition is associative
            print(" =+= Addition =+=");
            print("128 + 42 +   5 = " + (128 + 42 + 5));
            print("  5 + 42 + 128 = " + (5 + 42 + 128));
            print();
            // This tests whether the subtraction is associative
            print(" =-= Subtraction =-=");
            print("128 - 42 -   5 = " + (128 - 42 - 5));
            print("  5 - 42 - 128 = " + (5 - 42 - 128));

This would produce:

=+= Addition =+=
            128 + 42 +   5 = 175
            5 + 42 + 128 = 175
            =-= Subtraction =-=
            128 - 42 -   5 = 81
            5 - 42 - 128 = -165

Notice that both operations of the addition convey the same result. In the subtraction section, the numbers follow the same order but produce different results.

The Division

Dividing an item means cutting it in pieces or fractions of a set value. For example, when you cut an apple in the middle, you are dividing it in 2 pieces. If you cut each one of the resulting pieces, you will get 4 pieces or fractions. This is considered that you have divided the apple in 4 parts. Therefore, the division is used to get the fraction of one number in terms of another. The division is performed with the forward slash /.

Here is an example:

var value1 : double;
var value2 : double;
var result : double;
value1 = 224.58;
value2 = 1548.26;
result = value1 / value2;
print(value1 + " * " + value2 + " = " + result);

This would produce:

224.58 / 1548.26 = 0.145053156446592

When performing the division, be aware of its many rules. Never divide by zero (0). Make sure that you know the relationship(s) between the numbers involved in the operation.

 

The Remainder

The division program above will give you a result of a number with decimal values if you type an odd number (like 147), which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. Imagine you have 26 kids at a football (soccer) stadium and they are about to start. You know that you need 11 kids for each team to start. If the game starts with the right amount of players, how many will seat and wait?

The remainder operation is performed with the percent sign (%) which is gotten from pressing Shift + 5.

Here is an example:

var Players : int = 26;
            // When the game starts, how many players will wait?.
            print("Out of " + Players + " players, " + (26 % 11) + " players will have to wait
            when the game starts.\n");

This would produce:

Out of 26 players, 4 players will have to wait when the game starts.

JScript.NET Language Operators