0% found this document useful (0 votes)
39 views20 pages

PIAIC Conditional

Uploaded by

barige1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views20 pages

PIAIC Conditional

Uploaded by

barige1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Intro to TypeScript

Conditional Structures
Instructor: Mr Muhammad Sajid
Whatsapp: +92333 9211574

Quarter 1 PIAIC Peshawar Pakistan


Presidential Initiative for Artificial Intelligence & Computing
(PIAIC)
Control Structures

Algorithms require two important control structures: iteration (repeating)


and selection(conditional). Both are supported by typescript in various
forms. The programmer can choose the statement that is most useful for
the given circumstances.

Selection Statements/Decision Making

There come situations in real life when we need to make some decisions
and based on these decisions, we decide what we should do next.

Decision Making in Real life :

1. Choosing an Outfit: Imagine you're getting ready for the day and
checking the weather. If it's raining, you'll wear a raincoat. Otherwise,
you might just wear a T-shirt. This decision is based on a condition - the
weather.

2
2. Cooking: When boiling an egg, you might check: If the water is
boiling, put the egg in. Else, wait for a bit more.

3. Traffic Lights: If the traffic light is red, stop. If it's green, go. If it's
yellow, slow down.

We can clearly see we daily make different decisions based on different


factors similar situations arise in programming also where we need to
make some decisions and based on these decisions we will execute the
next block of code.

For example: We go to an ATM Machine and enter our ATM Card then
ATM Machine checks:
If the account balance is less than 0: Display "Insufficient funds".
Else: Allow the transaction.

Following is the general form of a typical decision making structure


found in most of the programming languages.

3
4
if Statement

"if" checks a condition. If the condition is true, the code inside the "if"
block is executed.

Syntax:

if (condition) {
// code to execute if condition is true
}

Flowchart of if statement:

5
Example: Let's say If the user's age is more than 18, they're allowed
to vote.

Output:
You're allowed to vote!

6
if else Statement

"if-else" checks a condition, if it's true, it executes the "if" block. If not,
it executes the "else" block.
Syntax:

if (condition) {
// code if condition is true
} else {
// code if condition is false
}

Flowchart of if else statement:

7
Example: If a student's score is greater than 50, they pass. Else, they
fail.

Output:
You failed.

8
if-else-if Ladder Statement

The if else if statements are used when the user has to decide among
multiple options. The if statements are executed from the top down. As
soon as one of the conditions controlling the if is true, the statement
associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement
will be executed. if-else-if ladder is similar to the switch statement.

Syntax:

if (condition1) {
// code for condition1
}
else if (condition2) {
// code for condition2
}
else {
// code if none of the conditions are true
}

9
Flowchart of if-else-if Ladder statement:

10
Example: Giving feedback based on student grades:

Output:
Try harder next time.

11
Nested if else Statement

A nested if in typescript is an if statement that is the target of another if


statement. Nested if statements mean an if statement inside another if
statement.

Syntax:

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}

12
Flowchart of Nested if else statement:

13
Example:Checking a number if it is positive or negative :

Output:
Positive number
Even number

14
Switch Statement

The switch case statement is an alternative to the if else if ladder and is


useful when there are multiple conditions based on a single expression's
value.

Syntax:

switch(expression) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code if none of the values match
}

15
Flowchart of Switch statement:

16
Example:

Output:
Wednesday

17
Note: In a switch statement, if we do not use a break after each case,
execution will continue to the next case and execute the code for that
case as well. For example:
let num = 1
switch(num){
case 1:
console.log("Number is 1");
case 2:
console.log("Number is 2");
default:
console.log("Invalid Number");
}
This will print "Number is 1" and "Number is 2" & "Invalid Number"
since there is no break after case 1 and 2. The break statement prevents
falling through to the next case.So we must use the break in switch
statement for proper working otherwise it will misbehave .

Sometimes, you might want to group multiple values under a single


case. TypeScript (and JavaScript) allows you to do this by placing
multiple case statements sequentially without a break in between.

let letter = 'a';


switch (letter) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
console.log('This is a vowel.');
break;

18
default:
console.log('This is a consonant.');
}
In this example, if a letter is any vowel (a, e, i, o, u), it will print "This is
a vowel." since all these cases lead to the same code block.

Conditional Operator

The conditional operator is used to add conditional code in our program.


It is similar to the if-else statement. It is also known as the ternary
operator as it works on three operands.

Syntax:

(condition) ? true_statements : flase_statements ;

19
Flowchart of Ternary Operator:

Example:Checking if user can vote or can not

Output:false

20

You might also like