Skip to main content

How programming conditionals works

When we are starting in programming, in some moment we'll encounter with conditionals. Conditionals are expressions that tell the program what to do in certain conditions. Theses statements are essential in programming, not only for beginners, but for every developer who wants to create a solution in their software.

First, to completely understand the use of conditionals we can connect it to the real life. In our daily routines why use conditionals to take decisions. For example, if I had a job interview but I'm running late, I'd say, "If I go by foot, I might not arrive at time, but if I take the subway I'll arrive just at time." Notice that I use if to show that I'm taking about a condition and what will happen if that condition is met. Well, the same is for programming.

- Second of all, before showing you code examples I'd like to briefly explain you the Truth Table. This table helps us to know when conditions combinations are true or false, it means when the final condition will be met or not. Here is a basic truth table.

Table created using designcap 

A and B represent the value below it, either T(true) or F(false). The values below AND and OR are the result of evaluating the two left values. For instance, AND to be true, both A and B must be true or else its false. OR to be true, A or B must have at least one true.

Also, there is NOT which is the negation of a value. For example, if A is T, the result is the opposite, F. If A is F, the result is T.

Next, one more step before putting all this into code. There are some symbols to identify each operation. Programming languages uses the following.

AND is represented by &.

OR is represented by |.

NOT is represented by !.

Finally, let's apply all this knowledge to some code examples. 

To use AND, we'll solve the next problem. "If the student is older than 18 and his class is business, print 'Congrats, you can apply for the job' or else print 'Sorry you must be older than 18 and be in the business class'"

To use OR, we'll solve the next problem. "If the student is older than 18 or has a dog, print 'Congrats, you can be a dog sitter' or else print 'Sorry you must be older than 18 or have a dog'"

To use NOT, we'll solve the next problem. "If the student career is related with medicine , print 'Congrats, you can apply for being a doctor' or else print 'Sorry you have to be studying medicine'"

To conclude, we use conditionals in order to execute certain code segments. These statements are very helpful when we want to create a solution. Programming languages have symbols to represent each condition type and also some keywords are similar like if and else. You also understood the truth table and how to use conditionals in real code exercises.

Comments

Popular posts from this blog

How to Install or Upgrade neovim, the easy way

 The easiest way to get neovim on ubuntu or wsl ubuntu, is to simply run sudo apt install neovim , however, later on you will want to update it, and you will try to uninstall and run sudo apt update and then install neovim again. All of that to realize that the version remains the same. I found that the second easiest way of installing and upgrading neovim is to install it form ‘Download’. This will allow you to install the exact version of neovim that you want, and even better, to keep multiple versions of neovim at the same time. Beyond that you can code a script to automatically switch between versions. This is how I reinstalled neovim without loosing any configuration. I used this article as a reference: https://docs.rockylinux.org/books/nvchad/install_nvim/ . Backup your config files: I would recommend you to create a Github repository of your neovim configuration files, just in case. Check your current nvim bin file: The location might change depending on how you insta...

How to start programming

 When you're starting in your process to become a Software Developer, either on your own or at a college, you'll need no understand the programming logic and how to solve different problems before learning a programming language. That's why I provide this tips and steps in order to understand programming. First of all, you need to know basic concepts of computer science, such as how a computer works, parts of a computer, and some logic problems. To involve in computers world you can research about the afore mentioned topics. I recommend you to watch videos, read short posts and also trying to teach someone what you just read. The second step I followed to involve in problem solving is creating flowcharts . I used to transform a bunch of exercises into flowcharts, and it helped me to understand how the programming logic works and to see many ways to solve the same problem. Though you need to learn some figures and know when and how to use them in order to create a correct ch...

Loops in programming

 If we are starting in the programming world, we've ever came across with a problem that has repetitive code or, also when we're working with array and many other similar situations. In this blog, I'm going to explain what a loop is, its types and some examples. First, a loop is something that is repeating many times until we decided when it to stop. Taking an example from real life, it could be your daily commute, you go to your work or school every day in the same way. Now, applying it to programming, a loop can execute a specific block code the times we want. The perfect example is a counter, but we'll see this example further in the blog. Second of all, let's see some kinds of loops and which ones are the most common in real programming. In my opinion, the most used loop is for or foreach.  It is perfect to iterate over all the items in an array, since arrays are very common when getting information from databases such as users, products, etc. It also needs a st...