SlideShare a Scribd company logo
1 of 3
Download to read offline
RUBY IF...ELSE, CASE, UNLESS
http://www.tutorialspoint.com/ruby/ruby_if_else.htm                                                Copyright © tutorialspoint.com



Ruby offers contional structures that are pretty common to modern languages. Here we will explain all the conditional
statements and modifiers available in Ruby

Ruby if...else Statement:

Syntax:

 if conditional [then]
    code...
 [elsif conditional [then]
    code...]...
 [else
    code...]
 end


if expressions are used for conditional execution. The values false and nil are false, and everything else are true. Notice
Ruby uses elsif, not else if nor elif.

Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed.

An if expression's conditional is separated from code by the reserved word then, a newline, or a semicolon.

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 modifier:

Syntax:

 code if condition


Executes code if the conditional is true.

Example:

 #!/usr/bin/ruby

 $debug=1
 print "debugn" if $debug


This will produce following result:
debug


Ruby unless Statement:

Syntax:

 unless conditional [then]
    code
 [else
    code ]
 end


Executes code if conditional is false. If the conditional is true, code specified in the else clause is executed.

Example:

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

 x is less than 2


Ruby unless modifier:

Syntax:

 code unless conditional


Executes code if conditional is false.

Example:

 #!/usr/bin/ruby

 $var = 1
 print "1 -- Value is setn" if $var
 print "2 -- Value is setn" unless $var

 $var = false
 print "3 -- Value is setn" unless $var


This will produce following result:

 1 -- Value is set
 3 -- Value is set


Ruby case Statement

Syntax:

 case expression
 [when expression [, expression ...] [then]
    code ]...
code ]...
 [else
    code ]
 end


Compares the expression specified by case and that specified by when using the === operator and executes the code of
the when clause that matches.

The expression specified by the when clause is evaluated as the left operand. If no when clauses match, case executes the
code of the else clause.

A when statement's expression is separated from code by the reserved word then, a newline, or a semicolon.

Thus:

 case expr0
 when expr1, expr2
    stmt1
 when expr3, expr4
    stmt2
 else
    stmt3
 end


is 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 following result:

 little child

More Related Content

What's hot

Ruby introduction part1
Ruby introduction part1Ruby introduction part1
Ruby introduction part1Brady Cheng
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Mohd Harris Ahmad Jaal
 
Javascript Tlabs
Javascript TlabsJavascript Tlabs
Javascript Tlabsmsneha
 
Javascript conditional statements 1
Javascript conditional statements 1Javascript conditional statements 1
Javascript conditional statements 1Jesus Obenita Jr.
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)heoff
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)Core Lee
 
Looping statement
Looping statementLooping statement
Looping statementilakkiya
 
Elements of c program....by thanveer danish
Elements of c program....by thanveer danishElements of c program....by thanveer danish
Elements of c program....by thanveer danishMuhammed Thanveer M
 
CMP1001 Loops 1
CMP1001 Loops 1CMP1001 Loops 1
CMP1001 Loops 1steves2001
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++amber chaudary
 
Les 2 Javascript
Les 2 JavascriptLes 2 Javascript
Les 2 Javascriptmvanginkel
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Sanjit Shaw
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a RescueLearn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a RescueJames Thompson
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statementspragya ratan
 

What's hot (20)

Ruby introduction part1
Ruby introduction part1Ruby introduction part1
Ruby introduction part1
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
Javascript Tlabs
Javascript TlabsJavascript Tlabs
Javascript Tlabs
 
Javascript conditional statements 1
Javascript conditional statements 1Javascript conditional statements 1
Javascript conditional statements 1
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Lecture 2 php basics (1)
Lecture 2  php basics (1)Lecture 2  php basics (1)
Lecture 2 php basics (1)
 
Looping statement
Looping statementLooping statement
Looping statement
 
Elements of c program....by thanveer danish
Elements of c program....by thanveer danishElements of c program....by thanveer danish
Elements of c program....by thanveer danish
 
C++ STATEMENTS
C++ STATEMENTS C++ STATEMENTS
C++ STATEMENTS
 
CMP1001 Loops 1
CMP1001 Loops 1CMP1001 Loops 1
CMP1001 Loops 1
 
Ruby
RubyRuby
Ruby
 
Conditional statement c++
Conditional statement c++Conditional statement c++
Conditional statement c++
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Les 2 Javascript
Les 2 JavascriptLes 2 Javascript
Les 2 Javascript
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Learn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a RescueLearn Ruby 2011 - Session 5 - Looking for a Rescue
Learn Ruby 2011 - Session 5 - Looking for a Rescue
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Programming in c plus plus3
Programming in c plus plus3Programming in c plus plus3
Programming in c plus plus3
 
Clojure for Rubyists
Clojure for RubyistsClojure for Rubyists
Clojure for Rubyists
 
Vb decision making statements
Vb decision making statementsVb decision making statements
Vb decision making statements
 

