• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

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

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

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

Using Conditional Statements in C++ Part 3

All right, so now that we have all of our code in here for the red seed.

Let’s go and do the same thing or something similar for our blue seed.

So we’ll come down here.

So we already have this first test here to see if seed color is blue,

and now we need to just come in and write some if statements, so ‘if’,

and this is gonna be something new as well,

if the temperature is between 60 and 70,

so you may be, you know, inclined to write this as
60小于等于temp
’60’ and then put less than or equal to ‘temp’,

and then put this here, so maybe something like this,

maybe be the way that you’ll write it in some mathematical notation,

so very strickly that’s saying the temperature is between 60 and 70.

Unfortunately you cannot get away with this in C++.

So let me show you the correct way that we write this expression.

So if we have two bounds that variable or value has to be between.

We have to express the variable being associated separately with both of those bounds.

So we could say ‘temp’. Well you could say ’60’ or ‘temp’ first.

You could say ‘temp >= 60’,
“与”temp小于等于70
‘&& temp <= 70',

so that’s the way we have to write that expression there.

So if we have a particular value and it needs to fall on a range,

we have to specify that particular variable

associated with both the lower range and the upper range.

And notice that I used the ‘&&’ boolean operator here

as opposed to the ‘||’ operator.

So if we put ‘||’, just to show you the difference here, if we put ‘||’ in here,

somebody may put in the temperature of 150,

that’s a very hot soil temperature, right?

So it would qualify here, so 150 is in fact greater than 60,

so this would be true.

So doesn’t matter if this part was false or not.

So really 150 doesn’t fall on the range.

We don’t need have the ‘||’ operation there, we need have the ‘&&’ opration.

Cos both this and the right hand side,

so both the left and right hand side need to be true.

All right, so come down and put a ‘{‘ in.

And we’ll come down a little bit below here right.

Right above other close brace.

So what we were saying is that all of this, opps I actually put my close brace in the wrong spot,

need be above the ‘Otherwise’ here.

So if the tempure is on our rage between 60 and 70.

We’ll do some code here, ‘otherwise’

which we say ‘else’ right, we’ll do this.

Okay if you want you can go ahead.

Do the output statement down here something scroll down a bit.

So we’ll do ‘cout’ and say
“蘑菇会长出来”
‘a mushroom will grow’.

okay, newline character.

Alright so now we can check to see if the soil is wet,

so we can see ‘if’,

‘soil moisture is wet’.

So do that test and

that turns out to be true.

We will output using ‘cout’ and say
“蒲公英会长出来”
‘a dandelion will grow’.

Alright and the same sort of logic here.

Again we could use the ‘else’ here as opposed to using ‘if’

assuming that we knew that the user input wet or dry.

Again we’re not doing any sort of input validation in this program.

But maybe for program 5, we’ll come back and revise program 4,

and show how to do some input validation.

And I’ll probably show some alternatives just how to write program 4.

And actually improve our logic and see what the difference is

whenever we change this stuff a bit. So soil moisture

and test to see if it’s equal to dry.

and so will come down here,

put in a close brace,

then do our ‘cout’,

and say ‘a sunflower will grow’.

Okay, that’s the basics of our program,

we’ll just check not chest but check to see

if we have a red seed or a blue seed.

and follow the appropriate logic within those.

So you could even have an ‘else’ operation here,

so if the seed wasn’t red,

we may be able to assume that was blue.

So you can actually put here an ‘else’.

We’ll come back and change that just to see what the difference is,

whenever we have the ‘else’ there versus having the if statement there.

All right another thing that I will point out real quick

is that if we only have a single statement

that we want to execute associated with ‘if’.

So if this is true we want to do this output here.

so that’s the only statement that we want to have,

we do not have to have this open brace close brace

defining the body of this ‘if’ statement being true.

And the same thing goes here,

the same thing over here well that’s it that’s not true,

I guess we do need to have… oh no we don’t have to have these

these braces either because we only have that single statement.

But if you have multiple statements associated with the ‘if’ or ‘else’,

then you definitely need to have those braces.

For me and the way that I do my programming always,

always put those braces in regardless if I have

a single statement or more than one statement.

I already have those braces there.

And also find it’s just easier to read for me,

but maybe that’s just the what I’m accustomed to.

So you can make that decision if you only have a single statement.

Then you can just ignore putting in those open brace close brace.

Oh we actually forgot to do our output statement here for this ‘else’,

so this should be ‘cout’ and then we have

‘a mushroom will grow’.

Alright so I think that’s it.

So I think we’re ready to save this program,

then run it and see how this program works.

Being