ARTICLE

Nested If-Else In TypeScript

Posted by Richa Garg Articles | TypeScript October 29, 2012
In this article I will explain how to use nested If-Else statements in TypeScript.
Reader Level:

Introduction

Today I am going to explain how to use nested If-Else statements in TypeScript. Using nested If-Else statements I explain how to find the grade of any student by entering his or her marks in TypeScript. The syntax of a nested If-Else is:

If(Condition)

{

    //If condition is true then write the statement here

}

Else  //if condition is false then cursor go to else part

{

    If(Condition)    //again here check the condition under the else part

    {

        //If condition is true then write statement here

    }

    Else  //if condition is false then cursor go to else part

    {

        If(Condition)    //again here check the condition under the else part

        {

            //If condition is true then write statement here

        }

        Else

        {

        }

    }

}

Now to see how the nested If-Else is used let's use the following.

Step 1

Open Visual Studio 2012 and click on "File" -> "New" -> "Project...".

Step 2

A window is shown, in this select HTML Application for TypeScript under Visual C# and give the name of your application and then click ok.

Step 3

Open the app.ts file and write the following code in it:

class Nested_IfElse {

    constructor () {

    }

    GradingSystem() {

        var n: number;

        var s = 0;

        n = parseInt(prompt("Enter the marks of any student"));

        if (n >= 80) {

            alert(" You got A grade");

        }

        else if (n >= 60) { // Note the space between else & if

            alert(" You got B grade");

        }

        else if (n >= 40) {

            alert(" You got C grade");

        }

        else if (n < 40) {

            alert(" You Failed in this exam");

        }

    }

}

window.onload = () =>

{

    var greeter = new Nested_IfElse();

    greeter.GradingSystem();

};

Step 4

Open the app.js file and write the following code in it:
 

var Nested_IfElse = (function () {

    function Nested_IfElse() {

    }

    Nested_IfElse.prototype.GradingSystem = function () {

        var n;

        var s = 0;

        n = parseInt(prompt("Enter the marks of any student"));

        if (n >= 80) {

            alert(" You got A grade");

        } else {

            if (n >= 60) {

                alert(" You got B grade");

            } else {

                if (n >= 40) {

                    alert(" You got C grade");

                } else {

                    if (n < 40) {

                        alert(" You Failed in this exam");

                    }

                }

            }

        }

    };

    return Nested_IfElse;

})();

window.onload = function () {

    var greeter = new Nested_IfElse();

    greeter.GradingSystem();

};

Step 5

Write the following code in the default.htm file:
 

<!DOCTYPE html>

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

<head>

    <meta charset="utf-8" />

    <title>TypeScript HTML App</title>

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

</head>

<body>

    <h1>Nested If Else Example</h1>

    <div id="content">

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

    </div>

</body>

</html>



Step 6

Now run the application; the output will look like:

Output-of-nested-ifelse-in-typescript.jpg

When I click the ok button it will give the result as:

Display-result-of-nested-ifelse-in-Typescript.jpg


Summary

In this article I explained how to use nested If-Else in TypeScript.

COMMENT USING

Trending up