Ruby Control Flow

Source: Internet
Author: User

I. Judgment Structure

  If Expression

Elsif expression

  Else

End

IfExpression then end#Use then when writing a line

 IfExpression#When the expression returns true, run the preceding code.

... Unless expression#When the expression is false, run the previous Code

Case expression
When condition 1 [, condition 2] [then]

When Condition 3 [, Condition 4] [then]

 Else

End

Two-Cycle StructureWhileExpression
 
[Break[Expression]
End

Run the command when the expression value is true.CodeBlock. You can use break to exit or specify a value for break as the return value of the while loop.

  While Expression

When the value of the following expression is true, the previous statement is executed cyclically. If the previous statement is a begin .. if the end block is used, execute begin first .. end block, and then determine whether to enter the next cycle, such:I=0
Begin
I+ = 1
Puts I
EndWhileI< 10

Until expression
 
End

Exit the loop until the expression value is true.

Until expression

Execute the statement until the expression value changes to true.

For Variable In Array
 
End

Retrieve an element from the array and traverse it. In Ruby, only .. in does not have a separate for, of course .. the usage frequency of in is not wide, and the most common in ruby is the each iterator that every array contains.

Break: exit the innermost loop, including while, until, for, and iteration. Break should not appear in case.
Next: Ignore the following code and enter the next loop.
Redo: do not check the cycle condition to start the current cycle again
Retry: restart the iterator

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.