Control, Comparators & Conditionals

Program Control
The most useful programs have the ability to make decisions. Program control gives us this ability. Decisions are made by comparing things and then performing a task, or series of tasks, as a result of the comparison.

Comparators
The simplest aspect of program control is comparators. The result of a comparator operation is a boolean value: True or False. Comparators can be combined into expressions. There are six comparators:

 

 ==  Equal to
 !=  Not equal to
 <  Less than
 <=  Less than or equal to
 >  Greater than
 >=  Greater than or equal to


Note: There is a difference between the = and == operators. The = operator is used to set a value, whereas the == operator is used to compare if two things are equal.

Examples:

bool_one = 17 < 328

bool_two = (100 == (2 * 50))

bool_three = 19 < 19

bool_four = -16 >= -18

bool_five = 99 != (98 + 1)

Boolean operators compare statements and result in boolean values: True or False. There are three boolean operators:

 

 and  Returns true if both statements are true
 or  Returns true if at least one of the statements is true
 not  Gives the opposite of the statement
 
and/or/not Truth Tables:
Note: The “true” and “false” keywords should be written with a lowercase “t” and “f” in Ruby.
 
 
Consider what will be the boolean value in the following examples:
 
Set bool_one equal to the result of false and false
 
Set bool_two equal to the result of 100 / 50 == 40 / 20 and 1 ** 2 > 1 ** 5
 
Set bool_three equal to the result of Math.sqrt(16) != 2 ** 2 or false
 
Set bool_four equal to the result of 4 < 2 ** 3 or 10 % 4 <= 1 + 1
 
Set bool_five equal to the result of true and true
 
Set bool_six equal to the result of not 4 < 2 ** 3 and 10 % 4 <= 1 + 1
 
Set bool_seven equal to the result of not false
 
Conditionals
The simplest conditional statement is an if-statement. An if-statement executes specified code after evaluation an expression to be True.
 
Consider what will be displayed on the screen in the following examples:
 
if 3 < 10
    puts (“Three is less than ten!”)
end
 
x = 5
if (x ** 2) == 25
    puts (“w00t!”)
end
 
if (21/7) >= 4
    puts (“Expelliarmus!”)
end
 
The else-statement compliments the if-statement. Together, they say, “If an expression is true, run this code; otherwise, run this other code.” Unlike the if-statement, the else-statement doesn’t depend on the evaluation of an expression to be True.
 
Consider what will be displayed on the screen in the following examples:
 
if 5 < 1
    puts (“Five is less than one!”)
else
    puts (“Five is NOT less than one!”)
end
 
y = 1
if y <= 0
    puts (“Please join me in the ballroom for tea and scones!”)
else
    puts (“You are not welcome here! Go!”)
end
 
good = 1
evil = 0
if good > evil
    puts (“Dumbledore’s Army triumphs!”)
else
    puts (“Crucio! Imperio! Avada Kedavra!”)
end
 
Lastly, the elsif-statement compliments the if- and else-statements. The elsif-statement is only checked if the original if statement is false.
 
Consider what will be displayed on the screen in the following examples:
 
answer = 7 
if answer < 1
    puts (“The answer is less than one!”)
elsif answer >= 1 and answer < 10
    puts (“The answer is between 1 and 10, but not equal to 10!”)
else
    puts (“The answer is 10 or greater!”)
end
 
white_knight = “Winner!”
if white_knight == “Loser!”
    puts (“The dark knight has won the list!”)
elsif white_knight == “Winner!”
    puts (“The white knight has won the list!”)
else
    puts (“Both knights were knocked off of their horses.”)
end
 
item = “Elder Wand “
if item == “Resurrection Stone”
    puts (“You now possess the #{item}. Congratulations!”)
elsif item == “Invisibility Cloak”
    puts (“You now possess the #{item}. Congratulations!“)
else
    puts (“You now possess the #{item}. Congratulations!“)
end
 
Practice what you’ve just learned!
Download the file “CCC.rb” below. Open the file with Notepad++ and follow the instructions to complete the program.