TypeScript If-Else Statements
Learn how to make decisions in TypeScript using if, if-else, else-if ladder, and nested if statements with practical examples and expected outputs.
What Will We Learn?
Conditional statements allow a program to execute different instructions depending on the values and conditions being checked.
- Use the basic if statement.
- Execute alternatives using if-else.
- Check multiple conditions with else-if.
- Use nested if statements.
- Combine conditions using logical operators.
- Apply conditions in student and course examples.
If-Else Workflow
Follow these steps to understand how the if-else statement makes a decision.
Step 1
Input ValueDeclare a variable.
Step 2
Check ConditionEvaluate true or false.
Step 3
Select BlockExecute the matching block.
Step 4
Display ResultContinue program execution.
1. If Statement
The if statement executes a block only when its condition evaluates to true. If the condition is false, the block is skipped.
Syntax
if (condition) {
// Code executes when condition is true
}
Example
let age: number = 20;
if (age >= 18) {
console.log("You are eligible to register");
}
console.log("Program completed");
Expected Output
You are eligible to register
Program completed
Since age is 20, the condition is true and the message inside the if block is displayed.
2. When the If Condition Is False
If a condition is false and no else block is provided, the code inside the if block is skipped. Statements after the block continue executing normally.
Example
let marks: number = 25;
if (marks >= 35) {
console.log("Pass");
}
console.log("Result checked");
Expected Output
Result checked
Marks are 25, so the condition is false. The Pass message is skipped, but the next statement still executes.
3. If-Else Statement
The if-else statement provides two possible execution paths. The if block runs when the condition is true; otherwise, the else block runs.
Syntax
if (condition) {
// True block
} else {
// False block
}
Example
let marks: number = 28;
if (marks >= 35) {
console.log("Pass");
} else {
console.log("Fail");
}
Expected Output
Fail
Since 28 is less than 35, the if condition is false and the else block executes.
4. Else-If Ladder
An else-if ladder checks multiple conditions in order. The first true condition determines which block executes. If no condition is true, the else block executes.
Syntax
if (condition1) {
// First block
} else if (condition2) {
// Second block
} else if (condition3) {
// Third block
} else {
// Default block
}
Example: Student Grades
let marks: number = 78;
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 75) {
console.log("Grade B");
} else if (marks >= 60) {
console.log("Grade C");
} else if (marks >= 35) {
console.log("Grade D");
} else {
console.log("Fail");
}
Expected Output
Grade B
Marks are below 90 but at least 75, so Grade B is selected. Once a matching branch executes, the remaining branches are skipped.
5. Nested If Statement
A nested if statement is an if statement inside another if statement. It is useful when the second condition should be checked only after the first condition is satisfied.
Example
let age: number = 22;
let hasDocuments: boolean = true;
if (age >= 18) {
if (hasDocuments) {
console.log("Registration Allowed");
} else {
console.log("Documents Required");
}
} else {
console.log("Age Requirement Not Met");
}
Expected Output
Registration Allowed
The age requirement is satisfied, so the inner if statement checks whether the student has documents. Both conditions are satisfied.
6. Comparison Operators in If Statements
Comparison operators are commonly used to form the conditions evaluated by if and else statements.
Example
let courseFees: number = 30000;
let studentBudget: number = 35000;
if (courseFees <= studentBudget) {
console.log("Course is within your budget");
} else {
console.log("Course is over your budget");
}
Expected Output
Course is within your budget
The condition is true because 30000 is less than 35000. Therefore, the first block executes.
7. Logical Operators with If-Else
Logical operators allow you to combine two or more conditions into a single decision.
Example: Admission Eligibility
let marks: number = 75;
let attendance: number = 85;
if (marks >= 35 && attendance >= 75) {
console.log("Student Eligible");
} else {
console.log("Student Not Eligible");
}
Expected Output
Student Eligible
The student has passing marks and the required attendance. Both conditions are true, so the student is eligible.
8. Types of Conditional Statements
| Statement | Purpose | Example Use |
|---|---|---|
if |
Executes a block when a condition is true. | Check age eligibility. |
if-else |
Selects between two paths. | Pass or fail. |
else-if |
Checks multiple conditions in order. | Calculate grades. |
Nested if |
Checks a condition inside another condition. | Validate eligibility and documents. |
Practical Example: CIIT Student Result
Let's calculate a student's result and assign a grade using conditional statements.
Example
let studentName: string = "Priya";
let marks: number = 82;
console.log("Student Name: " + studentName);
console.log("Marks: " + marks);
if (marks >= 90) {
console.log("Grade: A");
} else if (marks >= 75) {
console.log("Grade: B");
} else if (marks >= 60) {
console.log("Grade: C");
} else if (marks >= 35) {
console.log("Grade: D");
} else {
console.log("Result: Fail");
}
Expected Output
Student Name: Priya
Marks: 82
Grade: B
The else-if ladder evaluates the conditions in order. Since 82 is at least 75 but less than 90, Grade B is displayed.
Common Mistakes
-
Using assignment
=instead of comparison===when checking equality. - Forgetting braces around a block with multiple statements.
- Placing else before the corresponding if block is complete.
- Writing else-if conditions in an incorrect order.
- Forgetting that only the first matching else-if branch executes.
- Using nested if statements when a simpler condition would be easier to maintain.
Practice Task
Create a TypeScript program to calculate the result of a student.
- Student name: Amit
- Marks: 68
- Marks 90 or above: Grade A
- Marks 75 or above: Grade B
- Marks 60 or above: Grade C
- Marks 35 or above: Grade D
- Otherwise: Fail
Use an else-if ladder to display the student's grade and print the student's name and marks.
Hint
let studentName = "Amit";
let marks = 68;
console.log("Student: " + studentName);
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 75) {
console.log("Grade B");
} else if (marks >= 60) {
console.log("Grade C");
} else if (marks >= 35) {
console.log("Grade D");
} else {
console.log("Fail");
}
Expected Output
Student: Amit
Grade C
Summary
In this lesson, we learned how to use if statements, if-else statements, else-if ladders, and nested if statements. We also combined comparison and logical operators to make application decisions.
Key Point: Conditional statements allow a TypeScript program to select which block of code should execute according to the evaluated condition.