Interpreting conditional judgment statements in Ruby

Source: Internet
Author: User
Tags case statement reserved

This article mainly introduces the detailed interpretation of Ruby in terms of conditional judgment statements, if, else and other logical judgments are the basis of the programming language, the need for friends can refer to the

Ruby offers a conditional structure, which is common in modern programming languages. Here, we'll explain all of Ruby's conditional statements and modifiers

Ruby If...else Statement:

Grammar:

?

1 2 3 4 5 6 7 If conditional [then] code ... [elsif conditional [then] code ...] ... [Else code ...] End

An if expression is used for conditional execution. The value false and nil are false, and the rest are true. Note that Ruby strings use elsif, not else if and not elif.

The IF condition is ture executes the code. If the condition is not ture, the code specified in the ELSE clause is executed.

If the condition of an if expression is a reserved word, then a newline character or semicolon separates the code.

Instance:

?

1 2 3 4 5 6 7 8 9 10 11-12 #!/usr/bin/ruby x=1 if x > 2 puts "x is greater than 2" Elsif x <= 2 and x!=0 puts "x is 1" Else puts "I can ' t gue SS the number "end X is 1"

Ruby If rhetorical character:

Grammar:

Code if condition

If condition is true execution code.

Instance:

?

1 2 3 4 #!/usr/bin/ruby $debug =1 print "debugn" if $debug

This will produce the following results:

?

1 Debug

Ruby unless statement:

Grammar:

?

1 2 3 4 5 Unless conditional [then] code [else code] End

Executes the code if the condition is false. If the condition is that the code specified in the FALSE,ELSE clause is executed.

For example:

?

1 2 3 4 5 6 7 8 #!/usr/bin/ruby x=1 unless X>2 puts ' x is less than 2 ' else puts ' x is greater than 2 ' end

This will produce the following results:

?

1 X is less than 2

Ruby unless rhetoric characters:

Grammar:

Code unless conditional

Executes the code, False if there is a condition.

Instance:

?

1 2 3 4 5 6 7 8 #!/usr/bin/ruby $var = 1 print "1--the value is Setn" if $var print "2--the value is Setn" unless $var $var = False Print "3--Value is Setn" unless $var

This will produce the following results:

?

1 2 1--The value is set 3--the value is set

Ruby Case Statement

Grammar:

?

1 2 3 4 5 6 case expression [expression [, expression ...] [then] code] ... [Else code] End

The code executed when the = = = operator is used to match the specified terms, as specified by the comparison expression.

clause to calculate the expression specified by the When and the left operand. If no clause matches, the code else clause executes.

When the expression of the statement is reserved, then a newline character or semicolon separates the code.

So:

?

1 2 3 4 5 6 7 8 Case Expr0 when expr1, Expr2 stmt1 when EXPR3, EXPR4 stmt2 else Stmt3 end

is basically similar to the following:

?

1 2 3 4 5 6 7 8 _tmp = Expr0 if expr1 = = _tmp | | EXPR2 = = _tmp stmt1 elsif expr3 = = _tmp | | EXPR4 = = _tmp Stmt2 Else Stmt3 end

Instance:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 #!/usr/bin/ruby $age = 5 case $age when 0. 2 puts "Baby" when 3. 6 puts "little child" when 7. Puts ' child ' when 13. Puts "youth" else puts "adult" end

This will produce the following results:

?

1 Little Child
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.