Skip to main content

TypeScript Control Statements

Control statement are used to manage program execution is based on specific condition. A control statement is a statement that determines whether other statements will be executed.

if statement

That is simple and selection based statement that is depending upon given expression condition. Generally most of statements are based on condition, because condition is decide the which statements allowed to the current execution.

if..else statement

else statement is an optional block of if-statement, that are work when if statement conditions are not true. Both are conditional blocks, but both statements cannot be executed at the same time.

else..if statement

This relates to the next behavior of conditional statements. When one condition are not then second condition are define else if part. This condition are tested when above if or else..if condition are not valid.

Switch case statement

Switch case are used to check multiple condition of single value and pattern.

function checkMonth(id:number) 
{
    // Test expression
    switch(id)
    {
        case 1:
            console.log("January");
        break;
        case 2:
            console.log("February");
        break;
        case 3:
            console.log("March");
        break;

        case 4:
            console.log("April");
        break;

        case 5:
            console.log("May");
        break;

        case 6:
            console.log("June");
        break;

        case 7:
            console.log("July");
        break;

        case 8:
            console.log("August");
        break;

        case 9:
            console.log("September");
        break;

        case 10:
            console.log("October");
        break;

        case 11:
            console.log("November");
        break;
        
        case 12:
            console.log("December");
        break;
        default:
            console.log("Invalid Month");
        break;
    }
}
// Test
checkMonth(4); // April
checkMonth(12); // December 




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment