If () Then (block)

If () Then
2.0 If () Then.png
Category Control
Type C

The If () Then block is a Control block and a C block. The block will check its boolean condition. If the condition is true, the blocks held inside it will run, and then the script 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 If () Then, Else 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 ().

How to Write it in the Forums or Wiki

  • if <> then

end

Example Uses

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
if <(answer) = [5]> then
say [Correct!]
end
  • Checking if input is given
if <<mouse down?> and <(amount) = [1]>> then
stamp
end
  • Controlling objects
if <key [space v] pressed?> then
 broadcast [Jump v]
end

Workaround

Main article: List of Block Workarounds

This block can be replicated with the following code:

if <condition> then
. . .
else
//Leave empty.
end

Another workaround is this:

. . .
broadcast [if statement v] and wait
. . .

when I receive [if statement v]
repeat until <conditional>
stop [this script 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 forever loop.

Warning Note: The repeat until and repeat blocks can also be used to make a condition repeat, but only for a short amount of time.

The forever if block in Scratch 1.4 did exactly what users with this misconception think the if block does.

References

  1. http://scratch.mit.edu/discuss/topic/14843/
  2. http://scratch.mit.edu/discuss/topic/26823/?page=1#post-237085

See Also

  • This page was last modified on 5 January 2018, at 07:35.