Last active
August 29, 2015 13:56
-
-
Save mathildathompson/9199456 to your computer and use it in GitHub Desktop.
Conditionals & Loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#FLOW CONTROL | |
- Conditionals; | |
- Loops; | |
#PATH | |
- echo $PATH - shows all the paths the computer searches through (the paths are separated by a colon); | |
- which - will tell you which programme was run (if two files have the same name, it will run the first one found in the path); | |
- The ruby language will allow you to get hold of the process.id | |
- If you type irb into terminal, you can then type Process.pid to get the process id; | |
#CONDITIONALS | |
- 3 < 5 => true | |
- 3 <= 5 => true | |
- 3 > 5 => false | |
- 5 == 5 => true | |
- 5 != 5 => false | |
- !true => false | |
- !false => true | |
- ! (5 > 3) => false | |
#IF ELSE STATMENTS | |
if 5 < 7 | |
puts "5 is less than 7" | |
elsif 5 < 3 | |
puts "5 is less than 3" | |
else | |
puts "I dont know!" | |
end | |
score = 75 | |
if score >= 80 | |
puts "A grade" | |
elsif score >= 70 | |
puts "B grade" | |
else | |
puts "F" | |
end | |
=> "B grade" | |
- We are comparing score 3 times throughout the if else statement, this is not very DRY, use a CASE statment instead; | |
#CASE STATEMENT | |
brother = "Graucho" | |
case brother | |
when "Graucho" | |
puts "cigar" | |
when "Harpo" | |
puts "Piano" | |
when "Chico" | |
puts "piano" | |
end | |
#LOOP | |
- While statement will continue to execute code while statement is true; | |
#WHILE LOOP1 | |
while true | |
'puts this is also true' | |
end | |
- The while loop above is an infinite loop - AVOID THIS! | |
#WHILE LOOP2 | |
i = 0 | |
while i < 25 | |
print "#{i} is less than 25" | |
i = i + 1 | |
end | |
#WHILE LOOP3 | |
puts "Please enter a number" | |
i = gets.chomp.to_i | |
puts "You entered #{i}, thank you!" | |
while i < 25 | |
print "#{i} is less than 25" | |
i = i + 1 | |
end | |
#WHILE LOOP4 | |
puts "What is 2 to the 16th power" | |
answer = gets.chomp.to_i | |
while (2**16) != answer | |
puts "Wrong" | |
answer = gets.chomp.to_i | |
end | |
-Chomp takes off the new line character; | |
puts "What is your first name?" | |
first = gets.chomp | |
puts "You typed #{first}" | |
puts "What is your last name" | |
last = gets.chomp | |
puts "Your last name is #{last}" | |
#IF STATEMENTS REVERSE ORDER; | |
puts "you are old" if age < 50 | |
#FUNCTIONS | |
- Functions are first class; | |
def area(length, width) | |
a = length * width | |
a | |
end | |
def volume(length, width, height) | |
area(length, width) * height | |
end | |
#You can call a function within a function | |
def square(x) | |
x * x | |
end | |
def cube(x) | |
x * x * x | |
end | |
puts volume(square(5), cube(9), 77) | |
def name_tag_generator(first, last, gender, age) | |
if gender == 'f' | |
if age < 19 | |
puts "Miss #{first} #{last}" | |
else | |
puts "Ms #{first} #{last}" | |
end | |
else | |
puts "Mr #{first} #{last}" | |
end | |
end | |
#RESOURCE | |
- [RUBY](http://http://ruby-doc.org/) | |
#COMMAND LINE SHORTCUTS | |
- Reverse search in command line: ctrl + r and then start typing the file name and it will complete it; | |
- Press the up key to get previous commands; | |
- Ctrl + c (to kill a programme in the terminal); | |
- cmd + / (to comment out code); | |
- Ctrl + l to clear the screen; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment