Switch Statement

5 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.30
List Price:  €130.62
You save:  €37.32
£79.79
List Price:  £111.72
You save:  £31.92
CA$136.51
List Price:  CA$191.13
You save:  CA$54.61
A$152.32
List Price:  A$213.26
You save:  A$60.93
S$135.97
List Price:  S$190.37
You save:  S$54.39
HK$782.61
List Price:  HK$1,095.68
You save:  HK$313.07
CHF 91.14
List Price:  CHF 127.60
You save:  CHF 36.46
NOK kr1,099.65
List Price:  NOK kr1,539.56
You save:  NOK kr439.90
DKK kr695.84
List Price:  DKK kr974.21
You save:  DKK kr278.36
NZ$167.38
List Price:  NZ$234.34
You save:  NZ$66.95
د.إ367.24
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,987.33
List Price:  ৳15,382.71
You save:  ৳4,395.37
₹8,345.51
List Price:  ₹11,684.05
You save:  ₹3,338.54
RM476.90
List Price:  RM667.68
You save:  RM190.78
₦132,499.74
List Price:  ₦185,504.94
You save:  ₦53,005.20
₨27,870.93
List Price:  ₨39,020.42
You save:  ₨11,149.48
฿3,701.15
List Price:  ฿5,181.76
You save:  ฿1,480.61
₺3,236.01
List Price:  ₺4,530.55
You save:  ₺1,294.53
B$511.64
List Price:  B$716.32
You save:  B$204.68
R1,874.13
List Price:  R2,623.86
You save:  R749.72
Лв182.50
List Price:  Лв255.51
You save:  Лв73
₩137,667.26
List Price:  ₩192,739.67
You save:  ₩55,072.41
₪376.30
List Price:  ₪526.84
You save:  ₪150.53
₱5,769.57
List Price:  ₱8,077.63
You save:  ₱2,308.05
¥15,587.34
List Price:  ¥21,822.90
You save:  ¥6,235.56
MX$1,712.57
List Price:  MX$2,397.67
You save:  MX$685.09
QR365.03
List Price:  QR511.06
You save:  QR146.02
P1,378.03
List Price:  P1,929.30
You save:  P551.26
KSh13,498.65
List Price:  KSh18,898.65
You save:  KSh5,400
E£4,784.52
List Price:  E£6,698.52
You save:  E£1,914
ብር5,745.61
List Price:  ብር8,044.09
You save:  ብር2,298.47
Kz83,397.65
List Price:  Kz116,760.05
You save:  Kz33,362.40
CLP$95,071.49
List Price:  CLP$133,103.89
You save:  CLP$38,032.40
CN¥724.13
List Price:  CN¥1,013.82
You save:  CN¥289.68
RD$5,869.02
List Price:  RD$8,216.87
You save:  RD$2,347.84
DA13,441.55
List Price:  DA18,818.71
You save:  DA5,377.16
FJ$225.51
List Price:  FJ$315.73
You save:  FJ$90.21
Q778.65
List Price:  Q1,090.14
You save:  Q311.49
GY$20,944.44
List Price:  GY$29,323.05
You save:  GY$8,378.61
ISK kr14,022.59
List Price:  ISK kr19,632.19
You save:  ISK kr5,609.60
DH1,011.24
List Price:  DH1,415.78
You save:  DH404.53
L1,769.74
List Price:  L2,477.71
You save:  L707.96
ден5,744.06
List Price:  ден8,041.92
You save:  ден2,297.85
MOP$807.26
List Price:  MOP$1,130.19
You save:  MOP$322.93
N$1,888.87
List Price:  N$2,644.49
You save:  N$755.62
C$3,684.35
List Price:  C$5,158.24
You save:  C$1,473.89
रु13,347.21
List Price:  रु18,686.63
You save:  रु5,339.41
S/376.08
List Price:  S/526.53
You save:  S/150.44
K386.44
List Price:  K541.04
You save:  K154.59
SAR375.02
List Price:  SAR525.05
You save:  SAR150.02
ZK2,652.95
List Price:  ZK3,714.24
You save:  ZK1,061.28
L464.20
List Price:  L649.90
You save:  L185.70
Kč2,348.23
List Price:  Kč3,287.62
You save:  Kč939.38
Ft36,571.12
List Price:  Ft51,201.04
You save:  Ft14,629.91
SEK kr1,091.69
List Price:  SEK kr1,528.41
You save:  SEK kr436.72
ARS$87,466.76
List Price:  ARS$122,456.97
You save:  ARS$34,990.20
Bs694.29
List Price:  Bs972.03
You save:  Bs277.74
COP$395,890.30
List Price:  COP$554,262.26
You save:  COP$158,371.95
₡50,869.89
List Price:  ₡71,219.89
You save:  ₡20,349.99
L2,472.19
List Price:  L3,461.17
You save:  L988.97
₲745,423.92
List Price:  ₲1,043,623.31
You save:  ₲298,199.38
$U3,863.50
List Price:  $U5,409.05
You save:  $U1,545.55
zł403.45
List Price:  zł564.85
You save:  zł161.39
Already have an account? Log In

