A Fork in the Road

The case of Conditionals

Before jumping into Flatiron School, and broadening my coding horizons, I was a data quality analyst for a mortgage company. We worked with VB.net to bring our customers an easy and customizable experience when generating mortgage loans and documents. We had over 100 hundred different clients, and each client we treated with integrity and promise that their product would not only work but also look how they wanted it to look.

Keeping this promise meant making sure our clients were happy, but it was not always easy. We worked out of a single dev environment, which meant our clients shared a lot of logic. To keep a balance between providing custom, and compliant regulatory information we heavily utilized conditional statements in our code. Some examples of conditionals are If/else, and Case statements.

When I started going through the coursework at FlatIron, I immediately grasped these conditional statements because I had been around them for the last five years of my life. Having this background knowledge really helped me feel comfortable transitioning into a developer role.

I want to share my knowledge of some of these conditionals and when to use them properly.

If/Else

Probably the most common, and most easily understood conditional statement, is an ‘if’ statement. An if statement will evaluate an expression, and return a boolean result. Depending on whether the result is true or false, the system will execute different blocks of code.

Example:

A user is entering their name and address info on a form, there is a space to name which is stored in the ‘name’ variable. When the user presses a submit button on the form, the system could run an if/else statement to check to see if the user entered their name into the value. If they entered their name, return a welcome message. If not the system will return an alert to enter their name.

The ‘else’ of an if/else statement can be the default block of code if the expression is false, or you can add another expression with ‘else if’. So if the first ‘if’ statement is false, then it would evaluate another expression, and you could add different blocks of code to be executed depending on whether the second expression is true or false.

This chain of ‘if’ and ‘else if’ can continue on indefinitely, however adding more than one or two can result in code that is difficult to follow and opens it up to errors. Luckily there is another way to handle multiple conditionals. The Case statement.

Case

Case statements — or Switch statements in javascript — are best used when you have one variable field that could have multiple input values.

Example:

A user enters the abbreviated state they live in, the conditional logic will check the ‘state’ variable input against each case (in this case it could be all fifty states), and depending on what the value is, it will return different results.

User input State = NY, system output will return ‘New York’

Ternary

A ternary is similar to an if/else statement, it evaluates an expression, and executes a different block of code based on true or false. Ternaries are a shorthand way of writing an if-else statement, and there isn't really anything that compares to ‘else if’ so ternary is best to use when you only need to evaluate one expression.

Example:

A user is checking to see if the ‘number1’ and ‘number2’ variables add up to three, if true, the system will console log ‘True’. If the expression evaluates to false, the system will console log ‘False’.

Notice the syntax of a ternary. it uses the question mark (?) to signify that the expression to the left of the question mark is being evaluated as a conditional. The colon( : ) is used to separate the two expressions that should execute depending on the result, the colon is the same thing as an ‘else’ statement. To understand it a bit more, below is the same example written as an if/else statement.

Conditionals are almost universally used across languages. The syntax may differ between languages, but as long as you understand the concept, they are a great tool to have!

Hello! I’m Jason and I'm starting the journey of becoming a great software developer! I want to share my experiences with all the other green coders out there!