Does It Matter If a Conditional Statement Comes Before or After the Expression

Does it matter if a conditional statement comes before or after the expression?

It's syntactic sugar... allowing us to write code in a way that's easier to read.

http://rubylearning.com/satishtalim/ruby_syntactic_sugar.html

Note: for @Phrogz, the following are NOT the same!
Please make sure that you are not trying to assign a value to variable instead of comparing a variable to a value! Also, as Phrogz mentions, the order of variable assignment makes a big difference... see @Phrogz answer for mor details!

if 1 = 1 then do_something(with params) end
if 1 == 1 then do_something(with params) end

Two conditions in one if statement does the second matter if the first is false?

It is common for languages (Java and Python are among them) to evaluate the first argument of a logical AND and finish evaluation of the statement if the first argument is false. This is because:

From The Order of Evaluation of Logic Operators,

When Java evaluates the expression d = b && c;, it first checks whether b is true. Here b is false, so b && c must be false regardless of whether c is or is not true, so Java doesn't bother checking the value of c.

This is known as short-circuit evaluation, and is also referred to in the Java docs.

It is common to see list.count > 0 && list[0] == "Something" to check a list element, if it exists.


It is also worth mentioning that if (list.length>2 && list[3] == 2) is not equal to the second case

if (list.length>2){
if (list[3] == 2){
...
}
}

if there is an else afterwards. The else will apply only to the if statement to which it is attached.

To demonstrate this gotcha:

if (x.kind.equals("Human")) {
if (x.name.equals("Jordan")) {
System.out.println("Hello Jordan!");
}
} else {
System.out.println("You are not a human!");
}

will work as expected, but

if (x.kind.equals("Human") && x.name.equals("Jordan")) {
System.out.println("Hello Jordan!");
} else {
System.out.println("You are not a human!");
}

will also tell any Human who isn't Jordan they are not human.

Conditional statement true in both parts of if-else-if ladder

No, they won't both execute. It goes in order of how you've written them, and logically this makes sense; Even though the second one reads 'else if', you can still think of it as 'else'.

Consider a typical if/else block:

if(true){
// Blah
} else{
// Blah blah
}

If your first statement is true, you don't even bother looking at what needs to be done in the else case, because it is irrelevant. Similarly, if you have 'if/elseif', you won't waste your time looking at succeeding blocks because the first one is true.

A real world example could be assigning grades. You might try something like this:

if(grade > 90){
// Student gets A
} else if(grade > 80){
// Student gets B
} else if(grade > 70){
// Student gets c
}

If the student got a 99%, all of these conditions are true. However, you're not going to assign the student A, B and C.

That's why order is important. If I executed this code, and put the B block before the A block, you would assign that same student with a B instead of an A, because the A block wouldn't be executed.

&& (AND) and || (OR) in IF statements

No, it will not be evaluated. And this is very useful. For example, if you need to test whether a String is not null or empty, you can write:

if (str != null && !str.isEmpty()) {
doSomethingWith(str.charAt(0));
}

or, the other way around

if (str == null || str.isEmpty()) {
complainAboutUnusableString();
} else {
doSomethingWith(str.charAt(0));
}

If we didn't have 'short-circuits' in Java, we'd receive a lot of NullPointerExceptions in the above lines of code.

Will an IF statement stop evaluating if it fails the first condition?

It will stop evaluating because you're using the double ampersand && operator. This is called short-circuiting.

If you changed it to a single ampersand:

if(myList.Count > 0 & myString.Equals("value"))

it would evaluate both.

Execution order of conditions in C# If statement

The && and || operators short-circuit. That is:

1) If && evaluates its first operand as false, it does not evaluate its second operand.

2) If || evaluates its first operand as true, it does not evaluate its second operand.

This lets you do null check && do something with object, as if it is not null the second operand is not evaluated.

What is the advantage of commas in a conditional statement?

Changing your example slightly, suppose it was this

if ( a = f(5), b = f(6), ... , thisMustBeTrue(a, b) )

(note the = instead of ==). In this case the commas guarantee a left to right order of evaluation. In constrast, with this

if ( thisMustBeTrue(f(5), f(6)) )

you don't know if f(5) is called before or after f(6).

More formally, commas allow you to write a sequence of expressions (a,b,c) in the same way you can use ; to write a sequence of statements a; b; c;.
And just as a ; creates a sequence point (end of full expression) so too does a comma. Only sequence points govern the order of evaluation, see this post.

But of course, in this case, you'd actually write this

a = f(5);
b = f(6);
if ( thisMustBeTrue(a, b) )

So when is a comma separated sequence of expressions preferrable to a ; separated sequence of statements? Almost never I would say. Perhaps in a macro when you want the right-hand side replacement to be a single expression.

How to use OR condition in a JavaScript IF statement?

Simply use the logical "OR" operator, that is ||.

if (A || B)


Related Topics



Leave a reply



Submit