Article image Relational Operators: Less than

6.5. Relational Operators: Less than

Page 27 | Listen in audio

Relational operators are fundamental tools in programming logic and are used to compare two values, returning a boolean result (true or false). In this chapter, we'll focus on the 'less than' (<) relational operator, which is used to determine whether one value is less than another.

For better understanding, let's consider two values, A and B. The expression "A < B" will return true if A is less than B, and false otherwise. For example, if A is 5 and B is 10, then "5 < 10" will return true since 5 is indeed less than 10.

Relational operators are very useful in many situations in programming. They are often used in conditional statements (such as if and else), loops, and other flow control structures. For example, you can use the 'less than' operator in a loop to run a block of code until a certain condition is met.

See the following example in pseudocode:

For i = 0 through i < 10 do:
 Write "The value of i is: " + i
End To

In this example, the loop will run as long as the value of i is less than 10. At each iteration, the value of i is incremented by 1, and the message "The value of i is: " followed by the current value of i is printed. When i reaches 10, the condition "i < 10" becomes false, and the loop terminates.

Relational operators can also be used in combination with other operators, such as the logical 'and' (&&) and 'or' (||) operators, to create more complex conditions. For example, the expression "A < B && B < C" will return true if A is less than B and B is less than C.

It is important to note that the 'less than' operator can only be used to compare data types that are sortable, such as numbers and strings. If you try to compare data types that are not sortable, such as objects or arrays, the result will be undefined.

Also, when comparing strings, the 'less than' operator will compare the ASCII values ​​of the characters. For example, the expression "a" < "b" will return true because the ASCII value of 'a' is less than the ASCII value of 'b'. However, the expression "A" < "a" will also return true as the ASCII value of 'A' is less than the ASCII value of 'a'.

In summary, the 'less than' relational operator is a valuable tool in logic programming. It allows you to compare two values ​​and make decisions based on that comparison. Combined with other tools such as logical operators and flow control structures, you can create complex and powerful programs.

In the next chapter, we'll explore the 'greater than' relational operator (>), which is used to determine whether one value is greater than another. Stay tuned!

Now answer the exercise about the content:

What does the 'less than' (<) relational operator do in programming logic?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Relational Operators: Less than or equal to

Next page of the Free Ebook:

28Relational Operators: Less than or equal to

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text