Interpreting conditional judgment statements in Ruby

Source: Internet
Author: User
Tags case statement reserved

Ruby provides conditional structures, commonly found in modern programming languages. Here we will explain all conditional statements and modifiers in Ruby
Ruby if ... else statement:
grammar:


if conditional [then]
  code ...
[elsif conditional [then]
  code ...] ...
[else
  code ...]
end
if expressions are used for conditional execution. Values are false and nil are both false, others are true. Note that Ruby strings use elsif, not else if or elif.

If the condition is true, the code is executed. If the condition is not true, the code specified in the else clause will be executed.

The condition of an if expression is a reserved word, so a newline or semicolon separates the code.
Example:


#! / 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 guess the number"
end
 
x is 1
Ruby if rhetoric:
grammar:

code if condition

The if condition is true to execute the code.
Example:


#! / usr / bin / ruby
 
$ debug = 1
print "debug \ n" if $ debug
This will produce the following results:


debug
Ruby unless statement:
grammar:


unless conditional [then]
  code
[else
  code]
end
If the condition is false, execute the code. If the condition is false, the code specified in the else clause is executed.
E.g:


#! / 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:


x is less than 2
Ruby unless rhetoric:
grammar:

code unless conditional

Execute the code, false if there is a condition.
Example:


#! / usr / bin / ruby
 
$ var = 1
print "1-Value is set \ n" if $ var
print "2-Value is set \ n" unless $ var
 
$ var = false
print "3-Value is set \ n" unless $ var
This will produce the following results:


1-Value is set
3-Value is set
Ruby case statement
grammar:


case expression
[when expression [, expression ...] [then]
  code] ...
[else
  code]
end
When the comparison expression is specified, when the === operator is used, the code is executed when the specified terms match.

The clause evaluates the expression specified by when and left operand. If no clause matches, the code executes in the case of an else clause.

The when statement is reserved for words, then, a newline or semicolon separates the code.

Then:


case expr0
when expr1, expr2
  stmt1
when expr3, expr4
  stmt2
else
  stmt3
end
Basically similar to the following:


_tmp = expr0
if expr1 === _tmp || expr2 === _tmp
  stmt1
elsif expr3 === _tmp || expr4 === _tmp
  stmt2
else
  stmt3
end
Example:


#! / usr / bin / ruby
 
$ age = 5
case $ age
when 0 .. 2
  puts "baby"
when 3 .. 6
  puts "little child"
when 7 .. 12
  puts "child"
when 13:. 18
  puts "youth"
else
  puts "adult"
end
This will produce the following results:

1
little child

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.