Loop – What is it ?
17/08/2021 | 2min
A loop is a structure that allows you to easily repeat commands lot, there are different types of loops in programing.
infinite loop
An infinite loop is a loop as no condition , The result is that the loop repeats continually.while
A while loop checks a given condition before it executes the loopdo while
A do while loop checks a given condition after it executes the loop- ...
How to create infinite loop with command block
For an infinite loop, place a simple repeating command block and set it to automatic. (1)
Connect the other chain command blocks to execute the commands in the loop. (2)
Get repeating command block:
give @p repeating_command_block
Get chain command blocks:
give @p chain_command_block
For stop the infinite loop, set to Always Active
to Needs Redstone
(in the repeating command block GUI editor)
How to create while with command block
To create a while loop, create a condition after the repeating command block (1).
To create a condition we can use the /execute
command (3).
pattern:
/execute <condition> run <command>
In this example, the condition checks if the block at ~ ~1 ~
position is a dirt block and if the condition is true then stop the loop with /setblock
command (or /data modify block
).
Chain command block (3.1)
execute if block ~ ~1 ~ dirt data modify block ~-1 ~ ~ auto set value 0
Chain command block (3.2)
execute if block ~-1 ~1 ~ dirt run setblock ~2 ~ ~ minecraft:gold_block
Chain command block (3.3)
execute unless block ~-2 ~1 ~ dirt run setblock ~1 ~ ~ minecraft:chain_command_block[facing=south] replace
And connect the other chain command blocks to execute the commands in the loop. (2)
How to create do-while with command block
To create a do-while loop, create a condition before the repeating command block (1).
In this example, the condition checks if the block at ~ ~1 ~
position is a dirt block and if the condition is true then stop the loop with /setblock
command. (or /data modify block
)
execute if block ~ ~1 ~ dirt run setblock ~ ~ ~-2 minecraft:repeating_command_block[facing=east]{auto:0} destroy
with command /data modify block
execute if block ~ ~1 ~ dirt data modify block ~ ~ ~-2 auto set value 0