• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

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

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 【用代数讲计算机科学】条件语句

CS in Algebra: Conditionals

Booleans allow us to start asking questions in our programs such as, is the value of age

greater than 15? or does name equal John?

This lets us start introducing logic into our programs but to make them really smart,

we want our programs to be able to make decisions based on the answers to Boolean questions.

Conditionals let us do exactly that.

A conditional lets you program a list of Boolean questions and their associated code.

The conditional starts at the top of your list of questions or conditions and checks

whether that question is true or false.

If the condition is true, then the associated code is evaluated as a result.

If the condition is false, the associated code is ignored and we move on to the next

condition in the list.

Here’s a simple conditional to check the variable age and return a string describing someone

of that age.

Let’s see what happens when we evaluate with this age set to 15.

What about when age is 3?

Now, what should happen if age is 50?

None of our conditions evaluated true for 50.

That’s a bit troublesome.

If none of the conditions are true, we can provide a catch-all value called the else

statement.
else语句只在所有条件为假时执行
The else statement only runs if none of the other conditions are true.

So by adding adult to the end of our conditional, we can catch ages outside of the range of

our existing conditions.

The order in which you place your conditions is very important.

The first condition that returns true is the one that wins.

Let’s look what happens if we put our age-checking conditionals in the reverse order.

What would you expect it to return with age set to 15?
3 或者50呢?

In the next stage, you’ll write programs that use conditionals to make logical decisions.

Every conditional needs to have at least one condition and an else statement.

If you need to check against more conditions, click the plus button above the else statement.

If you want fewer conditions, click the minus button next to the condition you wish to remove.

【B】Cod