If, Else, and Other Conditionals that Exist

Hello fellow coding newbies. I decided to do my first ever blog post about a topic that I, myself, have struggled with as a new student of coding. Being new to Ruby, the conditional statement can be a confusing topic to grasp.

decisions, decisions, decisions

As ruby processes our code it moves in a single direction reading and responding to our code line by line. But sometimes, we want the program to “pick a side” based on conditions that we set. We can do this with some ‘if’ (conditional) statements. This can be similar to a regular “human” language. If some condition is true, then run this code.

if the current temperature is below 35 write “it’s freezing out there!”

Don’t forget to close your ‘if’ statements with an ‘end’.

Adding an ‘else’ in there can really can spruce your statement up, and this is when “decision making” begins to happen.

otherwise write “its not so cold out there”

Want to throw in another layer of decision making? Just drop in an ‘elsif’ in between the ‘if’ and ‘else’ and see what the program spits out.

In this case, our program will not know the temperature outside. We can use as many ‘elsif’ statements as we want and have a bunch of conditions to choose from.

If statements can be a great resource to put a variety of situations and variables to the test to achieve your desired output.

In this situation I am iterating through all of the students and comparing to a particular student, iterating through all the boat tests and comparing that to a specific test. I am then asking the program, did this particular student take this specific test before? If so, we can change the status of this student’s boat test to “passed”. If not, create a new ‘boat test’ instance where this student is considered “passed”.

Happy coding fellow coders, and don’t forget your ‘ends’!!!

--

--

--

Software engineering student, string cheese enthusiast!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrated Service Management: didn’t we already do that?

The importance of HTML semantic tags

Getting started with APIs | A simple hands-on practice

Cheapest Smart TV with Pi Zero

Intersola Launchpad V2 — Staking Migration

Measuring the Bull & Bear Power to Generate Trading Signals — A Python Back-test.

Non-Nullable Types in C#: Backwards Compatibility

A More Efficient Implementation of an Animated Pie Chart in SwiftUI 2

animated pie chart

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yevgeniy Valdman

Yevgeniy Valdman

Software engineering student, string cheese enthusiast!

More from Medium

Yup, this is going to be good

How I spent my holidays

John Kovalic and On Being Class-less — A Munchkin CCG Review

The Lookalike