In my last article, we learned some topics like strings, operators etc. in the Ruby language. Likewise, today we are going to learn about some important concepts in Control structures of Ruby. You have already learned this concept in other languages but here it has important expressions and conditions. You can read the first part here -

Ok, let us see the content that we are going to learn.

CONTENTS

Unless Expression

The Unless expression is the opposite of an If expression. It executes the code when a condition is false.

Basic Concepts Of Ruby Language

In the above code, you can see the usage of the Unless keyword a < 10. The condition is true but Unless makes it false and executes the Else statement.

You can use an else block with Unless, just like you did with the If expression. The end keyword is also required to close the block. The If and Unless modifiers can also be used to execute the code.

Basic Concepts Of Ruby Language

Case Statement

Basic Concepts Of Ruby Language

Multiple values can be tested within a single When by separating the values with commas.
Basic Concepts Of Ruby Language

In this above code, more than one values, i.e., multiple values also can be used if one condition becomes true. This means it displays the output.

An else statement can be provided to execute the code if no When condition matches.

Basic Concepts Of Ruby Language

Until Loops

Basic Concepts Of Ruby Language

Ranges

Basic Concepts Of Ruby Language

Basic Concepts Of Ruby Language
The first condition of a is (1..7) two dots, it displays the all the numbers between 1 and 7 as output. The second condition of b is (79…82) three dots, it displays the numbers of 79 to 81 except the last number as output. The third condition of c is also like the first condition.

Ranges can also be used in case statements for when values.

Basic Concepts Of Ruby Language

Break Statement

The break statement can be used to stop a loop. For example, we can try this in a For loop.

Basic Concepts Of Ruby Language

In this example, the Ranges concept is also included.

Next Statement

The Next statement can be used to skip one iteration of the loop and continue with the next one.

Basic Concepts Of Ruby Language

This will output only the odd numbers from 0 to 10 because the even number will skip the loop iteration.

Ruby also has the redo statement, which causes the current loop iteration to repeat.

Loop do

Summary

Today, we saw some interesting concepts. I hope all of them are understandable. If you have any queries, please ask anything about it.