Basic Algorithm with Typescript (Part 2)

Abiyogaaron
4 min readSep 10, 2021

--

basic programming for zero experience people

On the last article I talked about the concept of operator, how to apply it and the list of operator which necessary in making a behaviour of your code. In this article I will explain you about conditional and how to apply it.

Conditional

First of all what is conditional ? Conditional is an expressions or a statement to handle decisions which code must be executed after a specific line of your code, so it used to evaluate an expressions either to be true or false in terms of flow control.

conditional statement in programming
Illustration of conditional statement in programming

The picture above illustrates what is conditional looks like in programming. So imagine conditional expression / statement is a door that filtering whom will be inside the purple building, the green building, the yellow building or not all of them.

This is an example of the conditional statement

let grade = 89;if (grade >= 90 ) {
console.log("sukses");
} else if (grade >= 75) {
console.log("lulus");
} else {
console.log("tidak lulus");
}

Basic Syntax of Conditional Statement

  1. If statement: if statement consists of one or more boolean expression
let a = 5;if (a > 10) {
console.log("executed...") // "executed" will not be printed
}
if (a > 3 && a == 5) {
console.log("executed-1...") // "executed-1" will be printed
}

2. If … else statement: if else statement followed by else as an optional statement if the statement boolean is false

let a = 5;if (a > 10) {
console.log("hello");
} else {
console.log("world"); // "world" will be printed
}

3. If … else if statement: if else if statement followed by one or more than one else if as an optional statement that evaluate boolean from first if to the last else if or the else statement (sequentially compiled / executed).

let a = 5;if (a > 10) {
console.log("a: ", a);
} else if (a > 8) {
console.log("a: ", a);
} else if (a >= 5) {
console.log("a: ", a); // executed in here
} else {
console.log("a: ", a);
}

For now I want to tell you the tricky part of if else statement, so this is what will be the output on your screen if I write this code below:

let a = 5;if (a == 5) {
console.log("printed.."); // will be printed
}
if (a <= 10) {
console.log("printed.."); // will be printed
}
if (a == 5) {
console.log("printed.."); // will be printed
} else if (a <= 10) {
console.log("printed here.."); // will not be printed
}

As you see why “printed here..” won’t be printed same as the first if statement. The cause is the if else if statement behaviour will break the conditionals checking when there is if or else if statement fulfilled the true value, otherwise the compiler will keep checking until it stop at the else statement (false value) or the last else if statement.

4. Switch case statement: switch case statement looks similar with if else statement but there are fundamental differences between these two

  • The if-else statement checks for the value equality and the logical expressions. On the other hand, switch case statement only check for the value equality.
  • The sequence of execution will be executed only under if, else if or else statement, however switch case statement will be executed in every case statement after a case statement that fulfilled to be true, if you do not put a break; syntax inside each case statement.
  • The if else statement will execute else statement as turn outs the boolean value is false, otherwise the switch case statement will execute default statement as turn outs the boolean value is false.
  • The if else statement evaluates number, string or boolean data type, however the switch case statement only evaluates number or string data type.
let grade = "B";switch (grade) {
case "A": {
console.log("Sukses");
}
case "B": {
console.log("Cukup"); // will be printed
}
case "C": {
console.log("Lulus"); // will be printed
}
default: {
console.log("Tidak Lulus"); // will be printed
}
}

If you apply break; syntax inside each case statement, the result will be totally different.

let grade = "B";switch (grade) {
case "A": {
console.log("Sukses");
break;
}
case "B": {
console.log("Cukup"); // will be printed
break;
}
case "C": {
console.log("Lulus");
break;
}
default: {
console.log("Tidak Lulus");
}
}

Only “Cukup” will be printed on your terminal / cmder screen. Remember, only string or number data type allowed when using switch case statement, so this code below will throw an error:

let grade = 75;switch (grade) {
case (grade >= 75): { // 'boolean' is not comparable to 'number'
console.log("Sukses");
}
default: {
console.log("Kurang");
}
}

The Advantages using switch case statement

  1. Switch case statement is more faster during compliation / executing time rather than if else statement.
  2. Switch case statement is more readable rather than if else statement which could become too complex when we apply too many logic expressions.

This is the end of part 2, this article is more focusing on the concept of conditionals and how you apply it to your code. For the next part, I will write about Iteration or looping statement.

--

--

Abiyogaaron
Abiyogaaron

Written by Abiyogaaron

I’m a Software Engineer in one of Indonesia E-Commerce. A Tech enthusiast and a Programming teacher

No responses yet