Logical Operators in Ruby for Beginners 11
![](https://i0.wp.com/allprowebdesigns.com/wp-content/uploads/2023/12/1702050912_maxresdefault.jpg?resize=840%2C430&ssl=1)
Video Title: Logical Operators in Ruby for Beginners 11
Hello everyone today we’re going to be continuing where we left off by covering some logical operators these are going to be things we’ve already used a couple times in previous videos but I wanted to make a dedicated video to them so you have a quick and easy way to look up this
Information The Logical operators we’re going to be covering today are the and operator the or operator and the not operator these are pretty simple once you get used to using them but effectively what they allow you to do is inside of an if state or in some other
Locations uh create like a combination of conditions so you can think of it something like this that logical operators are used to combine multiple conditions and return a single Boolean value example of this might be uh if I have an age so let’s say I have like a
User age is equal to I don’t know like 20 so if the user age is uh greater than or equal to 16 and it’s less than 18 then you might want to print something like um you can drive but only with an adult and of course I have to change
This because we’re in Ruby land so we don’t actually need that colon there I’m still thinking about this like I’m in Python uh and then we say if the user age is less than 18 so if we are older than 16 but less than 18 years old we
Can drive but we need an adult to be in the vehicle with us right so that’s where we can use these uh combinations to um get you know multiple uh conditions together to sort of satisfy what we need because we won’t always just have one clear condition that we can
Use we kind of have to combine them now that said this is technically the same thing as doing something like if the user is greater than or equal to 16 then we can say if the user age is less than 18 uh we can print this and then we can
End this so if we Nest these two which is what this is called if we put one condition inside of another it’s essentially the same as this one uh but of course you know one of these is going to be a little bit easier to read than
The other and it’s going to be a little bit easier to work with in the same vein there’s always going to be uh some conditions that don’t cause this to be satisfied and for that you can use something called a truth table for this we can take a look at the truth
Table for the and operator so we’ll start by saying this is the and operator and then we can say for this both conditions must be true for the whole expression to be true what that means is if you have one condition and another condition and they’re both true you’ll
Get back true if you have one condition that’s true and one that’s false you’ll get back false and similarly if you have the inverse of that where one is false and one is true you’ll get get back false and finally if both are false you’ll also get back false so in this
Case when you’re using the and the only time you get a true condition that allows you to enter the if statement is going to be when both conditions are true now there’s a similar option which is the or and this one’s going to be where one of the conditions has to be
True in order for it to be true so you can see here we have three falses for the and but for the or operator we actually have three trues instead so the only way to get a truly false result with the or operator is for both conditions to be
False and finally we have the not operator the not Operator just takes a condition and just inverts it so if you have true and you use not true you get false and not false gives you true this is situationally going to be useful for you if you want to like make things a
Little bit more readable it’s often times where you’ll see the exclamation mark in front of a condition or a Boolean variable that you have but let’s take a look at how to actually use something like this so let’s come down here and let’s create a examples block
And again all of this code will be available in the video description you can just go to the GitHub page so here we’re going to check if a number is between 1 and 10 now if we were only checking one condition we would only need one if statement but here we’re
Checking two so let’s see how we can do that let’s start by entering a number so we’ll say puts enter a number and then we’ll use gets to get a number from the user and convert it to an integer so now we have our number let’s go ahead and
Let’s check if that number is between 1 and 10 we could do this by saying if the number is greater than or equal to one which means it’s at least one one would also satisfy this condition and if the number is less than or equal to 10 which
Means even 10 would satisfy this condition so if the number is 1 2 3 4 5 6 7 8 9 or 10 then we want to print out the number is between 1 and 10 else we’ll just say the number is not between 1 and 10 and then we can end this so
This right here will work but let’s go ahead and let’s comment out the code we had previously so that we only run this and then we can run RB and then main. RB inside of this file It’ll ask us for a number let’s try five you can see five
Is between 1 and 10 we can run the program again we can enter I don’t know 99 we can see that number is not between 1 and 10 and similarly if we run it one last time and enter -450 that number is also not between 1
And 10 so that’s our first example here let’s take a look at using or instead so if we want to check if a number is between 1 and 10 we can check if it’s you know greater than or equal to 1 and it’s less than or equal to 10 or we can
Check that it’s not uh in this range and the way we can do that is just check if a number is not between 1 and 10 we’ll go ahead we’ll at the number again and then we can check if the number is less than one which means it’s
01 -2 or if the number is greater than 10 which means it’s 11 or 12 remember in this case because of this being an or if if it’s less than one it’ll return true already we don’t even need to check the second condition similarly if the number
Is greater than 10 it’ll also return true so what we have to do then is we have to take the else line here from our if statement previously and we can put that into this if statement and then we can use the uh line in our previous if
Statement to put that into our else statement the reason why we do this is because now we’re actually checking the opposite condition as before which means we have to switch these around too so now if the number is less than one or the number is greater than 10 we print
That the number is not between 1 and 10 we’ll get the exact same results we had before we can enter this with main. RB put in a number of five we’ll see the number is between 1 and 10 because five is not less than one and it’s not
Greater than 10 which means we’re in the else block here you can do something similar put in 15 that number is not between 1 and 10 because 15 right here is going to be greater than 10 this is now true so then we enter this block and
We can see that number is not between 1 and 10 finally we can do like -3 that number is not between 1 and 10 as well and just to be clear we can also enter zero and we can see that Zer is not between 1 and 10 as well okay so that
Takes care of those cases now let’s take a look at our final example for our final example we want to check that the number is not between 1 and 10 so let’s go ahead and let’s get this number again but this time what we’ll do is we’ll say
Uh let’s store the result that we had previously so let’s create a variable call this between 1 and 10 set this equal to the number is greater than or equal to one and the number is less than or equal to 10 and just as an aside you
Could also do could also do uh between between 1 and 10 so we’re just going to overwrite that number and then we’ll just say the number is just like before less than one or the number is greater than 10 this is going to give you a slightly different result but what
We can do now is we can say let’s grab this and let’s invert it let’s use that not operator so if the between 1 and 10 is not true then we want to print that number is not between 1 and 10 else we could print that number is between 1 and
10 and we could do something very similar here where we say if it’s between 1 and 10 and then we just switch these around so both of these are going to be functionally the same but sometimes you really just want to use the KN operator let’s say you only want
To print out that the number is not between 1 and 10 in that case you don’t need this else block so you just have this if you didn’t have this inverter you’d then have to do this put in the else make sure nothing happens here and
Then have this else block and that just doesn’t make as much sense that’s four letters as opposed to just one extra character that we have to add so we can do this right here we can then run our program we can enter a number let’s enter five uh and let’s make sure that
We’re actually uh printing off the else block as well so let’s try this again let’s enter a number five and we can see that number is between 1 and 10 let’s enter 11 oops uh let’s run the program and then enter 11 and we can see 11 is
Not between 1 and zero and then let’s run it one final time and enter zero and we can see Zer is not between 1 and 10 as well so that’s effectively going to be it for this little lesson on our logical operators just as a reminder we
Have the and operator which has the following truth table where both values need to be true in order for be true we have the or operator where both values need to be false in order for it to be false and we have the not operator which
Is just going to give you the opposite of what you have that’s going to do it for now thank you so much for watching and hopefully I will see you in the next video
Video Keywords: Ruby Programming, deanin,ruby for beginners,logical operators,ruby tutorial for beginners,programming in ruby,ruby on rails for beginners,coding tutorial in ruby,ruby language for beginners,learn to program in ruby,ruby programming part 11 (logical operators),or operator (intro to logical operators),ruby for beginners tutorial,ruby logical operators,getting started in ruby,beginner,ruby tutorial for absolute beginners,ruby logical operators – speak khmer,learn ruby for beginners
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99