6.3. Relational Operators: Greater than

Página 25

Relational operators are a fundamental part of programming logic. They allow programmers to make comparisons between different values ​​or variables. In this chapter, we will focus specifically on the "greater than" operator, which is represented by the ">" symbol in many programming languages.

In simple terms, the "greater than" operator is used to compare two values. If the value on the left of the operator is greater than the value on the right, the expression returns true. Otherwise, it will return false. For example, the expression "5 > 3" will return true, because 5 is greater than 3. On the other hand, the expression "2 > 4" will return false, because 2 is not greater than 4.

This operator is often used in flow control structures such as loops and conditionals. For example, you can use the "greater than" operator in a "while" loop to continue executing the loop as long as a variable is greater than a given value.

Let's consider a practical example. Suppose we are writing a program that asks the user to enter a series of numbers. The program should keep prompting for numbers until the user enters a number greater than 100. This could be implemented with a "while" loop and the "greater than" operator, like this:


var number;
of {
  number = prompt("Please enter a number");
} while (number <= 100);

In this example, the program will continue executing the loop as long as the number entered by the user is less than or equal to 100. As soon as the user enters a number greater than 100, the condition "number <= 100" will return false and the loop will be closed.

Another common application of the "greater than" operator is in conditional statements. For example, you might want to run a block of code only if a variable is greater than a certain value. This can be done with an "if" statement, as in the following example:


var score = 85;
if (score > 70) {
  console.log("You passed the test!");
}

In this example, the message "You passed the test!" will be displayed only if the variable "score" is greater than 70. If "score" is 70 or less, the condition "score > 70" will return false and the message will not be displayed.

In summary, the "greater than" operator is a valuable tool in programming logic that allows programmers to make comparisons between values. It is often used in control-flow structures such as loops and conditionals to control the flow of execution of a program based on value comparisons.

It is important to note that although this chapter has focused on the "greater than" operator, there are many other relational operators in programming, including "less than", "equal to", "not equal to", "greater than or equal to " and "less than or equal to". Each of these operators works in a similar way to the "greater than" operator, but performs a slightly different comparison. Learning to use all these operators is a fundamental part of programming logic.

Now answer the exercise about the content:

What is the function of the "greater than" operator in programming?

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

You missed! Try again.

Next page of the Free Ebook:

266.4. Relational Operators: Greater than or equal to

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