Coder Social home page Coder Social logo

phase-3-control-flow-operators's Introduction

Control Flow: Operators

Learning Goals

  • Use common comparison methods for control flow (==, !=, >, <)
  • Use common operators for control flow (&&, ||, !, ?:)
  • Understand the differences in syntax between Ruby and JavaScript

Introduction

Just like JavaScript, Ruby has several ways we can control the flow of execution in our programs:

  • We can use conditional statements like if/else and switch/case
  • We can use looping constructs like loop (Ruby) and while (JavaScript)

Using these control flow constructs means we're taking our code out of the normal flow of execution (top-to-bottom, one line at a time) and instead providing some instructions to change that order. As you've surely seen in JavaScript, conditional statements and loops are critical for writing applications.

In the next series of lessons, we'll explore common approaches to control flow, and learn some new syntax that is unique to Ruby.

Conditional Operators

In general, using control flow in any language means running certain code if some condition is met. That means that in order to use control flow effectively, it's important to know how to use conditional operators to compare values.

Comparison Operators

In Ruby, many built-in classes have the following methods that can be used to compare two values:

  • >: greater than
  • >=: greater than or equal to
  • <: less than
  • <=: less than or equal to
  • ==: equal to
  • !=: not equal to

Unlike in JavaScript, the == method in Ruby will not coerce strings to numbers before comparing them, or perform some of the other type coercions that JavaScript does. For example, in JavaScript, using the == operator can lead to some strange behavior:

"1" == 1
// => true
0 == []
// => true
[] == ![]
// => true ๐Ÿค”

In Ruby, the == method checks if the objects on both sides are considered the equivalent values:

"1" == 1
# => false
1 == 1
# => true

There are some differences between Ruby's == and JavaScript's === though. In JavaScript, the === operator checks if both objects have the same identity, i.e. refer to the same space in memory. For example, in JavaScript, this example returns false because the two arrays are unique objects in memory:

[1, 2, 3] === [1, 2, 3];
// => false

In Ruby, this example returns true because Ruby considers these to have equivalent values:

[1, 2, 3] == [1, 2, 3]
# => true

Ruby will also check if an Integer has the equivalent value to a Float, even though they're technically different data types:

1.0 == 1
# => true

The reason for this is because in Ruby, the == operator is actually a method that has unique behavior that depends on which class the method is defined in! The code above is equivalent to the following method call:

1.0.==(1)

Because Ruby heavily favors object-orientation as a language, you'll find that many features other languages would define as operators (like ==, >, even math operators like + and *) are actually methods. If you're unsure what these methods do in a particular scenario, check the class definition in the documentation for the data type on the left-hand side of the == (for example, here is the documentation for the Array class and the Integer class).

Note: While Ruby does have a === method, it is not used the same way as it is in JavaScript. There are very few scenarios when you want to use the === method in Ruby; in general, for comparing data, you want to use the ==. See here for examples if you're curious about what this method does.

Logical Operators

Ruby has the same logical operators you'll find in many other languages, including JavaScript:

  • &&: Logical and. Are both values truthy?
  • ||: Logical or. Is one or the other value truthy?
  • !: not. Coerces the data to its boolean equivalent, then reverses it (true becomes false, and vice versa).
true && true
# => true
false && false
# => false
false && true
# => false
true || true
# => true
false || false 
# => false
false || true
# => true
!true
# false
!!true
# true

Ruby also has the ternary operator (?:) for writing an inline conditional statement:

age = 1

age < 2 ? "baby" : "not a baby"

This is the equivalent of the following if/else statement:

age = 1
if age < 2
  "baby"
else
  "not baby"
end

Conclusion

In the coming lessons, we'll be writing some methods that use control flow, so make sure to keep these methods for comparing data in mind โ€” they'll be very important to your ability to write conditional logic and looping code successfully!

Resources

phase-3-control-flow-operators's People

Contributors

ihollander avatar graciemcguire avatar lizbur10 avatar adam-larosa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.