Decision Structures

Introduction to Programming Decision Structures
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

Welcome to Lesson 13. In this lesson, we're going to talk about decision structures and Boolean logic. We need these in our programming so that we can ask questions and say, Hey, if this exists, then we need to do this. Whenever we asked a program with a question, basically what we're doing is we're creating a decision structure. So a decision structure allows a program to perform actions only under specified conditions. And the decision structure controls how those statements execute.

If you look at the example of what I did in Raptor, for example, we can ask the user is a cold outside, enter Y for yes and for now. And we can call that variable decision because when the user puts in a y or an N, we have to have something We're going to keep that value. And we're going to keep that in the variable decision. So that way we can tell the program to look at them. What's in that variable decision? What is the value?

If that value is yes. Then you notice on the left side, we have a yes. And so we're going to follow the arrows. And if it's a yes, then we say, put, wear a coat. We're going to wear a coat actually, because they're just cold outside. So if the decision is no, which would make decisions equal, why false, it would know to go to the no side.

And you would print no need for a coat. And then that ends the program. So based on what the user puts in, if we ask a user all the user sees is is a cold outside and and why for yes and no and for no And maybe today is cold, so we click Yes. And then it will come up on the print screen and say wear coat. Otherwise, it will say no need for a coat. So it is a decision structure.

If this is true, it's the fact that we have y in the value of the variable decision. And that's true, because that's what the user put in, then this is what we're going to do, we're going to wear a coat. Otherwise, if it's in and it doesn't match up, we're going to say, put no need for a coat and end that. There are some relational operators that we can use in our decision structure and our Boolean Boolean logic logic. In the previous example, we could have said, If, excuse me, if the temperature was lower than 40 degrees, then print wear coat If the tensions above 40 degrees, no need for Co. So we can use these operators in place of that we can say, the greater than operator.

So we could say one thing is greater than another and test that. And whichever one is true, that will be the statement for that one. We could use less than, greater than or equal to less than or equal to equal equal to or not equal to, maybe you want a user or a list of clients that are in the area, but not equal to your exact area. So if I lived in Boston, I could say I want every client except for those that live in Boston. And then that would give me a list and it would keep out everyone that lives in Boston, we wouldn't be able to see those people. So we can use these operators in Python, we can use them in many other languages like C sharp, we can Use them in c++, we can use them in just about in Java and just about any language, so that we need to consider.

The other thing that's super important is if you look at the equal to, you notice there's two equal signs there. Please do not confuse that with an assignment statement. If I create a variable and I say int number equal three, then I use one equal sign and that is an assignment variable, I have assigned the number three, okay to that variable. If I use the equal equal, that's really more of a comparison of values. So if number one is equal equal to number two, then do this. So there is a little bit of difference.

Make sure you keep that in mind. Another type of operator you'll use in decision structure and Boolean logic or logical operators. So and or not, are logical operators. So if I say, and both expressions need to be true, so if it was 40 degrees and humidity was 50%, for example, both expressions would have to be true before you could say wear a coat for the or one expression must be true. So we could say if it was 40 degrees, or 50 degrees, wear a coat. Now, of course, in something like that it's going to be literally 40 or 50 degrees, anything in between isn't going to work.

But I just want to say that you can be one or the other and if either expression is true, then the statement will run not completely reverses the truth. So it has be false. So if I wanted every client, except for the clients that live in Boston, for example, we would use the NOT operator because we don't want the people that live locally. Maybe we only want people that live at a distance. All right. So when you work with Boolean variables, they also have to be declared initialized like a standard variable.

A Boolean variable can hold one or two values, though it is either true or it's going to be false. So a boolean variable it has to be declared. So you see the example here is declare Boolean is cold. So we're saying that the type is Boolean, and the name that we've given that variable is is called the variable must be instantiated. So again, we have to set it to a value. So when we start with it, maybe we want is called to be set to true If it's true, here's your statement.

If it's not, then do this other statement. You cannot use any variable without giving it a value first. So even when you're asking the user to input a variable, a variable or a value in this variable, you still have to make sure that you give it a value. Before you start your program, it doesn't matter. It could be zero. It could be true, it could be false.

It could be depends on the type of variable that you're using, but you have to get value to start. Yay, great job. We just learned about decision structures and Boolean logic. So now let's learn about some repetition structures. See you soon.

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.