A tutorial on the usage of circular statements in Ruby

Source: Internet
Author: User
Tags reserved terminates

This article mainly introduces the use of circular statements in Ruby Tutorial, logic Loop statement is the basis of each programming language, need friends can refer to the

Loops in Ruby are used to perform the same number of times as the code block specifies. This chapter details the looping statements that are supported by Ruby.

Ruby While statement:

Grammar:

While conditional [do]

Code

End

Executes code when the condition is true. The while loop condition is a reserved word in the code, a newline, a backslash (), or a semicolon separated.

Instance:

?

1 2 3 4 5 6 7 8 9 #!/usr/bin/ruby $i = 0 $num = 5 while $i < $num did 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 rhetorical character:

Grammar:

Code while condition

OR

Begin

Code

End While conditional

Executes the code when the condition is true.

If the while modifier is immediately following a BEGIN statement but there is no rescue or ensure clause, the code is evaluated before the previous condition.

Instance:

?

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

Executes the code when the condition is false. Until a reserved word, a newline character, or a semicolon, separated from code by a conditional statement.

Statement:

?

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 L Oop i = 5

Ruby until rhetoric characters:

Grammar:

Code until conditional

OR

Begin

Code

End until conditional

Executes the code when the condition is false.

If the until rhetoric follows the BEGIN statement but there is no rescue or ensure clause, the code is executed 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 L Oop i = 5

Ruby for statement:

Grammar:

For variable [, variable ...] in expression [do]

Code

End

Executes each element of the code at once in an in expression.

Instance:

?

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

Here we define the range 0. 5. This produces the following results, because in the statement for I in 0..5 will allow the range of values to be taken from 0 to 5 (including 5):

?

1 2 3 4 5 6 Value of local variable is 0 value of the local variable is 1 value of the local variable is 2 value of the local variable is 3 value Of the local variable is 4 Value of the local variable is 5

The for...in cycle is almost entirely equivalent to:

?

1 (expression). Each do |variable[, variable...]| Code end

Except that a for loop does not create a range of new local variables. A circular expression is separated from code, reserved words, a newline character, or a semicolon.

Example:

?

1 2 3 4 5 #!/usr/bin/ruby (0..5). Each do |i| Puts "Value of local variable are #{i}" end

This will produce the following results:

?

1 2 3 4 5 6 Value of local variable is 0 value of the local variable is 1 value of the local variable is 2 value of the local variable is 3 value Of the local variable is 4 Value of the local variable is 5

Ruby Break statement:

Grammar:

Break

Terminates most internal loops. The method within the terminating block returns nil if the method called is associated with the block.

Instance:

?

1 2 3 4 5 6 7 8 #!/usr/bin/ruby for I-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 the local variable is 1 Value of the local variable is 2

Ruby Next Statement:

Grammar:

Next

Jumps to the next iteration of the inner loop. If the call block terminates execution within a block (with yield or calls to return nil).

Example:

?

1 2 3 4 5 6 7 8 #!/usr/bin/ruby for I-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 the local variable is 3 Value of the local variable is 4 value of the local variable is 5

Ruby Redo Statement:

Grammar:

Redo

Restarts this most internal loop iteration without checking the loop condition.

Will restart yield or call if a block is invoked.

Example:

?

1 2 3 4 5 6 7 8 #!/usr/bin/ruby for I-0..5 if I < 2 then puts "Value of local variable are #{i}" redo End

This produces the following result, which will perform an infinite loop:

?

1 2 3 The value of local variable is 0 Value of the local variable is 0 ...............

Ruby Retry statement:

Grammar:

Retry

If retry expresses the rescue clause now, start over again.

?

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, a block, or an expression in the body, restart the iteration call. The parameter condition of the iterator will be recalculated.

?

1 2 3 For I in 1..5 retry if Some_condition # restart from i = = 1 end

Instance:

?

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

This will produce the following results, which will enter an infinite loop:

?

1 2 3 4 5 6 7 Value of local variable is 1 value of the local variable is 2 value of the local variable is 1 value of the local variable is 2 value Of the local variable is 1 Value of the local variable is 2 ...............
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.