Difference Between ( For... in ) and ( For... of ) Statements

What is the difference between ( for... in ) and ( for... of ) statements?

for in loops over enumerable property names of an object.

for of (new in ES6) does use an object-specific iterator and loops over the values generated by that.

In your example, the array iterator does yield all the values in the array (ignoring non-index properties).

What's the difference between these two statements in Javascript?

They do exactly the same thing in the example you've given, but there are some slight differences which explain why there are seemingly 2 ways to do the same thing.

Take this example...

var obj = {
foo: function() {
// do something
}
};

You can then extend that object like this...

obj.bar = function() {
// do something different
};

But if you did this...

obj = {
bar: function() {
// do something different
}
};

... you would lose the function foo. So one method is for explicitly declaring an object, complete with properties and functions, and the other method can be used for extending an existing object.

Just to be perfectly clear, this is incorrect syntax...

var obj = {
foo = function() { // it should be foo: function() {}
// do something
}
};

Difference between statement and function

A for loop is a not usually a function, it is a special kind of statement called a flow control structure.

A statement is a command. It does something. In most languages, statements do not return values. Example:

print "Hello World"

A function is a subroutine that can be called elsewhere in the program. Functions often (but not necessarily) return values. Example:

function(a) { return a * 2 }

A control structure, also known as a compound statement, is a statement that is used to direct the flow of execution. Examples:

if (condition) then { branch_1 } else { branch_2 }
for (i = 0; i < 10; i += 1) { ... }

Also worth noting is that an expression is a piece of code that evaluates to a value. Example:

2 + 2

All examples are in pseudocode, not tied to any particular language. Also note that these are not exclusive categories, they can overlap.

Difference between if and when statements in SQL

IF statement controls the flow and decides what has to be evaluated next, based on the condition, while CASE is a function which returns the desired value.

The CASE statement is more readable than IF statement when you compare a single expression against a range of unique values and more efficient as well.

Difference between three separate if statements and using 'or' in one if statement

The difference is that when you use separate if statement for your conditions you would be able to follow an special code block (commands) based on that condition, while if you put all of the conditions on one if statement you have to use same commands for all of them.

So it's all depends on your logic, and there is not difference in terms of performance. if you want to follow an specific command for all of the conditions you better to chain the conditions with boolean operators. Otherwise you should use separate if statements for each condition and follow the related commands for each condition.

Performance difference between two if statements?

With the given example, there won't be any difference in performance, even with if a == 0 and b == 0 and c == 0: it won't check b == 0 when the initial condition a == 0 itself is False. But considering the minimal lines of code & readability, if a == 0 and b == 0 and c == 0: would be the better option.

What is the difference between function and keyword/statements?

Python keywords are essentially special reserved words that have specific meanings and purposes and can’t be used for anything but those specific purposes. They come with the python standard library and you cannot change them in anyway. You can read more about keywords here If you run help("keywords") in your python interpreter, a bunch of keywords will be returned, and del is one of them.

Python statements refer to any line/lines of python code (which include stuff such as del motorcycles[0] and print(motorcycles[0])), and they do not have to meet any specific conditions to be classified as statements.

Python functions refer to a block of reusable code that does something and can returns a value each time they are run. They can either come with the standard library or be defined by users. You can refer to this for more information about functions.

Del in python is a keyword as it does something a normal python function cannot do: it interacts with the underlying memory and unbinds the variable from the memory. It is implemented with CPython.

Difference between statement and expressions

Rust is an expression-oriented language. This means that most constructs including control-flow constructs are expressions. An expression followed by a semicolon ; is a statement, whose effect is to evaluate the expression and discard its result. Therefore, the distinction between expression and statement is less important. However, some expressions diverge and that's a little weird if you're coming from other languages.

break counter * 2 is an expression. What is the type and value of this expression? It is a diverging expression. It has no value, and the type is a type ! that has no values. Imagine writing:

let foo = break counter * 2;

Ask what is the type of foo. One cannot have the effect of break, which is essentially a goto, while also returning a value and continuing the loop. The type of a break expression therefore is always a type without values. An uninhabited type, a type without any values, may seem odd but conceptually it's fine. It's the return type of functions that never return. It's the type of infinite loops, which can never evaluate to a value.

Yes, break counter * 2; is a statement which discards the value of the expression break counter * 2, but the value of the expression is not counter * 2.

What is the type of loop { ... }? By fiat, it is the type of the expressions within any break expressions in the loop. The value of the loop must come from one of the break expressions.

So, if you add some types:

fn main() {
// The type of counter is i32, because it is not
// suffixed with a different literal type, and no
// use below causes a different type to be inferred.
let mut counter: i32 = 0;

// The type of result is the type of the loop
// expression, which by definition is the type of
// the expressions passed to `break` within the
// loop. There is only one `break`, which is passed
// counter * 2, of type i32.
let result: i32 = loop {
counter += 1;

if counter == 10 {
// The type of this expression is !.
// Since a semicolon follows, the value
// is discarded, but the expression has no value.
break counter * 2;
}
};

println!("The result is {}", result);
}


Related Topics



Leave a reply



Submit