
= - the "greater than or equal to" symbol. (The same holds true for the = operators shown below.) The final two example show that you can not use with values of different types. If both strings are equal up to the length of the shorter string as in the final example, then the shorter string is considered less than the longer string. Once it finds a character that differs, it compares that character with its counterpart, and makes a decision based on that. Ruby moves from left-to-right in the strings looking for the first character that is different from its counterpart in the other string. When comparing strings, the comparison is character-by-character.

The examples numbered 005, 006, and 007 are especially tricky! Make sure you understand them. # Example using 'less than' and 'greater than'ĪrgumentError (comparison of Integer with String failed) Anything to the left of the symbol has a higher value than anything to the right of the symbol. However, if the two values have different types, the return value is true. irb :001 > 4 != 5Īs with =, we can easily compare two values of the same type for inequality and get reasonable results. Anything to the left of the symbol is not equal to anything to the right. Thus, the string '5' is not the same as the number 5. We threw that last example in as a reminder that two values must have the same type or they are not equal. If there is any difference at all, the strings are not equal. The specific value doesn't matter, just that both strings have the same value. To be equal, two strings must have the exact same value. Notice that we can compare strings for equality. We talked about this operator earlier in our chapter on variables so it shouldn't be totally foreign. Anything to the left of the symbol is exactly equal to anything on the right. In comparisons, the expressions to the left and right of the operator are the operands. The expressions or values that an operator uses are its operands. We'll try them out in irb to see how they work as well. A boolean value is either true or false, nothing else. One thing to remember is that comparison operators always return a boolean value. Let's go over these comparison operators in a little more depth so you can build some more complicated conditional statements. It acts as the opposite of if, so you can use it like this: puts "x is NOT 3" unless x = 3 Example 1 from above could be rewritten like this: puts "x is 3" if x = 3 Last, because Ruby is such an expressive language, it also allows you to append the if condition at the very end. # Example 4: must use "then" keyword when using 1-line syntax The examples below are all valid Ruby conditionals. We have effectively controlled the flow of the program by setting conditionals in an if statement.
#Javascript does not equal integer code
What your code is doing is checking, using the = operator you learned previously, to see if the input is equal to the number we have defined. You can repeat the third step more than once to see its effect.

Here we are using gets to let the user input a number, chomp gets rid of the new line created when the user enters the data, and to_i is a method that can be called on a string to turn it into an integer. Enough talking, time to code.Ĭreate a file called conditional.rb and type the following code into it.

They are basic logical structures that are defined with the reserved words if, else, elsif, and end. Conditionals are formed using a combination of if statements and comparison and logical operators (, =, =, !=, &, ||). Your data approaches a conditional and the conditional then tells the data where to go based on some defined parameters. ConditionalsĪ conditional is a fork (or many forks) in the road.

How do we make data do the right thing? We use conditionals. In computer programming, this is called conditional flow. You want your data to do the right thing when it's supposed to. When you are writing programs, you want your data to make the right decisions. Which roads are chosen depends on the end goal. Like any journey, one must travel a given path. Along this journey, data encounters many things that have an impact on it and it is forever changed. In many ways, a computer program is like a journey for your data.
