(Redirected from If Else)
|If () Then, Else|
The If () Then, Else block is a Control block and a C block. The block will check its boolean condition. If the condition is true, the code held inside the first C (space) will activate, and then the script will continue; if the condition is false, the code inside the second C will activate (unlike the If () Then block).
In Scratch 1.4, this block was named If (), Else.
In programming, a very important part is "checking conditions." this is done with the If () block. However, an important part of the "checking conditions" is having another piece of code that runs if the condition is false. While this may be worked around, the If (), Else block makes this simpler. Some common uses:
- Do "this" (the code inside the first C) or "that" (the code inside the second C)
repeat until <touching [edge v]?> if <(loudness) > > then say [No loud noises.] for (2) secs else move (10) steps end end
- If a sprite's health is below a certain amount, it dies, otherwise it does something else
when gf clicked forever if <(health) = > then play sound [Failure v] stop [all v] else broadcast [attack v] end
- Easy script changes — if a variable equals a certain value, one thing happens, and if the variable does not equal the value, a different thing happens
if <(answer) = > then say [Correct!] for (2) secs else play sound [task fail v] until done stop [all v] end
- Scripts that are adaptable to changes in conditions (such as a changing variable)
if <touching color [#7092be]?> then say [We found water!] else say [ ] end
- Main article: List of Block Workarounds
This block can be replicated with the following code:
set [action v] to (0) if <. . .::grey> then set [action v] to (1) . . . //condition=true end if <(action) = (0)> then . . . //condition=false end
when I receive [if-statement v] if <. . .::grey> then . . . // condition=true broadcast [continue v] stop [this script v] end . . . // condition=false broadcast [continue v]
when I receive [continue v] . . .
if <. . .::grey> then . . . //condition=true end if <not <. . .::grey>> then . . . //condition=false end
Note that this last workaround only works if the condition will not be negated in the middle of the first If () block.
(if <> then  else ::operators)
It would be in the Operators category.
This block does exist in some Scratch modifications, such as Insanity.