Reader Level:
ARTICLE

How To Use If-Else Statement In TypeScript

Posted by Nitin Bhardwaj Articles | TypeScript October 18, 2012
In this article, we are explaining how to use an if-else statement in TypeScript with an example.
  • 0
  • 0
  • 4832

if..else statement in TypeScript

An if-else statement lets you control the execution of statements based on the results of conditional expressions. An if statement can include multiple else and if clauses and one else clause at the end. The if clause of the statement executes one or more statements if its condition is true.

The else if clause is executed when the previous condition or conditions are false. The statement or statements within an else if clause are executed if its condition is true.

The else clause is executed when all previous conditions are false.

You can code one if statement within the block of statements of an if, else if, or else clause in another if statement. This is referred to as nested if statements.

Syntax

For single statement :

if (condition)

statement_1

[else
statement_2]

The following example shows how to use an if..else statement in TypeScript. This code checks whether an input alphabet is a vowel or not. Both lower-case and upper-case are checked. Let's see how I implement the switch statement  in TypeScript. Let's use the following steps.

Step 1

Open Visual Studio 2012 and click "File" -> "New" -> "Project...". A window is opened. Give the name of your application as "if-else" and then click ok.

Step 2

After this session a new window is opened on the right. This window is called the Solution Explorer. The Solution Explorer contains the ts file, js file, css file and html file.

Coding

if-else.ts

class Greeter

{

    check_vowel(a:string):number

    {

    if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')

       return1;

    return0;

    }

}

window.onload = () =>

{

    var n: string;

    n =prompt("Enter a Char").toLowerCase();

    if (n.length == 1)

    {

        var greeter = new Greeter();

        var result: number;

        result = greeter.check_vowel(n);

        if (result == 1)

        {

            alert("Character is vowel ->" + n);

        }

        else

        {

            alert("Character is not vowel ->" + n);

        }

    }

    else

    {

        alert("Eneter a charcter not a string");

    }

};

 

if-elsedemo.html

<!DOCTYPEhtml>

 

<htmllang="en"xmlns="http://www.w3.org/1999/xhtml">

<head>

    <metacharset="utf-8"/>

    <title>if-else statement</title>

    <linkrel="stylesheet"href="app.css"type="text/css"/>

    <scriptsrc="app.js"></script>

</head>

<body>

    <h1>if-else statement Example in TypeScript HTML App</h1>

    <divid="content"/>

</body>

</html>

 

app.js

var Greeter = (function () {

    function Greeter() { }

    Greeter.prototype.check_vowel = function (a) {

        if(a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') {

            return 1;

        }

        return 0;

    };

    return Greeter;

})();

window.onload = function () {

    var n;

    n = prompt("Enter a Char").toLowerCase();

    if(n.length == 1) {

        var greeter = new Greeter();

        var result;

        result = greeter.check_vowel(n);

        if(result == 1) {

            alert("Character is vowel ->" + n);

        } else {

            alert("Character is not vowel ->" + n);

        }

    } else {

        alert("Eneter a charcter not a string");

    }

};

 

Output 1


enter-char.gif


Click on "Ok" the button.

 

Output 2

 

final-result.gif

Output 3


If we enter a string then we get:


enter-string.gif


O
utput 4


wrong-result.gif


Reference By

http://www.typescriptlang.org/

COMMENT USING

Trending up