-1

I get the nested if loops (same as using && operator), but how does this code here know which conditions to execute with no conditions and just back to back else statements? One of them is within the nested if statements. I can tell that's obviously why this works the way it does, I just don't get how. Also, I know how to write this in several more readable ways testing multiple conditions. Please just explain what is happening with this code here. How does it know to output "You are too old" or "You are too young?"

var age = prompt("Please enter Your age here :");
var min_age=18;
var max_age=40;

if(age>=min_age){
 if(age<=max_age){
   console.log("You meet the requirements for this competition");
 }else{
  console.log("You are too old");
 }
}else{
 console.log("You are too young");
}
dannymac
  • 517
  • 5
  • 14
  • 3
    *How does this code know which else statement to execute?* The curly braces. – Frédéric Hamidi Apr 12 '16 at 09:57
  • If age is greater or equal than min_age > (if age is less or equal than max_age > You meet the requirements ; else > You are too old) else > You are too young. Just read the code. – Marcos Pérez Gude Apr 12 '16 at 09:57
  • Put in other words: The wrapping `if else` is executed first. If the age is _under_ 18, it will just execute the `else` and say you're too young. Otherwise, it will go on the the next `if else`, and forget about the previous one. If the age is _under_ 40, it will say that you meet the requirements, and do nothing more. Otherwise, it will execute the `else` bound to the current `if`, and say you're too old. But really, you should allow everyone to participate. No discrimination. – blex Apr 12 '16 at 09:59
  • @blex Ahhh, thank you. I knew it was something dumb tripping me up. Just never seen it written like this before. – dannymac Apr 12 '16 at 10:01

4 Answers4

0

Firstly, let's indent your code.

var age = prompt("Please enter Your age here :");
var min_age = 18;
var max_age = 40;

if (age >= min_age)
{
    if (age <= max_age)
    {
        console.log("You meet the requirements for this competition");
    }
    else
    {
        console.log("You are too old");
    }
}
else
{
    console.log("You are too young");
}

Starting off..

var age = prompt("Please enter Your age here :");

Let's say you enter 21 in the prompt box, so age=21

We initialize

var min_age = 18;
var max_age = 40;

Now let's look at the first if condition.

 if (age >= min_age)

If you substitute the values,this translates to

if (21 >= 18)

This is true,therefore we go inside the if block and not to the else. The next line is.

 if (age <= max_age)

This translates to

 if (21 <= 40)

Considering this is also true, we print You meet the requirements for this competition.

The most important take-away from this is, indent your code, and the rest becomes pretty simple.

Satej S
  • 2,113
  • 1
  • 16
  • 22
  • 1
    Yeah, it was copy pasted off of another tutorial. I wouldn't write anything like this lol. Thanks. – dannymac Apr 12 '16 at 10:02
0

The brackets {} set the limit.

Try to think in pseudocode, look beyond the characters and think about what is happening.

Reading in order:

If you are old enough
  If your are not too old 
    'You meet the requirements for this competition'
  OTHERWISE
    'You are too old'
  END
OTHERWISE
  'You are too young'
END

Note how indentation can help see the limits of the conditions. Each indented part can be separated.

Reuben Mallaby
  • 5,740
  • 4
  • 47
  • 45
  • Yeah, had just never seen it written like this before, was throwing me off. Should have done the pseudo approach. Thanks. – dannymac Apr 12 '16 at 10:03
0

The if-then-else ambiguity is known for a long time. All languages have solved it by defining that an else will match the first perceding if. So:

if (a)
    if (b)
        x = 1;
else
    x = 2;

resolves to:

if (a) {
    if (b) {
        x = 1;
    }
    else {
        x = 2;
    }
}

EDIT by Nisar's reuest:

The if statement is defined as:

if (<condition>) <statement> [else <statement>]

This means that a <statement> in the above may also be an if statement. So, for example:

if (<condition>) if (<condition>) [else <statement>] [else <statement>]

As each else part is optional, the compiler has no way of knowing when it sees an else part to which if it belongs. To solve that the language defines that an else always matches the first preceding if.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

There are just 3 Options

  • too young
  • correct age
  • too old

First Check - is the person old enough?

if(age>=min_age)

Second check - is the person too old?

if(age<=max_age)

the only possible option left after this if statment is FALSE :

  • too old
a1xon
  • 85
  • 1
  • 1
  • 6