Conditional Statements - Coding Basics #2

If You read -> You Learn


Conditionals.png

Shoutout to Flocabulary

HiveDivider.png

In this article you'll find:

  • Introduction
  • What is a conditional?
  • The Elif - Else If
  • Is there an alternative to the If-Else

HiveDivider.png

Greetings to all, I did not expect such a warm reception of my previous article. That is why, I have decided to turn this into a series where I will make basic programming concepts understandable, even by those who have never written a line of code.

Today we will go with conditionals. After understanding what is a variable and Datatypes, most likely the next step to take is to learn about conditionals.

You might think that conditionals are one step away from building rockets, but this is far from reality, and I'll show you now.

Let's get started!

HiveDivider.png

What is a conditional?


images.png

Shoutout to Programiz

According to the RCSDB Code website, a conditional statement is "A type of coding instruction used to compare values and express and make decisions". However, this definition makes life more difficult than it should be.

Simply put, a conditional tells the program to run different instructions depending on whether a condition is true or false. To understand It better, take a look at this image:

image.png

There's a question or condition: Should I drive drunk? This condition has two outcomes: Yes and No. If you choose Yes, something happens. If you choose no, another thing happens. This is the essence of a conditional statement.

However, translating this into code that our programming language can understand, we will see the following:

if john_driving_drunk == True:
    print("You just crashed and now you owe 20,000$ in damages")
else:
    print("""You woke up the next morning with a clogged dishwasher in your brain\n
But you're alive""")

Using third-grade English logic: If John drives drunk, he's going to crash and get a big ticket.

Otherwise (Else), John does not drive drunk and suffers from a hangover, but he is safe and sound.

The key words here are If and else, which will be the basis of all conditional statements. Now, if we declare that john_driving_drunk is True and execute:

john_driving_drunk = True

>>> You just crashed and now you owe 20,000$ in damages

And in the opposite case:

john_driving_drunk = False

>>> You woke up the next morning with a clogged dishwasher in your brain

But you're alive

In the vast majority of modern programming languages, you will see the if-else combination whenever you are looking to execute an instruction, only if something is true. But what if we have more than one condition?

HiveDivider.png

The Elif - Else If


ifelse9_nk5ugf.webp

Shoutout to DataCamp

Sometimes, we want to have more than one condition, which is useful if we are working with a large number of options, with numbers or with letters. This is where the Else If (or Elif) comes in handy.

The Elif works like an Else, only now instead of executing an instruction whenever any condition opposite to the If condition is met, it will now have another specific condition. Let's say that John now has a third option.

image.png

Now, since the values of True and False are boolean (it can only be one or the other, with no values in between), we will have to change the value to be considered to a string that says "Yes", "No" or "Call your Ex". Seeing this in code:

if john_driving_drunk == "Yes":
    print("You just crashed and now you owe 20,000$ in damages")
elif john_driving_drunk == "Call your ex":
    print("Bro, you just called your ex and told her that you still loved her\n What's wrong with you?")
else:
    print("""You woke up the next morning with a clogged dishwasher in your brain\n
But you're alive""")

Here we can see the elif in action, where, if and only if john_driving_drunk is equal to "Call your ex", the instruction is executed.

Note: At the end, we use else since we assume that any value other than "Yes" or "Call your ex", will be "No".

Now, if we assign "Call your ex" to john_driving_drunk:

john_driving_drunk = Call your ex

>>>Bro, you just called your ex and told her that you still loved her
 What's wrong with you?

HiveDivider.png

Is there an alternative to the If-Else?


matchcase.png

Shoutout to GeekPython

Of course. A very popular alternative is the use of switch-case statements. These are often used when we have a large number of conditions, as they are much easier to structure.

To write a switch-case, we first define the switch or match (it depends on the language), and inside it, we place each case and the instruction to be executed in case it is fulfilled. An example taken from freecodecamp for Python:

match lang:
    case "JavaScript":
        print("You can become a web developer.")

    case "Python":
        print("You can become a Data Scientist")

    case "PHP":
        print("You can become a backend developer")
    
    case "Solidity":
        print("You can become a Blockchain developer")

    case "Java":
        print("You can become a mobile app developer")
    case _:
        print("The language doesn't matter, what matters is solving problems.")

We can see how the "match" set of conditions is defined first, and then the cases are set.

HiveDivider.png

As you can see, conditionals are no big deal. Applying logic that would be used in everyday life and some examples are enough to get to grasp quickly.

In this way, I want to let you know that programming is not complex, it just requires understanding how things work before using them, this and constant practice.

Challenge: Write your own conditional statement with if-elif-else. Let's see how it goes.

HiveDivider.png

Thank you for your support and good luck!

HiveDivider.png

@jesalmofficial.png



0
0
0.000
2 comments
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support. 
 

0
0
0.000
avatar

Congratulations @jesalmofficial! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You received more than 3250 upvotes.
Your next target is to reach 3500 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

Women's World Cup Contest - Quarter Finals - Recap of Day 2
Women's World Cup Contest - Quarter Finals - Recap of Day 1
LEO Power Up Day - August 15, 2023
0
0
0.000