YouTip LogoYouTip

Ts If Statement

* * * Conditional statements are used to perform different actions based on different conditions. TypeScript conditional statements are code blocks that executed based on the result (True or False) of one or more statements. You can understand the execution process of conditional statements through the following figure: !(#) * * * ## Conditional Statements When writing code, you often need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task. In TypeScript, we can use the following conditional statements: * **if statement** - Use this statement to execute code only when the specified condition is true * **if...else statement** - Execute code when the condition is true, and execute other code when the condition is false * **if...else if....else statement** - Use this statement to select one of multiple code blocks to execute * **switch statement** - Use this statement to select one of multiple code blocks to execute * * * ## if Statement A TypeScript if statement consists of a boolean expression followed by one or more statements. ### Syntax The syntax is as follows: if(boolean_expression){ # execute when boolean_expression is true} If the boolean expression boolean_expression is true, the code block inside the if statement will be executed. If the boolean expression is false, the first group of code after the if statement (after the closing brace) will be executed. ### Flowchart ![Image 2: if statement in Perl](#) ### Example var num:number = 5 if(num>0){console.log("The number is positive")} Compiling the above code produces the following JavaScript code: var num = 5; if(num>0){console.log("The number is positive"); } Executing the above JavaScript code, the output is: The number is positive * * * ## if...else Statement An if statement can be followed by an optional else statement, which executes when the boolean expression is false. ### Syntax The syntax is as follows: if(boolean_expression){ # execute when boolean_expression is true}else{ # execute when boolean_expression is false} If the boolean expression boolean_expression is true, the code inside the if block will be executed. If the boolean expression is false, the code inside the else block will be executed. ### Flowchart !(#) ### Example ## TypeScript var num:number = 12; if(num % 2==0){console.log("Even number"); }else{console.log("Odd number"); } Compiling the above code produces the following JavaScript code: ## JavaScript var num = 12; if(num % 2 == 0){console.log("Even number"); }else{console.log("Odd number"); } Executing the above JavaScript code, the output is: Even number * * * ## if...else if....else Statement The if...else if....else statement is very useful when executing multiple conditional checks. ### Syntax The syntax is as follows: if(boolean_expression 1) { # execute when boolean_expression 1 is true} else if( boolean_expression 2) { # execute when boolean_expression 2 is true} else if( boolean_expression 3) { # execute when boolean_expression 3 is true} else { # execute when all boolean expressions are false} Note the following points: * An **if** statement can have 0 or 1 **else** statement, which must come after the **else..if** statements. * An **if** statement can have 0 or more **else..if** statements, which must come before the **else** statement. * Once the code inside an **else..if** is executed, the subsequent **else..if** or **else** will not be executed. ### Example ## TypeScript var num:number = 2 if(num>0){console.log(num+" Is positive")}else if(num0){console.log(num + " Is positive"); }else if(num<0){console.log(num + " Is negative"); }else{console.log(num + " Neither positive nor negative"); } Executing the above JavaScript code, the output is: 2 Is positive * * * ## switch…case Statement A **switch** statement allows testing a variable for equality against multiple values. Each value is called a case, and the variable being tested is checked against each **switch case**. Syntax for the **switch** statement: switch(expression){case constant-expression : statement(s); break; case constant-expression : statement(s); break; default : statement(s); } The **switch** statement must follow these rules: * The **expression** in the **switch** statement is an expression to be compared, which can be of any type, including primitive data types (such as number, string, boolean), object types (such as object, Array, Map), and custom types (such as class, interface, enum). * A switch can have any number of case statements. Each case is followed by a value to compare and a colon. * The **constant-expression** of the case must have the same or compatible data type as the variable expression in the switch. * When the variable being tested equals a constant in the case, the statements following the case will be executed until a **break** statement is encountered. * When a **break** statement is encountered, the switch terminates, and the control flow jumps to the next line after the switch statement. * Not every case needs to contain a **break**. If a case statement
← Jsref Isfinite NumberTs Basic Syntax β†’