Transcript

Hello, welcome to the 28th tutorial on the c++ programming series. And in this part we'll be looking at the switch statement. switch statements are essentially a simplified if else if else statement. And the related check for variable has a certain value you cannot use integrals or enumeration basically how you integrate or Charles kinds of strings technically that's a user defined name, user defined type even though you belong to the standard namespace. It's not part of the it's not starting to grow or it's not an enumeration I think it was pronounced interviewer of integral earring. Yeah.

So you can only use and and Charles and lots it's a very simple if else if statement doesn't need to do really a condition. The only condition that it does itself is allows you to specify a variable and then you can check if the variable equals in Nevada so you got a is greater And or if this is greater than this, and this is this, all this all these locks a very simple stem. And there are certain instances where you just want some kind of switch statement. So let's show you how to use a switch statement very extensible about very easily to add new conditions. So we're going to do is we're gonna create a variable, we're gonna call it superhero, we're gonna assign the value one to it. What we're gonna do is switch.

And this is the syntax of a switch statement, you have your expression, which is basically variable that you want to check, don't put superhero, then the constant is basically the comparison. So those superhero equals zero. And then statement is, whatever you want to do. Once this other code equals haven't noticed with 78, whatever, if you could have a for loop, you can happen with any sort of code you want. So what we We're going to do is we're going to do STD c mon STD and line and the default statement is basically the outs the equivalent if not the case your tree will fall back to this and in here we're just gonna put the CDC not a superhero semicolon, nephew right? I was gonna open up the debugger area since nobody's up Rob because he checks those superego zero now and I know the checks he just default to this statement but what we can do that another case of case is literally to add a case you do case then the You can put one colon, and then you put whatever thing you want to do.

Let me just show you something. Actually, let me show you this clip. And then I'll show you what the purpose of the break statement. Nothing now bring this at all to an in your day. He's not a superhero. Yeah, super power.

So let's see the superhero. Run this, and you get Yoda. So what we're going to do is, yeah, forgot to put in the break statement. And that's a good way to illustrate what the break statement does. The brace timer prevents the code from going to the next case or to the default case. And if you don't have a break statement, it doesn't do the check.

It's essentially as this this wasn't there, or this wasn't there. And both of these longer in the case, you might think of that sort of fault. No, we didn't because sometimes, you may just want to group a bunch of conditions together. You want you may do it this. Let me show you for a break here. And you might just do case two, case three, case four, case five and an order all the cases we want to do the same function until it's going to print out.

T case need to put quotation mark s, t and y when it changes we just need to input mode I forgot to put a break statement and we put we run a faith mode has come down here they'll supercritical zero not equal one none superhero equal to yes no breaks emanates witches so this one is no code here but uh, no breaks down miniver thankful for and then it goes to Princeton said then it's The break and then the exits out to the switch there. They have it for this tutorial. If you have any questions feel free to message us at support at sono system correct Hey, the email will be in the description in comment on this video with us directly messages via YouTube wherever you feel comfortable with or requiring for source code. We'll also be inscription in the next part of the series we'll be looking at functions and as usual, thanks for watching and I hope you have a nice day.

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.