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
€66.45
List Price:  €94.93
You save:  €28.48
£55.32
List Price:  £79.03
You save:  £23.71
CA$98.04
List Price:  CA$140.06
You save:  CA$42.02
A$107.72
List Price:  A$153.89
You save:  A$46.17
S$94.09
List Price:  S$134.42
You save:  S$40.33
HK$544.68
List Price:  HK$778.15
You save:  HK$233.46
CHF 61.92
List Price:  CHF 88.46
You save:  CHF 26.54
NOK kr774.79
List Price:  NOK kr1,106.89
You save:  NOK kr332.10
DKK kr495.67
List Price:  DKK kr708.13
You save:  DKK kr212.46
NZ$118.82
List Price:  NZ$169.76
You save:  NZ$50.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,339.40
List Price:  ৳11,913.94
You save:  ৳3,574.54
₹5,914.23
List Price:  ₹8,449.26
You save:  ₹2,535.03
RM311.24
List Price:  RM444.65
You save:  RM133.41
₦118,083.62
List Price:  ₦168,698.12
You save:  ₦50,614.50
₨19,391.36
List Price:  ₨27,703.14
You save:  ₨8,311.77
฿2,410.17
List Price:  ฿3,443.25
You save:  ฿1,033.08
₺2,423.71
List Price:  ₺3,462.60
You save:  ₺1,038.88
B$418.75
List Price:  B$598.25
You save:  B$179.49
R1,273.04
List Price:  R1,818.71
You save:  R545.66
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩97,732.12
List Price:  ₩139,623.30
You save:  ₩41,891.18
₪255.55
List Price:  ₪365.09
You save:  ₪109.53
₱4,110.61
List Price:  ₱5,872.56
You save:  ₱1,761.94
¥10,630.43
List Price:  ¥15,186.98
You save:  ¥4,556.55
MX$1,418.07
List Price:  MX$2,025.91
You save:  MX$607.83
QR254.36
List Price:  QR363.39
You save:  QR109.02
P953.37
List Price:  P1,362.02
You save:  P408.64
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.89
List Price:  E£4,970.06
You save:  E£1,491.16
ብር8,820.94
List Price:  ብር12,601.88
You save:  ብር3,780.94
Kz63,759.63
List Price:  Kz91,089.09
You save:  Kz27,329.46
CLP$68,404.72
List Price:  CLP$97,725.22
You save:  CLP$29,320.50
CN¥507.53
List Price:  CN¥725.08
You save:  CN¥217.54
RD$4,206.74
List Price:  RD$6,009.89
You save:  RD$1,803.14
DA9,351.13
List Price:  DA13,359.34
You save:  DA4,008.20
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.41
List Price:  Q769.20
You save:  Q230.78
GY$14,600.65
List Price:  GY$20,858.96
You save:  GY$6,258.31
ISK kr9,615.92
List Price:  ISK kr13,737.62
You save:  ISK kr4,121.70
DH699.24
List Price:  DH998.96
You save:  DH299.72
L1,281.52
List Price:  L1,830.82
You save:  L549.30
ден4,084.06
List Price:  ден5,834.63
You save:  ден1,750.56
MOP$559.24
List Price:  MOP$798.95
You save:  MOP$239.71
N$1,266.15
List Price:  N$1,808.86
You save:  N$542.71
C$2,568.06
List Price:  C$3,668.81
You save:  C$1,100.75
रु9,427.87
List Price:  रु13,468.97
You save:  रु4,041.09
S/262.66
List Price:  S/375.25
You save:  S/112.58
K281.39
List Price:  K402.01
You save:  K120.61
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,903.42
List Price:  ZK2,719.29
You save:  ZK815.86
L330.70
List Price:  L472.45
You save:  L141.75
Kč1,679.39
List Price:  Kč2,399.23
You save:  Kč719.84
Ft27,552.80
List Price:  Ft39,362.83
You save:  Ft11,810.03
SEK kr765.72
List Price:  SEK kr1,093.94
You save:  SEK kr328.21
ARS$70,549.59
List Price:  ARS$100,789.45
You save:  ARS$30,239.85
Bs482.26
List Price:  Bs688.98
You save:  Bs206.71
COP$307,907.55
List Price:  COP$439,886.78
You save:  COP$131,979.23
₡35,643.95
List Price:  ₡50,922.11
You save:  ₡15,278.16
L1,765.02
List Price:  L2,521.57
You save:  L756.54
₲545,488.80
List Price:  ₲779,303.12
You save:  ₲233,814.31
$U2,997.83
List Price:  $U4,282.79
You save:  $U1,284.96
zł286.31
List Price:  zł409.03
You save:  zł122.72
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.