Using Loops in combination with Conditionals

This week, let’s discuss combining two concepts we have learnt in the last few weeks: loops and conditionals. As discussed in those blog posts, loops are used to execute a code of block multiple times and conditionals are used to define if a particular block of code will be executed or not.

Let’s start with repeat until block in SNAP!

Repeat until block in SNAP! block is used when a loop needs to run until a particular condition is met. Unlike repeat block, which is run a set number of times, repeat until block is not defined to run a set number of iterations.

Using forever with if block

Another way we can use conditional loop statement is by combining forever block with if or if-else block. For example, if we run if block inside forever block, it will look like: Forever with If block. Using Loop with Conditional Blocks and any blocks inside if will be executed multiple times in the program until the condition for if block is true.

What is the difference between repeat until and forever-if block?

The condition in repeat until block is a termination condition, while in forever-if block it is a continuation condition.

Understanding the simple difference in these conditions will help you in defining the variable flow of the code in your program.

Feel free to post your questions or feedback in the comments below. Additionally, if you would like me to cover additional topics, you can let me know in the comments.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *