• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 商业

BUSINESS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 《卷轴教程之C++入门》#12 条件语句四

Using Conditional Statements in C++ Part 4

All right. So I’m going to click on save and then click on the bulid button.

Build this program. That’s compiling and linking it.

Now, we’ll run it.

So down here on the console says enter the seed color

so I’m going to enter in red.

Enter the temperature. I’ll say the temperature is 85.

And the soil moisture is dry.

So it outputs “A dandelion will grow” and it’s that what we expected.

So if we go back up here to the paragraph dealing with a red seed

it’s above 75 degrees

and the soil is dry then it’s going to produce a dandelion.

And that’s what we expected if you look at our logic, that’s exactly what happened.

So we have a red seed, temperature was greater than or equal to 75

and the soil moisture was dry so it outputs the dandelion.

So very basic stuff here.

and hopefully that make sense.

Let’s run it one more time just test another path of it exeuction here.

So run this, this time we’ll type in blue.

We’ll say the temperature is 45.

And enter the soil moisture:wet

So it says a mushroom will grow. Let’s check that out, make sure that that is true.

So if we have a blue seed

and for a flower to grow it has been between 60 and 70.

In our case we had 45.

And then it says that so if it doesn’t meet the temperature range here

then it will grow mushroom that’s exactly what we got.

We can look at our logic to confirm that. Blue

so the temperature was not between this range

So we ended up with the “else”.

So we tested this here, this turned out to be false.

So it ends up doing whatever we have here in the else operation

so mushroom will grow.

Now something that you probably noticed here

is that we’re actually asking for the soil moisture

after we have got the temperature.

There is really no reason to ask for the soil moisture

if we know that we will end up with a mushroom.

So that’s maybe something we can go and change in our program.

Okay, so let’s look at the moving this code here where we get the soil moisture.

Down to the point where it actually matters.

So we are going to move these 3 lines of code

down below for them in the case the red seed

after we know that we have a temperature greater than or equal to 75.

Because only at that point time, do we take into account

the soil moisture being wet or dry.

So if we fall into a situation where temperature is less than 75

we’re know…we know that we’re going to get a mushroom.

Doesn’t matter what the soil moisture is.

It’s the same case with the blue seed.

The blue seed is not falling in this range here in term of the tempetature.

Then we’re all end up with a mushroom

so there is no need to ask for the soil moisture

until we know that we’re in the correct temperature range.

So I’m gonna just take this code here and select it.

I’m just dragging over, left clicking and dragging.

And then I’m going to use hold down “ctrl”

and then hit the “x” key

and that will cut that bit of code.

And you see that the code analyzer here in Eclipse

was already complaining it’s already saying that

we have some unresolved variables here but as soon as we come in here

inside of this if statement where we have the if

temperature is greater than or equal to 75

and paste it we’ll hold down “ctrl”

and then hit the “v” key to paste.
eclipse的代码分析器就不会再有警告了
Then the eclipse code analyzer is not complaining any more.

That’s now resolved

still unresolved down here. So let’s go ahead and

paste in this bit of code down here as well.

So “ctrl” “v” once again.

and we paste that code in.

So now we’re not asking for the soil moisture until it actually matters.

So let’s go ahead and save this

build it

and we’ll run it.

So we’ll put in a red

the seed color

And we’ll put in a temperature of 65, so that’s down below

where we get a flower.

And it says a mushroom will grow and notice how we did

not ask the user for the soil moisture.

And that’s good.

We don’t want to ask them unnecessary questions

if we already know ahead of time that

we’re not going to get that result.

Let’s run it one more time.

And we’ll do it for blue seeds.

So we’ll type in blue

and we’ll put in a temperature of 95.

And it says a mushroom will grow.

So we exercised

the second case here if the seed color is blue.

All right, so let’s take a look at our code

and notice how we have this if statement here to test if we have a red seed.

And then we also have another if statement here to see if we have a blue seed.

And you may be wondering what would happen if we change this

to an else statement.

So that’s saying that

if the seed color is not red

then it has to be blue.

And that may not be the case right so somebody could actually input

the color green or brown or maybe not even a color at all.

They could input anything.

So anything other than red

would actually come down and try to execute

whatever we have here in this else statement here.

So if the temperature happened to fall between 60 and 70

then we’ll ask for the soil moisture.

And if it happened to be wet,

we would output that a dandelion would grow.

They may have been putted, you know, that the seed color

was, you know, hamburger or something like that

something that doesn’t even make any sense.

So we have to be careful whenever we use this else here.

We have to know that we have valid input

and that we exhausted all other alternatives.
else语句中剩下的
And the only thing else left

is this stuff here.

That’s where we put how we would use the else statement.

We could, I guess, leave the else in there.

If we came up here and maybe did something like this.

So after we get the seed color test to see if

the seed color

if the seed color is not equal to

red

and

the seed color

is… seed color…

seed color is not equal to blue

If that turns out to be the case, maybe we’ll just output to the user…

We’ll do a “cout”, and then say
“颜色选择无效，
“Invalid color selected”

“Program will terminate”

