If Statements

4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$39.99
List Price:  $79.99
You save:  $40
€34.30
List Price:  €68.62
You save:  €34.31
£29.82
List Price:  £59.66
You save:  £29.83
CA$55.17
List Price:  CA$110.36
You save:  CA$55.19
A$61.22
List Price:  A$122.47
You save:  A$61.24
S$51.52
List Price:  S$103.05
You save:  S$51.53
HK$312
List Price:  HK$624.09
You save:  HK$312.08
CHF 32.13
List Price:  CHF 64.27
You save:  CHF 32.14
NOK kr400.87
List Price:  NOK kr801.85
You save:  NOK kr400.97
DKK kr256.03
List Price:  DKK kr512.14
You save:  DKK kr256.10
NZ$68.19
List Price:  NZ$136.39
You save:  NZ$68.20
د.إ146.86
List Price:  د.إ293.76
You save:  د.إ146.90
৳4,863.98
List Price:  ৳9,729.17
You save:  ৳4,865.19
₹3,521.26
List Price:  ₹7,043.42
You save:  ₹3,522.15
RM169.05
List Price:  RM338.15
You save:  RM169.10
₦61,442.63
List Price:  ₦122,900.63
You save:  ₦61,458
₨11,315.87
List Price:  ₨22,634.58
You save:  ₨11,318.70
฿1,293.53
List Price:  ฿2,587.39
You save:  ฿1,293.86
₺1,645.69
List Price:  ₺3,291.79
You save:  ₺1,646.10
B$218.77
List Price:  B$437.60
You save:  B$218.83
R704.97
List Price:  R1,410.13
You save:  R705.15
Лв67.15
List Price:  Лв134.32
You save:  Лв67.17
₩55,569.77
List Price:  ₩111,153.44
You save:  ₩55,583.66
₪134.68
List Price:  ₪269.41
You save:  ₪134.72
₱2,291.40
List Price:  ₱4,583.38
You save:  ₱2,291.98
¥5,940.79
List Price:  ¥11,883.07
You save:  ¥5,942.28
MX$748.03
List Price:  MX$1,496.24
You save:  MX$748.21
QR145.70
List Price:  QR291.45
You save:  QR145.74
P539.02
List Price:  P1,078.19
You save:  P539.16
KSh5,164.70
List Price:  KSh10,330.70
You save:  KSh5,166
E£1,943.28
List Price:  E£3,887.05
You save:  E£1,943.77
ብር5,713.49
List Price:  ብር11,428.40
You save:  ብር5,714.91
Kz36,469.08
List Price:  Kz72,947.28
You save:  Kz36,478.20
CLP$38,935.86
List Price:  CLP$77,881.46
You save:  CLP$38,945.60
CN¥285.16
List Price:  CN¥570.39
You save:  CN¥285.23
RD$2,519.01
List Price:  RD$5,038.65
You save:  RD$2,519.64
DA5,195.64
List Price:  DA10,392.59
You save:  DA5,196.94
FJ$90.43
List Price:  FJ$180.89
You save:  FJ$90.46
Q306.48
List Price:  Q613.04
You save:  Q306.55
GY$8,361.95
List Price:  GY$16,726
You save:  GY$8,364.04
ISK kr4,927.16
List Price:  ISK kr9,855.56
You save:  ISK kr4,928.40
DH361.71
List Price:  DH723.51
You save:  DH361.80
L659.41
List Price:  L1,318.99
You save:  L659.58
ден2,114.98
List Price:  ден4,230.49
You save:  ден2,115.51
MOP$321.49
List Price:  MOP$643.07
You save:  MOP$321.57
N$708.48
List Price:  N$1,417.15
You save:  N$708.66
C$1,471.59
List Price:  C$2,943.55
You save:  C$1,471.96
रु5,644.07
List Price:  रु11,289.56
You save:  रु5,645.48
S/141.34
List Price:  S/282.72
You save:  S/141.37
K167.68
List Price:  K335.40
You save:  K167.72
SAR150.05
List Price:  SAR300.15
You save:  SAR150.09
ZK947.44
List Price:  ZK1,895.13
You save:  ZK947.68
L174.20
List Price:  L348.45
You save:  L174.24
Kč838.79
List Price:  Kč1,677.79
You save:  Kč839
Ft13,507.75
List Price:  Ft27,018.89
You save:  Ft13,511.13
SEK kr377.45
List Price:  SEK kr755
You save:  SEK kr377.55
ARS$54,365
List Price:  ARS$108,743.61
You save:  ARS$54,378.60
Bs276.30
List Price:  Bs552.67
You save:  Bs276.37
COP$160,356.77
List Price:  COP$320,753.65
You save:  COP$160,396.87
₡20,216.02
List Price:  ₡40,437.11
You save:  ₡20,221.08
L1,053.98
List Price:  L2,108.23
You save:  L1,054.24
₲288,815.03
List Price:  ₲577,702.28
You save:  ₲288,887.25
$U1,600.48
List Price:  $U3,201.37
You save:  $U1,600.88
zł145.87
List Price:  zł291.78
You save:  zł145.90
Already have an account? Log In

Transcript

In the previous tutorial, we talked about the switch statement how and how important it is, in case we want to do a specific thing, depending upon the value of a variable. But there is another alternative for switch statement, which is called if statement. And actually, it's much more important than switch statement and it's commonly used more than switch statement. Let's now create a similar example as this one. Suppose that we have a variable called gender, and the value of that gender variable is male. And again, we want to determine what we want to do if the value of the gender is male.

And what we want to do is devalue and what we want to do if the value of that variable is A female. So instead of using switch, we will use something called if statement. And to create a if statement, you just type if, and then parentheses and inside these parentheses, you add what you want to test. So for example, I want to test to the value and check the value of this gender variable. So here inside these parentheses, I'm going to type gender. And then I'm going to add double equal sign.

And then I'm going to add meal. And remember, when we want to check the type as well as the value, we use triple equal signs. But if we want to check only the value, regardless of the type, we should just use double equal signs. And in our case, we it's better to use triple equal sign and then we add curly braces here and inside here, we type the code that we wanted to be implemented if the value if the value of the gender equals male, so I want just to print as before, I just wanted to be printed on the screen. So I'm going to use console dot log, and I'm going to add male inside it male. And in order to check if it was a female, we can use something called ILS.

If else e L is E, and then if and again, parentheses and inside them you just add gender, gender and then equals, triple equal sign, and then female. And, again, curly braces. And again, you can add console log. One more time here and change from male to female. See male and finally We can add something called else. And and what else means is that if the value does not equal the first one, and also if it doesn't equal the second one, it is going to implement this value, meaning that this is some kind of a default, as we learned before.

In switch switch statement. It's the default value. So here I'm going to use console dot log. And then I'm going to say gender has not been chosen to choosing. And let's save now and run. And if I save and run, I get mail because the current value of gender is male.

If I change this to female, the male if I save and run, I get female. But if the value of gender has not been specified at all, if it's undefined if I save and run it Yet gender has not been chosen. So as you can see here, in if statement, what we do is just we add if for the first value and then else if for other values. Of course here we can add more ifs here. For example, if we have a variable that has more than one value that has more than two possibilities, we can add here is F's, and then we add ales for the default value

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.