Logical operators indicating a range between numbers

Asked

Viewed 45 times

-1

is a simple question: how to specify a range of numbers using operators logicos? for example: if the number typed is between 1 and 10,notify that is too little? this syntax is valid?

if(valor>1&&9){alert("muito pouco")}
if else{valor>10&&15 alert("bom!")}
if else{ valor>15&&20 alert("muito bom!")}
  • 2

    Is that Javascript? Anyway, if it’s to test if the number is between 1 and 10, it would be if (valor >= 1 && valor <= 10) { alert("muito pouco") } - already to have several conditions chained, is else if and not if else, something like this: https://ideone.com/tl0gye - not to sound boring or anything, but as it seems that you are "getting" the basic syntax of the language, I suggest you go back to the basics and start from here - seriously, it’s not irony or anything, it’s a genuine attempt to help...

  • Yes, I’m picking up because I started a little while ago ahaha.. Thanks for your help! ^^

1 answer

1

Answering your question, not the syntax you used is not valid Binary Logic Operators are normally used with boolean values and return a boolean.

In your first condition for example:

if(valor>1&&9) {...

If you want to compare valor is between 1 and 10 should be made so:

if(valor > 1 && valor < 10) {...

For the logical conjunction operator && will:

  • Testing the first sentence valor > 1...
    • ...if the value of the judgment is true he goes on to the next test...
    • ...if the value of the judgment is false it stops the test and returns false.
  • Test the sentence valor < 10..
    • ...if the value of the judgment is true returns true.
    • ...if the value of the judgment is false returns false.

Or simply the conjunction operator && tests two boolean sentences and returns true only if both sentences are true.

An addendum with three ways to make these comparisons:

With the statement if...

do {
  var n = parseInt(window.prompt("Digite um número:"));
} while (isNaN(n));

if (n < 0){
  alert("Negativos não valem!");
} else if (n == 0) {
  alert("Zero!");
} else if (n < 10){
  alert("Muito pouco!");
} else if (n < 15){
  alert("Bom!");
} else if (n < 20){
  alert("Muito bom!");
} else {
  alert("Passou...");
}

With the statement switch/case

do {
  var n = parseInt(window.prompt("Digite um número:"));
} while (isNaN(n));

switch (true) {
  case n < 0:
    alert("Negativos não valem!")
    break;
  case n == 0:
    alert("Zero!")
    break;
  case n < 10:
    alert("Muito pouco!")
    break;
  case n < 15:
    alert("Bom!")
    break;
  case n < 20:
    alert("Muito bom!")
    break;
  default:
    alert("Passou......")
}

With the conditional ternary operator

do {
  var n = parseInt(window.prompt("Digite um número:"));
} while (isNaN(n));

alert(
  (n < 0)  ? "Negativos não valem!" :
  (n == 0) ? "Zero" :
  (n < 10) ? "Muito pouco!" :
  (n < 15) ? "Bom!" :
  (n < 20) ? "Muito bom!" :
  "Passou......"
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.