Control Flow Flashcards
What is control flow?
Control flow is that we can select different outcomes depending on the environment and the input.
What is an if statement?
Ruby takes an if statement and decides if its true and runs code accordingly.
What is an else statement?
The else statement is the continuation of the if statement. This code runs after the if statement is false.
What is an elsif statement?
An elsif statement gives the if else statement more options.
What is an unless statement?
An if statement checks if something is true, An unless statement checks if something is false and runs code accordingly.
What is a comparator?
A comparator checks if two values are equal or not
we use == to check if they are equal and != if we want to check if two values are not equal.
What are the comparator for less or equal than?
less than <
less than or equal to <=
greater than >
Greater than or equal to >=
What are boolean operators?
Boolean operators are either true or false
- && (and)
- | | (or)
- ! (not)
What is an and operator?
- && (and), The expression is true if both statements before and after && are true
true && true => true
true && false => false
false && true => false
false && false => false
What is an Or-operator?
- | | (or) is an operator when at least one of the expressions are true true || true # => true true || false # => true false || true # => true false || false # => false
What is an Not-operator?
A not operator (!) makes any statement after the expression false
!true= false
What is gets.chomp?
Gets chomp is for getting user input from the terminal
What is the .downcase method?
the .downcase method takes the user input and converts it do lower case letters.
What is the .include? method
The include? method checks if the user input contains what we want
What is the .gsub! method?
The .gsub method stands for global substitution and changes all the chosen inputs.
string_to_change.gsub!(/s/, “th”)
What is string interpolation ?
String interpolation adds a value to a string.
print “Adios, #{my_string}!”
What is a While loop?
The while loop runs code as long as the conditions are true.
counter = 1 while counter < 11 puts counter counter = counter + 1 end
What is an infinite loop?
An infinite loop is a loop that runs forever.
They need to be avoided at all cost.
What is an untill loop?
An until loop runs as long as the conditions are not met.
i = 0 until i == 6 i = i + 1 end puts i
What is an assignment operator?
An assignment operator is used to update a variable.
+=, -=, *=, and /=.
What is an for loop?
The for loop runs a code between a range.
for current_iteration_number in 1..100 do
puts “Hello world, this is number #{current_iteration_number}”
end
What is an inclusive range?
An inclusive range is a range that includes the last number in the range
1..3 (two dots)
for num in 1..20 (don’t forget the in)
puts num
end
What is an exclusive range?
An exclusive range is a range where the last number is not included in the range.
1..3 (three dots)
for num in 1…20 (don’t forget the in)
puts num
end
What is an iterator?
An iterator is just a Ruby method that repeatedly invokes a block of code.
What is the next keyword?
The next keyword is used to skip a step.
What is an array?
An array is a variable with multiple items in it
my_array = [1, 2, 3, 4]
An array is also indexed starting from 0.
How do you get user input?
gets.chomp
What is The .each Iterator ?
.each method, which can apply an expression to each element of an object, one at a time.
object.each do | item | # Do something end
What is the .times Iterator?
The .times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times
10.times { print “Chunky bacon!” }
What is the .split Method?
it takes in a string and returns an array. If we pass it a bit of text in parentheses, .split will divide the string wherever it sees that bit of text, called a delimiter. For example,
text.split(“,”)