Similar to 09 ruby if else

Ruby Basics
Ruby BasicsRuby Basics
Ruby BasicsSHC
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technologyppparthpatel123
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un UnixVpmv
 
Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERLRavi kumar
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docxajoy21
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIALzatax
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 

Similar to 09 ruby if else (20)

Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Osp2.pdf
Osp2.pdfOsp2.pdf
Osp2.pdf
 
Ruby -the wheel Technology
Ruby -the wheel TechnologyRuby -the wheel Technology
Ruby -the wheel Technology
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un UnixCase, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
 
OS.pdf
OS.pdfOS.pdf
OS.pdf
 
Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERL
 
Unix shell scripts
Unix shell scriptsUnix shell scripts
Unix shell scripts
 
00 ruby tutorial
00 ruby tutorial00 ruby tutorial
00 ruby tutorial
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
Vb script tutorial
Vb script tutorialVb script tutorial
Vb script tutorial
 
Javascript
JavascriptJavascript
Javascript
 
Perl_Part2
Perl_Part2Perl_Part2
Perl_Part2
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
Ruby Gotchas
Ruby GotchasRuby Gotchas
Ruby Gotchas
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
test ppt
test ppttest ppt
test ppt
 
ppt30
ppt30ppt30
ppt30
 

More from Walker Maidana (19)

20 ruby input output
20 ruby input output20 ruby input output
20 ruby input output
 
19 ruby iterators
19 ruby iterators19 ruby iterators
19 ruby iterators
 
18 ruby ranges
18 ruby ranges18 ruby ranges
18 ruby ranges
 
17 ruby date time
17 ruby date time17 ruby date time
17 ruby date time
 
16 ruby hashes
16 ruby hashes16 ruby hashes
16 ruby hashes
 
15 ruby arrays
15 ruby arrays15 ruby arrays
15 ruby arrays
 
14 ruby strings
14 ruby strings14 ruby strings
14 ruby strings
 
13 ruby modules
13 ruby modules13 ruby modules
13 ruby modules
 
12 ruby blocks
12 ruby blocks12 ruby blocks
12 ruby blocks
 
11 ruby methods
11 ruby methods11 ruby methods
11 ruby methods
 
08 ruby comments
08 ruby comments08 ruby comments
08 ruby comments
 
07 ruby operators
07 ruby operators07 ruby operators
07 ruby operators
 
06 ruby variables
06 ruby variables06 ruby variables
06 ruby variables
 
05 ruby classes
05 ruby classes05 ruby classes
05 ruby classes
 
03 ruby environment
03 ruby environment03 ruby environment
03 ruby environment
 
01 index
01 index01 index
01 index
 
02 ruby overview
02 ruby overview02 ruby overview
02 ruby overview
 
21 ruby exceptions
21 ruby exceptions21 ruby exceptions
21 ruby exceptions
 
Tutorial ruby eustaquio taq rangel
Tutorial ruby   eustaquio taq rangelTutorial ruby   eustaquio taq rangel
Tutorial ruby eustaquio taq rangel
 

09 ruby if else

  • 1. RUBY IF...ELSE, CASE, UNLESS http://www.tutorialspoint.com/ruby/ruby_if_else.htm Copyright © tutorialspoint.com Ruby offers contional structures that are pretty common to modern languages. Here we will explain all the conditional statements and modifiers available in Ruby Ruby if...else Statement: Syntax: if conditional [then] code... [elsif conditional [then] code...]... [else code...] end if expressions are used for conditional execution. The values false and nil are false, and everything else are true. Notice Ruby uses elsif, not else if nor elif. Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed. An if expression's conditional is separated from code by the reserved word then, a newline, or a semicolon. 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 modifier: Syntax: code if condition Executes code if the conditional is true. Example: #!/usr/bin/ruby $debug=1 print "debugn" if $debug This will produce following result:
  • 2. debug Ruby unless Statement: Syntax: unless conditional [then] code [else code ] end Executes code if conditional is false. If the conditional is true, code specified in the else clause is executed. Example: #!/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 following result: x is less than 2 Ruby unless modifier: Syntax: code unless conditional Executes code if conditional is false. Example: #!/usr/bin/ruby $var = 1 print "1 -- Value is setn" if $var print "2 -- Value is setn" unless $var $var = false print "3 -- Value is setn" unless $var This will produce following result: 1 -- Value is set 3 -- Value is set Ruby case Statement Syntax: case expression [when expression [, expression ...] [then] code ]...
  • 3. code ]... [else code ] end Compares the expression specified by case and that specified by when using the === operator and executes the code of the when clause that matches. The expression specified by the when clause is evaluated as the left operand. If no when clauses match, case executes the code of the else clause. A when statement's expression is separated from code by the reserved word then, a newline, or a semicolon. Thus: case expr0 when expr1, expr2 stmt1 when expr3, expr4 stmt2 else stmt3 end is 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 following result: little child