If () Then
if <> then
Category Control
Type C
Not to be confused with If () Then, Else (block).

The If () Then block is a Control block and a C block. If its Boolean condition is true, the blocks inside it will run. If the condition is false, the blocks inside the block will be ignored. 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.

Prior to Scratch 2.0, this block was called If (), or if <>::control.

Example Uses

The most basic uncertainty in programming is checking conditions. This block is Scratch's main way of doing this; it is therefore used in nearly all projects. Some common uses include:

  • Comparing values
if <(answer) = [5]> then
    say [Correct!]
end
  • Checking if input is given
if <<mouse down?> and <(amount) = [1]>> then
    stamp
end
  • Checking if a sprite is clicked

if <<mouse down?> and <touching (mouse-pointer v)?>> then

end

  • Controlling objects
if <key (space v) pressed?> then
    broadcast (jump v)
end

when I receive [jump v]
repeat (6)
    change y by (2)
end
repeat (6)
    change y by (-2)
end
  • Making sure a list contains a specific item
if <[list v] contains [1]?> then
    stop [all v]
end

Workaround

Main article: List of Block Workarounds

This block can be replicated by not utilizing the second field in the If () Then, Else block:

if <. . . :: grey> then
    . . .
else
end

Alternatively, this block can be replicated by passing the Boolean condition to the Repeat () block:

repeat <. . . :: grey>
...
end

This works because if the Boolean is true, it will run one time as putting a Boolean into a number reporter equals 1, but if the Boolean is false, it will repeat zero times which means the script will not run.

Common Issue

Some users are confused as to why a script does not work when using the block. One of the most common misunderstandings 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 or another kind of repetition.

forever
 if <. . . ::grey> then
...

The Repeat Until () and Repeat () blocks can also be used to make a condition repeat, but only for a finite amount of time.

repeat(6)
 if <. . . ::grey> then
 ...

See Also

References

  1. jkris11. (31/8/2021). "If(touching) not working[title]" topic:14843
  2. ssss. (22/1/2014). "Reason it's not working - the fruit needs to have
    forever
    if <> then
    " post:237085
Cookies help us deliver our services. By using our services, you agree to our use of cookies.