Conditional Statements in Programming - A comprehensive guide.

Conditional Statements in Programming - A comprehensive guide.

ยท

7 min read

Introduction

Part of the most important basics of programming include variables, loops, conditional statements, functions, and basic data structures like arrays. Any good tutorial or guide to learning a programming language will always have these concepts come first before going to teach how to build software programs that work. There is no other reason for this than the undeniable fact that these are the foundations of any given program. In this article, I would be going over one of these foundational concepts of programming: Conditional Statements.

Prerequisites

This guide doesn't make use of any programming language, instead, pseudocode is what I use to demonstrate the sample programs. So anyone learning any language can learn about conditional statements and loops here. Let's go!

Conditional Statements

In programming, a conditional statement is a line or block of code that runs only if a condition is met. Wikipedia says:

In computer science, conditionals (that is, conditional statements, conditional expressions, and conditional constructs,) are programming language commands for handling decisions. Specifically, conditionals perform different computations or actions depending on whether a programmer-defined boolean condition evaluates to true or false. In terms of control flow, the decision is always achieved by selectively altering the control flow based on some condition (apart from the case of branch predication). So from the name given to it, a "conditional statement" checks that a particular required condition is met (or evaluates to true), then it runs the line or block of code enclosed in its scope.

Drake Hotline Bling meme

Generally, in programming, there are 4 types of conditional statements. They are: if-else conditional, if - else if - else conditional, switch-case conditional, and the ternary operator conditional.

If-else conditional statement -

The if-else conditional statement can be thought of as the most commonly used type of conditional statement. It is very accessible and straightforward to use. Consider the computer program below:

# a program to demonstrate if-else statements
start

let small_number = 4
let big_number = 12

if: small_number < big_number
   print: "Maths is simple๐Ÿ˜€, 4 is less than 12"
else:
   print: "Oh! Here we go again, how is 4 greater than 12?!๐Ÿ˜‘"

# program flow continues
end

If the program above is implemented in any language and is executed, the output should display: Maths is simple๐Ÿ˜€, 4 is less than 12 obviously because 4 is less than 12. Well except something has gone wrong๐Ÿ˜‚. But if by chance, something went wrong, and the program discovers that 4 is not less than 12, then the output will display what is contained in the else statement: Oh! Here we go again, how is 4 greater than 12?!๐Ÿ˜‘. It is after executing either of the statements, whether if or else that the program passes control to the running function body and continues the execution flow. An else statement is not a requirement for every if statement. That is, you can decide to not have an else block of code to run if the condition in the if is not met. In cases like this, the program just continues running and subsequent lines/blocks of code are executed normally as if they were inside the else block.

if-else if-else conditional statement -

This is another type of conditional statement with support in most programming languages. It is basically just another kind of if-else statement, but this time there's an else if. What's that all about? Consider the following program:

# demonstrating else if statements
start

let small_n = 4
let midi_n = 8
let big_n = 20

if: small_n == 6
   print: "My small number is 6, what's yours?๐Ÿ‘€"
else if: big_n == 20 and big_n <= midi_n
   print: "Big number is less than 6? How?!๐Ÿฅด"
else if: midi_n > 5
   print: "Medium number is 8, so it's greater than 5. Yes!๐Ÿ™‚"
else:
  print: "oh well, it's screwed! Bye"

# do remaining stuff in the program.
end

If the above program is being examined and executed, it is observed that the output displayed will be what is in the second else if statement, simply because it is the first condition that turns out to be true out of all the conditions there. In cases where an if-else if conditional flow is implemented, the program looks for the first condition that evaluates to true and executes the block of code there. This means that if there are up to 10 conditions and the 3rd condition evaluated to true, the remaining else if statements will not be checked even if they are also evaluating to true. This program also shows how logical operators are very much useful and important when using conditional statements. Logical operations always evaluate to a boolean value, I.e true orfalse, so an operation that evaluates to true means the condition was met, and one that evaluates to false means otherwise. Find out about Logical operators here. The sample program also shows that you can have as many else if statements as you want in a single flow.

Note: An else statement ending every if-else if may not be required depending on the language, but it is good practice to always have an else statement ending the if-else if conditional statement flow.

switch-case conditional statement -

This is a type of conditional statement that is a bit different from the two types discussed already. A switch statement works like a series of nested if-else statements but, it uses a case keyword to check the value of an input variable. The value of this variable is what causes a change in the execution flow of the program. That is, if the variable's value is the same as what is passed to a particular case, the line/block of code that follows the case is executed. This is shown in the following program which is a simple calculator:

# demonstrating switch statements
start

let first_number = 3
let second_number = 7
let operator = "+"

switch: operator
   case "-": 
       print: first_number - second_number
   case "+":
       print: first_number + second_number
   case "x":
       print: first_number x second_number
   case "รท":
       print: first_number รท second_number
    default:
       print: " I don't know what to do with the numbers..."

end

The above program has just demonstrated what a switch statement looks like and how it works. Notice how the operator variable was defined with the value "+", and how it was passed to the switch statement. When the program is implemented in any programming language and executed, the output displayed is 10 which is the sum of 7 and 3, because the value of the operator variable is actually "+", so an addition operation is carried out on the 2 numbers.

Note the use of default in the code. The default statement usually contains the code to be executed if none of the conditions are met. It is a requirement whenever the switch-case conditional statement is used.

Ternary operator -

This is the last type of conditional statement and it is the one with the simplest and shortest syntax. It is mostly used when assigning values to a variable only on condition. Most programming languages use the ? and : to express the ternary operator conditional statement. Consider the program below:

# using the ternary operator
start

let x = 12

let y = x + 1 == 13 ? "Yes" : "No"

end

This looks creepy, right?๐Ÿ‘€๐Ÿ˜… So what is going on up there? Let's see! What that single line of code does can be interpreted as: "if x(which already has a value of 12) plus 1 is logically equal to 13, then assign y a string value of "Yes", else assign it a "No". Therefore it is easy to translate the ternary operator statement to a basic if and else statement.

it is also possible to have the ternary operator check for many conditions at a time, and it can as well be nested. "The control flow meant to be controlled by you" ๐Ÿ˜‰๐Ÿ˜…

Conclusion

In this article, the concept of conditional statements in the world of computer programming has been broken down into simple terms. It is good and advisable to apply the concepts learned here to practical use in whatever language you wish. A glance at the syntax of your language will make it quite easy to convert all the pseudocode above into working programs. If anything still looks unclear, feel free to ask questions in the comment section or any of my social media handles๐Ÿ˜‰.

References

ย