Sunday, April 10, 2016

Ruby_Lesson_Control Flow in Ruby

Introduction to Control Flow
1.How It Works
Control flow gives us the flexibility we're looking for. We can select different outcomes depending on information the user types, the result of a computation, or the value returned by another part of the program.
2.If
Ruby's if statement takes an expression, which is just a fancy word for something that has a value (like 4, true, or pants). If that expression is true, Ruby executes the block of code that follows the if. If it's not true (that is, false), Ruby doesn't execute that block of code: it skips it and goes on to the next thing.
Here's an example of an if statement in action:
if 1 < 2
  print "I'm getting printed because one is less than two!"
end
Ruby doesn't care about whitespace (spaces and blank lines), so the indentation of the print statement isn't necessary. However, it's a convention that Rubyists (Ruby enthusiasts) follow, so it's good to get in the habit now. The block of code following an if should be indented two spaces.
When you're done with your if, you have to tell Ruby by typing end.
3.Else
The partner to the if statement is the else statement. An if/else statement says to Ruby: "If this expression is true, run this code block; otherwise, run the code after the else statement." Here's an example:
if 1 > 2
  print "I won't get printed because one is less than two."
else
  print "That means I'll get printed!"
end
4.Elsif
What if you want more than two options, though? It's elsif to the rescue! The elsif statement can add any number of alternatives to an if/else statement, like so:
if x < y  # Assumes x and y are defined
  puts "x is less than y!"
elsif x > y
  puts "x is greater than y!"
else
  puts "x equals y!"
end

5.Unless
Sometimes you want to use control flow to check if something is false, rather than if it's true. You could reverse your if/else, but Ruby will do you one better: it will let you use an unless statement.
Let's say you don't want to eat unless you're hungry. That is, while you're not hungry, you write programs, but if you are hungry, you eat. You might write that program in Ruby like this:
hungry = false
unless hungry
  puts "I'm writing Ruby programs!"
else
  puts "Time to eat!"
end 
Making Comparisons
6.Equal or Not?
In Ruby, we assign values to variables using =, the assignment operator. But if we've already used = for assignment, how do we check to see if two things are equal? Well, we use ==, which is a comparator (also called a relational operator). == means "is equal to." When you type
x = 2
y = 2
if x == y
  print "x and y are equal!"
end
you're saying: "if x equals y, print 'x and y are equal!'" You can also check to see if two values are not equal using the != comparator.
7.Less Than or Greater Than
We can also check to see if one value is less than, less than or equal to, greater than, or greater than or equal to another. Those operators look like this:
  • Less than: <
  • Less than or equal to: <=
  • Greater than: >
  • Greater than or equal to: >=
Boolean Operators
8.And
Comparators aren't the only operators available to you in Ruby. You can also use logical or boolean operators. Ruby has three: and (&&), or (||), and not (!). Boolean operators result in boolean values: true or false.
The boolean operator and, &&, only results in true when both expression on either side of && are true. Here's how && works:
true && true # => true
true && false # => false
false && true # => false
false && false # => false
For example, 1 < 2 && 2 < 3 is true because it's true that one is less than two and that two is less than three.

9.Or
Ruby also has the or operator (||). Ruby's || is called an inclusive or because it evaluates to true when one or the other or both expressions are true. Check it out:
true || true # => true
true || false # => true
false || true # => true
false || false # => false

10.Not
Finally, Ruby has the boolean operator not (!). ! makes true values false, and vice-versa.
!true # => false
!false # => true
11. Project
11.1.Getting User Input
First, we should print a statement to prompt the user for input, then set that input to a variable using gets.chomp.
print "please input string:"
user_input = gets.chomp

11.2.Downcase!
Call the .downcase! method on user_input. Make sure to include the ! so that the user's string is modified in-place; otherwise, Ruby will create a copy of user_input and modify that instead.
user_input.downcase!
11.3.Setting Up the 'If' Branch, Part 1
For the if half of our branch, we want to check whether the user's input contains an "s".
if string_to_check.include? "substring"
We can do that using Ruby's .include? method, which evaluates to true if it finds what it's looking for and false otherwise.
(As a general rule, Ruby methods that end with ? evaluate to the boolean values true or false.)
if user_input.include? "s"
    print "find s"
end

11.4.Setting Up the 'If' Branch, Part 2
When we find "s", we want Ruby to replace every instance of "s" it finds with "th". We can do this with the .gsub! method, which stands for global substitution.
The syntax looks like this:
string_to_change.gsub!(/s/, "th")
user_input.gsub!(/s/,"th")
11.5.Setting Up the 'Else' Branch
else
    print "not find s"

11.6.Returning the Final String
puts "hello,#{user_input}"









No comments :

Post a Comment