Free Ebook cover Complete Logic Programming Course for Beginners

Complete Logic Programming Course for Beginners

4

(3)

83 pages

Relational Operators: Less than

Capítulo 27

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

The 'less than' (<) relational operator is used in programming to compare two values. It returns true if the first value is less than the second value, otherwise it returns false. This comparison is fundamental for conditional logic, often seen in loops or conditional statements.

Next chapter

Relational Operators: Less than or equal to

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.