From Test-Scratch-Wiki

如果 () 那么(积木)
如果 () 那麼
类别 控制类积木
形狀 半框形积木

The If () Then 积木 is a 控制类积木 and a 半框形积木. The block will check its 菱形积木 condition. If the condition is true, the blocks held inside it will run, and then the 程式 involved will continue. If the condition is false, the code inside the block will be ignored and the script will move on (unlike in the 如果 () 那么,否则 block). The condition is only checked once; if the condition turns to false while the script inside the block is running, it will keep running until it has finished.

Before Scratch 2.0, this block was named If ().

用法示例

In programming, one of the most important parts is "checking conditions". This block is the simplest way to do that. Therefore, the block is used practically everywhere. Some common uses:

  • Comparing values
如果 <(詢問的答案) = [5]> 那麼 
  說出 [Correct!]
end
  • Checking if input is given
如果 <<滑鼠鍵被按下?> 且 <(amount) = [1]>> 那麼 
  蓋章
end
  • Controlling objects
如果 <[空白 v] 鍵被按下?> 那麼 
  廣播訊息 [Jump v]
end

应用

Main article: List of Block Workarounds

This block can be replicated with the following code:

如果 <condition> 那麼 
  . . .
否則
//Leave empty.
end

Another workaround is this:

. . .
廣播訊息 [if statement v] 並等待
. . .

當收到訊息 [if statement v]
重複直到 <conditional> 
  停止 [這個程式 v]
end
. . .

Common Issue

Some users are confused as to why a script does not work when using the block. One of the most common misconceptions about it is that it repeatedly checks for a condition,[1][2] so some users do not understand why a script isn't working with only the if block. To make it repeatedly check a condition, it simply needs to be put in a 重复无限次迴圈.

Note Note: The 重复直到 and 重复 () 次 blocks can also be used to make a condition repeat.

The 重复无限次如果 () block in Scratch 1.4 did exactly what users with this misconception think the if block does.

参考

参见

Cookies help us deliver our services. By using our services, you agree to our use of cookies.