-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbranching_chris_pine6.rb
70 lines (55 loc) · 1.73 KB
/
branching_chris_pine6.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#Comparison methods
puts 1 > 2 #false
puts 1 < 2 #true
puts 5 >= 5 #true
puts 5 <= 4 #false
#greater/less than or equal to
puts 1 == 1 #true
puts 2 != 1 #true
# == are these equal?
# != are these different?
#comparing srings, compares their lexicographical ordering
puts 'cat' < 'dog' #true
#computers order capital letters as coming before lowercase letters
#e.g. 'Zoo' will come before 'ant'
#makesure to use .downcase or .upcase or .capitalize before trying to compare them
#give us special objects true & fale (not the entire string)
#Branching
puts "Hello, what\'s your name?"
name = gets.chomp
puts "Hello, " + name + "."
if name == "Charlotte"
puts "What a lovely name!"
end
#branching comes after the if statement
#run the code between the if & the end, if what comes after it is false the code is not run.
#else - tell program what to do if false
puts "I am a fortune-teller. Tell me your name:"
name = gets.chomp
if name == "Charlotte"
puts "I see great things in your future."
else
puts "Your future is... Oh my! Look at the time!"
puts "I really have to go sorry!"
end
#can have branches within branches
puts "Hello, and welcome to 7th grade English."
puts "My name is Mrs. Gabbard. And your name is...?"
name = gets.chomp
if name == name.capitalize
puts "Please take a seat, #{name}."
else
puts "#{name}? You mean" + name.capitalize + "right?"
puts "Don\'t you even know how to spell your name??"
reply = gets.chomp
if reply.downcase == "yes"
puts "Hmmph! Well, sit down!"
else
puts "GET OUT!!"
end
#write an end at same time as write the if
#if name == name.capitalize
#else
#end
#then can add comments for what the working code needs to do within the if/else statements
#then replace # with the working code itself