 Syntax for multiple level if then statement

Hi,

Not sure what I am doing wrong, can't seem to get the right syntax.

I have a first IF loop when that works, then it drops into a second and third IF loop.

But when the first IF is not valid, it drops into an ELSE condition and continues processing from there.

a = 200;
var status = "status";

if (a== 100){
b = 200;
if (b = 200);
c=300;
if (c = 300);
status = "first";

}else if (a != 100)  {
b = 200;
if (b = 200);
c=300;
if (c = 300);
status = "second";
}

return msg;

What did you expect to happen?

Trying to get the second and third if statements to work.

To process from the first section if a = 200.

If A is not 200 expect it to drop to the else then process from there.

This is not the code I am working with, I am just trying to get the syntax correct so I can integrate it into my code

But if you simplify your code -

if (a== 100){
do something
}
else if (a != 100)  {
do something else;
}

You can see why it's failing

Be careful to use == or === in your if statements and not = by itself. The former are used to compare values, the latter assigned the value.

There's a few problems with your code...

• As @knolleary points out, you're using = in some if statements, which is assignment, when you should be using == or ===.

• Some of your if statements end in a ;, which ends them. The statement on the next line is therefore not part of the if, and will always execute.

• You have indented some of the code as if it should be part of a block. Python works this way; Javascript does not. For Javascript you need to define a block using{...} as you have done for the if (a == 100) {... The test for b and c are therefore within the test for a, but c is not within b.

• The else if can just be simplified to else given if a is 100, the else condition is a is not 100.

• You also said
"But when the first IF is not valid, it drops into an ELSE condition and continues processing from there"
This is what is supposed to happen. Everything within the {...} after the first if is treated as one block. If the if fails, the else (if it exists) is executed.

Your code should look more like that below. Assuming that's what you want to achieve, because I'm guessing it's just made up for the purpose of posting as it does nothing useful.

if (a === 100) {
b = 200;
if (b === 200) {
c = 300;
if (c === 300)
status = "first";
}
} else {
b = 200;
if (b === 200) {
c = 300;
if (c === 300)
status = "second";
}
}
1 Like

My recommendation, particularly for beginners, is always to use {..} for if statements and blocks, even for single line blocks. It makes the code much less prone to the sort of errors you have in your code.

2 Likes
if (a === 100) {
b = 200;
if (b === 200) {
...

In other words, if a == 100 then b is always 200 and c is always 300.

Hi Guys,

Thank you all for helping, it turned out to be a mismatch of =, == and ===.

I also learnt more about writing JavaScript code along the way.

That part of my code is functional, just working on the other bugs, lol.

Merry X-mas.