Skip to content

Commit ed72b63

Browse files
authored
Merge pull request Swap76#238 from lnchapin/master
Conditional Statements
2 parents bcd116f + 3c7edcd commit ed72b63

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed
Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
# IF, ELSE IF, ELSE CONDITION
1+
# IF, ELSE IF, ELSE CONDITION
2+
Conditional statements run specific blocks of code based on certain parameters that you set. Like other code it reads top to bottom so the order that you put your conditions in matter.
3+
4+
Example of an if/else if/else:
5+
```js
6+
if(this statement is true){
7+
do this
8+
} else if (this statement is true){
9+
do this
10+
} else{
11+
do this
12+
}
13+
```
14+
`If` always comes first and have a set of parenthesis with a statement in them that you are checking to see if it's true. If it is, the code in the curly brackets is run, otherwise it would move on to the `else if`, and see if that statement is true. If `else if` is true then it runs that block of code, if not then it would run the code in the `else` statement. Once a statement is deemed true and the code is run it doesn't continue to read through the code even if there is a more accurate statement lower down. A good example of where this matters would be the common coding challenge fizzbuzz. The idea of fizzbuzz is that you want to log all the numbers from 1-100 however if it's divisible by 3 print "fizz", 5 print "buzz", and if it's divisible by both 3 and 5 print "fizzbuzz".
15+
If we write our if's the way the problem is stated we would would write:
16+
```
17+
if(num % 3 == 0){
18+
console.log("fizz")
19+
} else if(num % 5 == 0){
20+
console.log("buzz")
21+
} else if(num % 3 == 0 && num % 5 == 0){
22+
console.log("fizzbuzz")
23+
} else {
24+
console.log(num)
25+
}
26+
```
27+
In this case every time we'd hit a number that should print fizzbuzz we'd get fizz because that is a truthful statement and it's not looking for a more correct answer.
28+
To account for that we'd want to adjust our statement order to:
29+
```js
30+
if(num % 3 == 0 && num % 5 == 0){
31+
console.log("fizzbuzz")
32+
} else if(num % 5 == 0){
33+
console.log("buzz")
34+
} else if(num % 3 == 0){
35+
console.log("fizz")
36+
} else {
37+
console.log(num)
38+
}
39+
```
40+
41+
## What if we don't write else if but just another if?
42+
Sometimes you see code that has a number of `if`'s in a row but no `else if`'s or `else`'s in this case it's checking each condition in that case each `if`
43+
44+
This code will only print "it's a number":
45+
```js
46+
var number = 2
47+
if(typeof number === "number"){
48+
console.log("it's a number")
49+
} else if(number % 2 === 0){
50+
console.log("it's even")
51+
}
52+
```
53+
However this code will print both "it's a number" and "it's even"
54+
```js
55+
var number = 2
56+
if(typeof number === "number"){
57+
console.log("it's a number")
58+
}
59+
if(number % 2 === 0){
60+
console.log("it's even")
61+
}
62+
```
63+
64+
## What if we only have an if?
65+
If there is only one `if` then it only does that code `if` the condition is true and nothing if it's false.
66+
67+
For example:
68+
```js
69+
if(number % 2 === 0){
70+
console.log("it's even")
71+
}
72+
console.log(number)
73+
```
74+
In this case if `number` is even it will print it's even and it will always print the number.

0 commit comments

Comments
 (0)