Conteúdo do Curso
Introduction to TypeScript
Introduction to TypeScript
If-else statement
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/a5c23211-8dc2-4c4a-9a83-07a51e843ab6/section_1/TS_meme_10%404x.png)
Sometimes, one condition is not enough, and for that, in TypeScript (as in other programming languages), there is the if-else
construct. For example, if you need to create a calculator, you, as a true programmer, will do it using the if-else
construct, like this:
In this code, we have 3 variables: number a
, number b
, and the operation that will be performed between them. Using the if-else
construct, we determine which operation will be applied to these two numbers. If we don't find a suitable operation, we will display a message indicating that such an operation is not available!
Now let's take a closer look at the syntax we're using:
Note that if one of the conditions is met, we exit the if-else
statement, and the remaining blocks are ignored.
Unlike else if
, the else
block does not have a condition block. This is because else executes only if all previous conditions were false
.
The if-else
construct is often used for a variety of tasks, from checking if a number is positive to writing artificial intelligence.
You can also choose not to use else-if
blocks and use only if
and else
, for example:
This way, we can experiment and use such a construct for various purposes!
Tudo estava claro?
Conteúdo do Curso
Introduction to TypeScript
Introduction to TypeScript
If-else statement
![](https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/a5c23211-8dc2-4c4a-9a83-07a51e843ab6/section_1/TS_meme_10%404x.png)
Sometimes, one condition is not enough, and for that, in TypeScript (as in other programming languages), there is the if-else
construct. For example, if you need to create a calculator, you, as a true programmer, will do it using the if-else
construct, like this:
In this code, we have 3 variables: number a
, number b
, and the operation that will be performed between them. Using the if-else
construct, we determine which operation will be applied to these two numbers. If we don't find a suitable operation, we will display a message indicating that such an operation is not available!
Now let's take a closer look at the syntax we're using:
Note that if one of the conditions is met, we exit the if-else
statement, and the remaining blocks are ignored.
Unlike else if
, the else
block does not have a condition block. This is because else executes only if all previous conditions were false
.
The if-else
construct is often used for a variety of tasks, from checking if a number is positive to writing artificial intelligence.
You can also choose not to use else-if
blocks and use only if
and else
, for example:
This way, we can experiment and use such a construct for various purposes!
Tudo estava claro?