Skip to content

Commit b9124dd

Browse files
committed
Flow Control
1 parent 83bae1a commit b9124dd

10 files changed

+202
-1
lines changed
File renamed without changes.
File renamed without changes.

Flow Control/branching.rb

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# using if
2+
3+
puts 'Hello, what\'s your name?'
4+
name = gets.chomp
5+
puts 'Hello, ' + name.capitalize + '.'
6+
7+
if name.capitalize == 'Claudiu'
8+
puts 'What a lovely name!'
9+
end
10+
11+
12+
# using if / else
13+
14+
puts 'I\'m a fortune teller. Tell me your name.'
15+
name = gets.chomp.capitalize
16+
17+
if name == 'Claudiu'
18+
puts 'I see great things in your future.'
19+
else
20+
puts 'Your future is ... Oh, my! Look at the time!'
21+
puts 'I really have to go, sorry!'
22+
end
23+
24+
25+
# branches of branches
26+
27+
puts 'Hello, and welcome to seventh grade English.'
28+
puts 'My name is Mr. Gabbard. And your name is...?'
29+
name = gets.chomp
30+
31+
if name == name.capitalize
32+
puts 'Please take a seat, ' + name + '.'
33+
else
34+
puts name + '? You mean ' + name.capitalize + ', right?'
35+
puts 'Don\'t you even know how to spell your name?'
36+
reply = gets.chomp
37+
38+
if reply.downcase == 'yes' || reply == name.capitalize
39+
puts 'Hmmph! Well, sit down!'
40+
else
41+
puts 'GET OUT!!!'
42+
end
43+
end
44+

Flow Control/comparison.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# less and greater < & >
2+
3+
puts 1 > 2
4+
puts 1 < 2
5+
6+
# less than or equal <=; greater than or equal >=
7+
8+
puts 5 >= 5
9+
puts 5 <= 4
10+
11+
# equal == & not equal !=
12+
13+
puts 1 == 1
14+
puts 2 != 1
15+
16+
# comparing strings = comparing their lexicographical ordering
17+
18+
puts 'cat' < 'dog'
19+
puts ''
20+
21+
puts 'bug lady' < 'Xander'
22+
puts 'bug lady'.downcase < 'Xander'.downcase
23+
puts ''
24+
25+
puts 2 < 10
26+
puts '2' < '10'

Flow Control/ex_99_bottles_of_beer.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Write a program that will print out the lyrics of "99 Bottles of Beer on the Wall".
2+
3+
initial_number = 99
4+
current_number = initial_number
5+
6+
7+
while current_number > 0
8+
9+
puts current_number.to_s + ' bottles of beer on the wall, ' + current_number.to_s + ' bottles of beer.'
10+
current_number = current_number - 1
11+
12+
if current_number > 1
13+
puts 'Take one down and pass it around, ' + current_number.to_s + ' bottles of beer on the wall.'
14+
else
15+
puts 'Take one down and pass it around, ' + current_number.to_s + ' bottle of beer on the wall.'
16+
end
17+
18+
puts '---------------------------------------'
19+
20+
if current_number == 1
21+
puts current_number.to_s + ' bottle of beer on the wall, ' + current_number.to_s + ' bottle of beer.'
22+
current_number = current_number - 1
23+
puts 'Take it down and pass it around, no more bottles of beer on the wall.'
24+
puts '---------------------------------------'
25+
puts 'No more bottles of beer on the wall, no more bottles of beer.'
26+
puts 'Go to the store and buy some more, ' + initial_number.to_s + ' bottles of beer on the wall.'
27+
end
28+
29+
end

Flow Control/ex_grandma.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
=begin
2+
3+
Deaf grandma. Whatever you say to Grandma (whatever you type in), she should respond with this:
4+
HUH? SPEAK UP, SONNY!
5+
unless you shout it (type in all capitals). If you shout, she can hear you (or at least she think so) and yells back:
6+
NO, NOT SINCE 1938!
7+
To make program believable, have Grandma shout different year each time, maybe any year at random between 1930 and 1950.
8+
You can't stop talking with Grandma until you shout BYE.
9+
10+
=end
11+
12+
puts 'WHAT\'S UP, SONNY?'
13+
while true
14+
response = gets.chomp
15+
if response == 'BYE'
16+
puts 'GOOD BYE, MY SON!'
17+
break
18+
elsif response == response.upcase
19+
random_year = rand(20) + 1930
20+
puts "NO, NOT SINCE #{random_year}!"
21+
else
22+
puts 'HUH? SPEAK UP, SONNY!'
23+
end
24+
end
25+
26+
=begin
27+
28+
Deaf grandma extended. What if Grandma doesn't want you to leave? When you shout 'BYE' she could pretend not to hear you.
29+
Change your previous program so that you have to shout 'BYE' three times in a row. If you shout BYE three times, but not
30+
in a row, you should still be talking with Grandma.
31+
32+
=end
33+
34+
puts 'WHAT\'S UP, SONNY?'
35+
count = 0
36+
while true
37+
response = gets.chomp
38+
if response == 'BYE'
39+
count = count + 1
40+
puts 'HUH?'
41+
else
42+
count = 0
43+
if response == response.upcase
44+
random_year = rand(20) + 1930
45+
puts "NO, NOT SINCE #{random_year}!"
46+
else
47+
puts 'HUH? SPEAK UP, SONNY!'
48+
end
49+
end
50+
if count >=3
51+
puts 'GOOD BYE, MY SON!'
52+
break
53+
end
54+
end
55+
56+

Flow Control/logic.rb

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# logical operators ||, &&, and !
2+
3+
i_am_chris = true
4+
i_am_purple = false
5+
i_like_beer = true
6+
i_eat_rocks = false
7+
8+
puts i_am_chris && i_like_beer
9+
puts i_like_beer && i_eat_rocks
10+
puts i_am_purple && i_eat_rocks
11+
puts
12+
puts i_am_chris || i_like_beer
13+
puts i_like_beer || i_eat_rocks
14+
puts i_am_purple || i_like_beer
15+
puts i_am_purple || i_eat_rocks
16+
puts
17+
puts !i_am_purple
18+
puts !i_am_chris

Flow Control/looping.rb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# while in loop
2+
3+
input = ''
4+
while input != 'bye'
5+
puts input
6+
input = gets.chomp
7+
end
8+
puts 'Come again soon!'
9+
10+
11+
# looping forever until break
12+
13+
while 'Spike' > 'Angel'
14+
input = gets.chomp
15+
puts input
16+
if input == 'bye'
17+
break
18+
end
19+
end
20+
puts 'Come again soon!'
21+
22+
while true
23+
input = gets.chomp
24+
puts input
25+
if input == 'bye'
26+
break
27+
end
28+
end
29+
puts 'Come again soon!'
File renamed without changes.

Mixing It Up/name.rb Mixing It Up/ex_name.rb

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Full name greeting. Write a program that asks for a person's first name, then middle, and then last.
32
#Finally, it should greet the person using their full name.
43
puts 'Hello there, and what\'s your name?'

0 commit comments

Comments
 (0)