There are two types of if
function codes:
if (condition1) {
function1
}
if (condition2) {
function2
}
function3
if (condition2) {
function1
} else {
if (condition3) {
function2
} else {
function3
}
}
I don't know what is the correct way to use the if
function code, I hope everyone can tell me.
Feel free to leave a comment if you need more information.
What type of if
function code should I use? I would appreciate any help. Thank you in advance!
CodePudding user response:
else
only runs when the if
is not true. What you need to use depends on what you want it to do.
In your first example, if both condition1
and condition2
are true then both function1
and function2
will run. function3
will run regardless of the conditions.
In your second example when condition2
and condition3
are true, only function1
will run because it will never go in the else
block. So function3
will only run when both condition2
and condition3
are false, and function2
will only run when condition2
is false and condition3
is true.