diff --git a/Learning/conditioning/README.md b/Learning/conditioning/README.md new file mode 100644 index 0000000..e28f793 --- /dev/null +++ b/Learning/conditioning/README.md @@ -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). diff --git a/Learning/conditioning/conditional.js b/Learning/conditioning/conditional.js new file mode 100644 index 0000000..fc516af --- /dev/null +++ b/Learning/conditioning/conditional.js @@ -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; +} \ No newline at end of file