Computer scienceAlgorithms and Data StructuresIntro to algorithms and data structuresPseudocode and representation

Relations and conditions in pseudocode

2 minutes read

In our previous topic, we covered the basics of pseudocode: variables, assignments, operations, and comments. In this topic, we're diving into a new dimension that adds real capability to your pseudocode toolkit: relational and logical operators. These are the tools that give your pseudocode the ability to make decisions, adding a whole new layer of sophistication to your "algodecsriptive" skills. Let's explore how these operators work and how they can make your pseudocode a pro at handling complex scenarios.

Relational and logical operators

You can also use these relational operators in your pseudocode:

a == b // a equal to b
a != b // a is not equal to b
a < b  // a is less than b
a <= b // a is less than or equal to b
a > b  // a is greater than b
a >= b // a is greater than or equal to b

All these operations return true or false.

In case of a complex condition, you can use logical operators. and returns true only if both conditions are true. or returns false only if both conditions are false. not just inverts a value. It works this way:

true and true == true
true and false == false
false and true == false
false and false == false

true or true == true
true or false == true
false or true == true
false or false == false

not true == false
not false == true

Conditional statements

Another commonly used type of construction is conditional statements. Conditional statements are a crucial part of pseudocode and help guide the flow of your algorithm. Let's take a look at an example:

a = 3

if a < 5 then:
    print(a)

Here, we create a variable a and initialize it with a number 3. Then, we check if a is less than 5 and if it is true, we print it to the screen. The syntax is clear: the if keyword is followed by a condition, and all following lines (that are indented deeper than the 'if' line) get executed only if the condition is true.

If you need to combine several conditions, you can use and, or, and not operators:

a = 10 
b = 20

if (a == 10 and b == 20) or not (a == 20 and b == 10) then:
    print(a)
    print(b)

To avoid ambiguity, we may need to wrap the conditions into parentheses, like in the example above.

Proper indentation is essential to clearly define the scope of conditions and branches. Here, notice how the print statements are indented to clearly show which lines of code are associated with the if branch. Indentation helps maintain readability and ensures that the program's logic is accurately represented.

More branches: else and elif

Now, you can put an else branch after the if condition. This branch gets executed if the condition is false. Below you can see an example with the if-else construction:

a = -3

if a > 0 then:
    print("positive")
else:
    print("negative or zero") // prints this

Additionally, you can use an elif branch. The statement elif is just an abbreviation for else if. The program checks this condition if the first one is false:

a = -5

if a > 0 then:            // false
    print("positive")
elif a == 0 then:         // checks this 
    print("zero")
else:
    print("negative")     // output

Here, we check whether a is greater than 0, then we check whether it equals 0 using the elif branch, and finally we execute the last else branch.

When dealing with complex conditions or nesting multiple conditions, indentation becomes even more crucial to maintain clarity. Indented blocks help distinguish between different branches and their respective code blocks, making it easier to understand the logic of your pseudocode.

Below, you can see the same code without the elif branch:

a = -5

if a > 0  then:               
    print("positive")
else:
    if a == 0  then:          
        print("zero")
    else:
        print("negative") 

Summary

In this topic, we've just unlocked a new level of pseudocode finesse by understanding relational and logical operators. These are the tricks that let your algorithm navigate through choices and complexities. It's not just about writing pseudocode like boring code; it's about describing smart algorithms that can tackle challenges. So, as you experiment with these operators, know that you're not just learning; you're gearing up to make your algorithms sharp and adaptable.

Ready to infuse some decision-making magic into your pseudocode? Let's get some practice!

20 learners liked this piece of theory. 2 didn't like it. What about you?
Report a typo