Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Conditioning Material #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Learning/conditioning/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Conditioning

The purpose of conditioning is to tell the program which block of code can be executed. In javascript there are three kinds of conditioning, namely:

1. `if`

In the javascript programming language there is a way in which we can execute code, if the conditions we specify are met (`true`), namely by using the if statement, usually the conditions in the if statement use the comparison operator or logical operator. the code inside the if statement will be executed only if the condition returns `true`.

for example:

```js
if (true) {
console.log("This code must be executed");
}
```

2. `else`

Javascript also has a way to execute code if the specified condition is not met (`false`) by using the else statement. If the code in the if is not executed because the condition is not met or returns false, then the code in the else block will be executed.

for example:

```js
if (false) {
console.log("This code will never run");
} else {
console.log("This code must be executed");
}
```

3. `else if`

There is another method to perform sequential checking, namely the else if. If the first if block is false, then the code will check with the second condition in the else if block. If the second condition evaluates to true, the else if block will be executed.

for example:

```js
const date = 10;

if (date < 5) {
console.log("This code will never run");
} else if (date > 8) {
console.log("This code must be executed");
} else {
console.log("This code will never run");
}
```

4. `switch case`

There is another alternative to check sequentially, namely with a switch case. We can determine which variable we will check, then add some cases below it to check the contents of the variable that we specified in the switch earlier. We can also provide a default condition as well as the else above.

for example:

```js
const fruit = "Oranges";

switch (fruit) {
case "Apple":
console.log("This code will never run");
break;
case "Pineapple":
console.log("This code will never run");
break;
default:
console.log("This code must be executed");
break;
}
```

Lihat kode lengkapnya [disini](conditional.js).
54 changes: 54 additions & 0 deletions Learning/conditioning/conditional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//--------------if else-------------

// let's see an example implementation
const x = 10;
// we find out with the comparison operator
// is the value of the variable x less than 11
if (x < 11) {
// if fulfilled
console.log("TRUE, variable x is less than 11");
} else {
// if not fulfilled
console.log("FALSE, variable x is greater than 11");
}

//--------------else if-------------

// let's see an example implementation
const clock = 13;
// we check the value of the clock variable
// is the clock variable less than 12 ?
if (clock < 12) {
// if fulfilled
console.log("good morning");
//if not met then proceed to else if . block
//that is, is the clock variable greater than or equal to 12
} else if (clock >= 12) {
// if fulfilled
console.log("good afternoon");
} else {
// if none of the conditions are met from the two blocks above, then the else block will be executed
console.log("FALSE, variable x is greater than 10");
}

//------------switch case------------

// let's see an example implementation
const fruit = "Oranges";

// first, we determine what variables we will check
switch (fruit) {
// let's check, does the fruit variable contain apples?
case "Apple":
console.log("An apple costs 2 dollars");
break;
// if not then move on to the next case
// does fruit variable contain Pineapple?
case "Pineapple":
console.log("An pineapple costs 5 dollars");
break;
// if still not, it will pick up from default
default:
console.log("Oranges are gone");
break;
}