Sie sind auf Seite 1von 27

Chapter 3: Control Statements

By:
Kushal Jangid

Introduction

In every instance, when you use a control structure with a


conditional, you are asking if something is true or false.
When you get the desired answertrue or false
depending on how youve designed your codethe code
block associated with the control is executed.

The IF Statement
Program #1 :

if 1 == 1 then
print "True!"
end
- If the code execute true then the print statement
will execute.

The IF Statement
Program #2:

x = 256
if x == 256
puts "x equals 256"
end
Output => x equals 256.
No Need of then.

The IF Statement
Program #3:
x = 256
if x == 256 then puts "x equals 256" end
Another way of writing the same statement.

The IF Statement
Program #4:
x = 256
puts "x equals 256" if x == 256
Can change the order of things, placing if after puts.

The IF Statement
Program #5:
x = 256
if x == 256: puts "x equals 256" end
Can replace : with then .

&& operator means and.


Program #6:
ruby = "nifty"
programming = "fun"
if ruby == "nifty" && programming == "fun"
puts "Keep programming!"
end
Output => Keep programming!
If Both statements are true then puts will execute.

&& operator means and.


Program #7:

if ruby == "nifty" and programming == "fun"


puts "Stop programming and go outside for a break!"
end

Can use and in place of &&.

&& operator means and.


Program #7:

if ruby == "nifty" and programming == "fun"


puts "Stop programming and go outside for a break!"
end

Can use and in place of &&.

|| operator means or.


Program #8:

if ruby == "nifty" or programming == "fun"


puts "Keep programming!"
end

Can use or in place of ||.

Other Operators
Program #9:
delete_record if record != 0x8ff # not equal to
if amt > 1.00 then desc = "dollars" end # greater than
desc = "cents" if amt < 1.00 # less than
if height >= 6 then print "L or XL" end # greater than or
equal to
print "shrimpy" if weight <= 100 # less than or equal to

Using else and elsif

The elsif keyword provides you with one or more intermediate


options after the initial if , where you can test various
statements.

Using else and elsif


lang = :es
if lang == :en
print "dog"
elsif lang == :es
print "perro"
elsif lang == :fr
print "chien"
elsif lang == :de
print "Hund"
else
puts "No language set; default = 'dog'."
end.

The Ternary Operator


label = length == 1 ? " argument" : " arguments"

This expression assigns a string value to label based on the


value of length. If the value of length is 1 , then the string
value argument (singular) will be assigned to label ; but if it is
not truethat is, if length has a value other than 1 then
the value of label will be the string arguments (plural).

The case Statement


lang = :fr
dog = case lang
when :en: "dog"
when :es: "perro"
when :fr: "chien"
when :de: "Hund"
else "dog"
end

The case Statement


scale = 8
case scale
when 0: puts "lowest"
when 1..3: puts "medium-low"
when 4..5: puts "medium"
when 6..7: puts "medium-high"
when 8..9: puts "high"
when 10: puts "highest"
else
puts "off scale"
end
# => high
1..3 means a range of numbers from 1 to 3.
1...3 means a range of numbers from 1 to 2.

The while Loop


i=0
breeds = [ "quarter", "arabian", "appalosa", "paint" ]
puts breeds.size # => 4
temp = []
while i < breeds.size do
temp << breeds[i].capitalize
i +=1
end
temp.sort! # => ["Appalosa", "Arabian", "Paint", "Quarter"]
breeds.replace( temp )
p breeds # => ["Appalosa", "Arabian", "Paint", "Quarter"]

The while Loop


i=0
breeds = [ "quarter", "arabian", "appalosa", "paint" ]
puts breeds.size # => 4
temp = []
while i < breeds.size Do is optional
temp << breeds[i].capitalize
i +=1
end
temp.sort! # => ["Appalosa", "Arabian", "Paint", "Quarter"]
breeds.replace( temp )
p breeds # => ["Appalosa", "Arabian", "Paint", "Quarter"]

Unless and Until


An unless statement is really like a negated if statement.

if lang == "de"
dog = "Hund"
else
dog = "dog"
end

unless lang == "de"


dog = "dog"
else
dog = "Hund"
end

Unless and Until


An until is really a negated form of while .
weight = 150
while weight < 200 do
puts "Weight: " + weight.to_s
weight += 5
end
weight = 150
until weight == 200 do
puts "Weight: " + weight.to_s
weight += 5
end

The loop Method


loop do
print "Type something: "
line = gets
break if line =~ /q|Q/
puts line
end
However, if line matches q or Q , you will break out of loop then and
there; otherwise, puts prints the contents of line to standard
output. When you hit end , control returns to the top of the loop
again.

The for loop

for i in 1..5 do [ Do is optional ]


print i, " "
end
# => 1 2 3 4 5

The times Method

for i in 1..10 do [ Do is optional ]


print i, " "
end
# => 1 2 3 4 5 6 7 8 9 10
OR

10.times { |i | print i, " " } # => 0 1 2 3 4 5 6 7 8 9

The upto Method

for i in 1..10 do [ Do is optional ]


print i, " "
end
# => 1 2 3 4 5 6 7 8 9 10
OR

1.upto(10) { |i| print i, " " } # => 1 2 3 4 5 6 7 8 9 10

The downto Method

for i in 5..1 do [ Do is optional ]


print i, " "
end
# => 5 4 3 2 1
OR

5.downto(1) { |i| print i, " " } # => 5 4 3 2 1

Thank You

Das könnte Ihnen auch gefallen