Testing Variables and Branching

Learn about conditional expressions with the help of interactive examples.

Variable comparison

One of the foundations of any programming language is variable comparison, or variable testing. Depending on the comparison result, we can execute this or another part of a program. Here is an example scenario: if the user’s age is less than 18, we will either ask them for additional information or we will block access for this user. There is a special vocabulary that is used for this functionality. Let’s take a closer look at it:

  • Branching: It is implied that a program may have multiple branches and chunks of code, which can be executed if a certain condition is met.

  • Branch, block, code block: This is usually a few lines of code that can or cannot be executed under certain conditions.

  • Comparison, testing, test, “if statement,” condition: This is the comparison itself, which involves checking if the variable or value equals, does not equal, is greater than, less than, truthy, or falsy, and so on. Experienced programmers often use the word “test,” which means “testing the variable for a certain value.” In Linux, macOS, or POSIX-compatible operating systems, we can get documentation about testing variables in the shell (not Ruby):

$ man test
...
test - check file types and compare values

Later in this course, we’ll cover very basic unit testing and create our own tests. Those will also be tests, but of different kinds. Usually, unit tests have multiple lines of code, and variable testing has only one or two lines of code.

Let’s see how variable comparison works. We will enter our age in the input box below before pressing the “Run” button.

Get hands-on with 1200+ tech skills courses.