Okay, so we could have some bit of code like this

and then we could do maybe a return zero and now to actually

terminate our program at that point of time.

So if we had that, it would actually be okay

to have an else statement in here.

Because if they input anything other than red or blue

it’s going to come in here and execute this statement

and then do the return zero meaning it’s going to exit out of our main program

our main function.

Notice how we did construct the particular boolean expression here.

We said seed color not equal to red and seed color not equal to blue.

You have to be very careful. If we were to use the “or” here.

It opposites to the “and” operation.

Let’s notice what happens because this is a common mistake

as well that someone who put an “or” here instead of an “and” statement.

So with the “or” maybe they type in blue. Okay.

So blue is not equal to red, right?

So this would be a true statement.

And we only have to have one true statement

in order for

this whole thing, in order for this whole entire expression to be true.

So we would end up executing this statement here and that’s not what we wanted to have.

We needed to have

both of these being true.

So if they type in blue

this would turn out to be true

and

this would be blue is not equal to blue.

That’s…

that is in fact false.

So true and false for the “and” operation the whole expression turns out to be false.

So we wouldn’t execute this bit of code here.

And that’s what we want to happen if they typed in blue.

So do be careful

between using the “and” and the “or” operations and test out your logic.

All right, so let’s see how this works.

I’m going to save it

build it

and run it once again.

All right, so we’ll type in

brown.

And it says “invalid color selected. Program will terminate.”.

That’s exactly what we expected to happen.

So go back and run it one more time

and make sure it does work if we type in blue.

So, let’s type in blue.

And we’ll put in a temperature of 75.

And it has a mushroom will grow.

That’s exactly what we except because it did not fall into this temperature range.

Let’s scroll down a little further to get to the blue section.

So it didn’t fall them in between the 60 and 70 range.

So we ended up with a mushroom here.

So it’s perfectly okay to have the else statement like we have here now

as long as we validated our input through this condition here.

So we provided this conditional statement.

All right, so maybe the last thing that we’ll do with this program is

actually do some input validation for our soil moisture.

So much like we did here.

For the seed color

can do similar things for the soil moisture.

So we’ll come down here and

write some code below this.

So after we get the soil moisture, we can test to see if

soil moisture

is not equal to wet.

and soil moisture is not equal to dry.

What do we know? we know that we have invalid input there.

So we’ll say “cout”
“无效……”
and say “invalid”

say “invalid soil moisture”

“was inputted”, maybe “inputted” is a better word to use

and then we’ll say “program will terminate”.

All right.

So we call do that and then do a return zero and that actually ends the execution of the program.

All right, so it exits the main function just returns it to operating system control.

All right, so we have got that and that looks correct hopefully.

So the soil moisture is not equal to wet and not equal to dry

that means we do not have a soil moisture that we expect to make use of.

And since we have that bit of code, we could actually go in here

and have this if statement here but have this as an else.

So we can change this bit of code and have this as an else much like we did before.

And we can copy this bit of code here

copy that and then paste it down here for the blue one as well

do input validation as well here.

And then what can we do?

We could take this out and change it to an else.

Because we know if we make it past this point here, after this if statement here

we make it pass, this ends up not being true

then we must have the soil moisture being wet or dry.

Okay, so that’s the reason why we can do that else there

because if it’s not wet

it has to be dry if we’ve made it pass this condition here.

Now one thing that may be bothering you about this code is

that we have replication of code segments in several places here.

Right, so we have this replication of code here and reputation of code here.

And hopefully that’s a little bit unsettling to you.

As you start programing more and more, anytime you see replication of code

that should be upsetting. But latter on we’ll look at

maybe being able to get rid of this replication of code

and write a function if we are not interested in doing the same operation again.

We’ll just simply call a function to do that task

that suppose to rewritting the same code over.

All right, so I think that pretty much wraps up our discussion of program for.

I guess, the last thing we can do is save our program and

maybe build it and run it a couple of times.

So let’s build this and run it.

So go ahead and type in red

and the temperature we’ll say is 75.

And we’ll put the soil moisture as…

I don’t know, let’s make up something it doesn’t make any sense which just say dirt

the soil moisture is dirt

and hit enter and it says “Invalid soil moisture was inputted. Program will terminate.”

Only the other thing I guess we may do is

we could just say soil moisture with a space we don’t have to use

the variable name inside that string literal.

So we change that and put a space in there.

Then we can save and rebuild that.

Maybe we’ll check out the blue and see if it works.

So blue.

And than we’ll put a temperature of 65.

And the soil moisture is we’ll say “moist” which is not a valid selection.

So invalid moisture was inputted. Program will terminate.

That’s exactly what we expect.

So we need time of constructing a program, you may have multiple alternatives or multiple paths

that you can select through the program.

It’s good to try to test and exercise all those paths to make sure that the program works.

And we certainly haven’t exercised all the paths for this program

so I encourage you to, you know, continue playing around this program

and maybe see how you could improve it.

I’m not saying it’s perfect.

##### 译制信息

[B]hugue

[B]hugue

https://www.youtube.com/watch?v=PCucIVDFqyk