Introduction to loop statements in Ruby concise tutorial

Source: Internet
Author: User

Loop structures are indispensable in programming languages, so loops in Ruby also have their own custom rules.

While we focus on the loop structure, we need to know two factors: 1) the condition of the loop; 2) the content of the loop execution

Ruby has some methods to implement cyclic struct:

1. times Method

As mentioned above, the syntax is as follows:
Copy codeThe Code is as follows:
Number of cycles. times do number of cycles. times {
Repeated actions
End}

# You can also add variables to the times module.

5. times {| I |
Print "This is the", I + 1, "time. \ n"
}

# I variable is calculated from 0

2. for statement
Copy codeThe Code is as follows:
Syntax:
For variable in start value... end value do
Repeated actions
End

# Do can be omitted.

From = 0
To = 20
Sum = 0
For I in from...
Sum + = 1
End

Syntax:
For variable in object
Repeated actions
End

Names = ["Windy", "Cindy", "Jack", "Hugo"]
For name in names
Print name, "likes Ruby. \ n"
End

The for statement in the second syntax is very similar to the for each statement in java. for (I in list ?) {...}

3. while statement

The while statement is similar to that in JAVA.
Copy codeThe Code is as follows:
Syntax:
While condition do
Repeated actions
End

A = 1
Sum = 0
While a <10 do
Sum + =
I + =
End

4. until statement
Its syntax is similar to the while statement, but the loop is executed repeatedly only when the conditions do not match.

Copy codeThe Code is as follows:
Syntax:
Until condition do
Repeated actions
End
# Do can be omitted

Sum = 0
Until sum> 50
Sum + = 1
End
Print sum

# The above until loop can be converted to the following while LOOP
While! (Sum> 50)
Sum + = 1
End

5. each Method
This method has been mentioned earlier. Here we will briefly record the syntax.
Copy codeThe Code is as follows:
Object. each {| variable |

Actions to be executed repeatedly

}

6. loop Method

It is a method without an ending condition, but is continuously processed cyclically, for example:
Copy codeThe Code is as follows:
Loop {
Print "Ruby"
}

Loop Control:

There are mainly the following keywords: break, next, redo; while in java, there are break, continue, return

Command Purpose
Break Stop the action and immediately jump out of the loop
Next Jump directly to the next loop
Redo Re-execute this loop with the same conditions


Conclusion: when the number of times is fixed, the times method is better, while the while and each methods can be used for most other loops.

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.