A tutorial on the usage of circular statements in Ruby

Time:2022-5-23

 RubyThe loop in is used to execute the same code block a specified number of times. In this chapter, we will introduce the supported in ruby in detailCircular statement
Ruby while statement:
Syntax:

while conditional [do]
   code
end

Execute code when the condition is true. The condition of a while loop is a reserved word, line feed, backslash (\) or a semicolon separated in the code.
example:

?
1
2
3
4
5
6
7
8
9
#!/usr/bin/ruby
 
$i = 0
$num = 5
 
while $i < $num do
  puts("Inside the loop i = #$i" )
  $i +=1
end

This will produce the following results:

?
1
2
3
4
5
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4

Ruby while Rhetoric:
Syntax:

code while condition

OR

begin
  code
end while conditional

Execute the code when the condition is true.

If the while modifier is followed by a begin statement but there is no rescue or ensure clause, the code is evaluated the previous time.
example:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
$i = 0
$num = 5
begin
  puts("Inside the loop i = #$i" )
  $i +=1
end while $i < $num

This will produce the following results:

?
1
2
3
4
5
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4

Ruby until statement:

until conditional [do]
   code
end

Execute the code when the condition is false. Until conditional statements are reserved words, line breaks, or semicolons that are separated from code.
sentence:

?
1
2
3
4
5
6
7
8
9
#!/usr/bin/ruby
 
$i = 0
$num = 5
 
until $i > $num do
  puts("Inside the loop i = #$i" )
  $i +=1;
end

This will produce the following results:

?
1
2
3
4
5
6
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5

Ruby until Rhetoric:
Syntax:

code until conditional

OR

begin
   code
end until conditional

Execute the code when the condition is false.

If the until rhetorical character follows the begin statement but there is no rescue or ensure clause, once the code is executed, it is before the condition is evaluated.
example:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
$i = 0
$num = 5
begin
  puts("Inside the loop i = #$i" )
  $i +=1;
end until $i > $num

This will produce the following results:

?
1
2
3
4
5
6
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5

Ruby for statement:
Syntax:

for variable [, variable …] in expression [do]
   code
end

Execute each element of the code at once in the expression.
example:

?
1
2
3
4
5
#!/usr/bin/ruby
 
for i in 0..5
  puts "Value of local variable is #{i}"
end

Here we define the range 0 5 。 Because in the statement for I in 0 5 will allow the range of values from 0 to 5 (including 5), which will produce the following results:

?
1
2
3
4
5
6
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

for… The in cycle is almost identical to:

?
1
(expression).each do |variable[, variable...]| code end

Except for a for loop, a new range of local variables is not created. One cycleexpressionSeparation from code, reserved word, a newline character, or semicolon.
example:

?
1
2
3
4
5
#!/usr/bin/ruby
 
(0..5).each do |i|
  puts "Value of local variable is #{i}"
end

This will produce the following results:

?
1
2
3
4
5
6
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

Ruby break statement:
Syntax:

break

Terminate most internal loops. The method within the termination block returns nil if the called method is different from the related block.
example:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
for i in 0..5
  if i > 2 then
   break
  end
  puts "Value of local variable is #{i}"
end

This will produce the following results:

?
1
2
3
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Ruby next statement:
Syntax:

next

Jump to the next iteration of the innermost loop. If the call block terminates execution within a block (with yield or nil returned by the call).
example:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
for i in 0..5
  if i < 2 then
   next
  end
  puts "Value of local variable is #{i}"
end

This will produce the following results:

?
1
2
3
4
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5

Ruby redo statement:
Syntax:

redo

The innermost loop iteration is restarted without checking the loop conditions.

Will restart yield or call if called within a block.
example:

?
1
2
3
4
5
6
7
8
#!/usr/bin/ruby
 
for i in 0..5
  if i < 2 then
   puts "Value of local variable is #{i}"
   redo
  end
end

This will produce the following results, and an infinite loop will be executed:

?
1
2
3
Value of local variable is 0
Value of local variable is 0
............................

Ruby retry statement:
Syntax:

retry

If the retry expression appears in the rescue clause, restart from the beginning.

?
1
2
3
4
5
6
begin
  do_something # exception raised
rescue
  # handles error
  retry # restart from beginning
end

If there is a retry iteration, block, or expression in the body, restart the iteration call. The parameter conditions of the iterator are recalculated.

?
1
2
3
for i in 1..5
  retry if some_condition # restart from i == 1
end

example:

?
1
2
3
4
5
6
#!/usr/bin/ruby
 
for i in 1..5
  retry if i > 2
  puts "Value of local variable is #{i}"
end

This will produce the following results and enter an infinite loop:

?
1
2
3
4
5
6
7
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
............................

 

Recommended Today

MySQL creates table, adds, deletes, adds some statements, and modifies table data.

Class___________ Name__________ Achievements_________ Employee number , employee name , position manager‘s number , date of entry , salary , bonus , department number Emp Emp –New library java0408CREATE DATABASE java0408 CHARACTER SET utf8;–New tableCREATE TABLE Emp (Empno int, — employee numberEname varchar (5), — employee nameJob varchar (10), — positionMgr double, — manager’s numberHire date, […]