variable-assignment

Which costs more while looping; assignment or an if-statement?

吃可爱长大的小学妹 提交于 2019-11-30 06:37:58
Consider the following 2 scenarios: boolean b = false; int i = 0; while(i++ < 5) { b = true; } OR boolean b = false; int i = 0; while(i++ < 5) { if(!b) { b = true; } } Which is more "costly" to do? If the answer depends on used language/compiler, please provide. My main programming language is Java. Please do not ask questions like why would I want to do either.. They're just barebone examples that point out the relevant: should a variable be set the same value in a loop over and over again or should it be tested on every loop that it holds a value needed to change? Please do not forget the

Python: Assign Value if None Exists

我的未来我决定 提交于 2019-11-30 05:36:21
I am a RoR programmer new to Python. I am trying to find the syntax that will allow me to set a variable to a specific value only if it wasn't previously assigned. Basically I want: # only if var1 has not been previously assigned var1 = 4 This is a very different style of programming, but I always try to rewrite things that looked like bar = None if foo(): bar = "Baz" if bar is None: bar = "Quux" into just: if foo(): bar = "Baz" else: bar = "Quux" That is to say, I try hard to avoid a situation where some code paths define variables but others don't. In my code, there is never a path which

ES6 Structuring Assignment?

六眼飞鱼酱① 提交于 2019-11-30 05:00:25
The new destructuring assignment features of ES6 are fairly well known now ( live copy on Babel's REPL); in the case of variables that already exist: let a, b; // Existing variables let o = {a: "a", b: "b"}; // An object to get values from // ... ({a, b} = o); // Set them to the props from `o` console.log(a); // "a" console.log(b); // "b" Is there a simple converse in ES6? Setting properties on an existing object based on variables with the same name? (Other than the obvious o.a = a; o.b = b; ) Note I'm not talking about when creating an object, we could do that with the wonderful new object

Java Program using 4D array

萝らか妹 提交于 2019-11-30 02:57:37
问题 I'm a first year computer engineering student and I'm quite new here. I have been learning Java for the past three and a half months, and C++ for six months before that. My knowledge of Java is limited to defining and using own methods, absolute basics of object-oriented programming like use of static data members and member visibility. This afternoon, my computer programming prof taught us about multi-dimensional arrays in Java. About multi-dimensional arrays being simply arrays of arrays

Why does the goatse operator work?

只谈情不闲聊 提交于 2019-11-30 02:56:58
问题 The difference between arrays and lists and between list and scalar context have been discussed in the Perl community quite a bit this last year (and every year, really). I have read over articles from chromatic and friedo, as well as this recommended monks node. I'm trying now to understand the goatse operator, documented in perlsecret. Here is some code I used to study it: # right side gets scalar context, so commas return rightmost item $string = qw(stuff junk things); say $string; #

Javascript. Assign array values to multiple variables? [duplicate]

二次信任 提交于 2019-11-30 02:32:58
This question already has an answer here: Multiple assignment in javascript? What does [a,b,c] = [1, 2, 3]; mean? 4 answers var a,b,c; var arr = [1,2,3]; [a,b,c] = arr; this code works perfectly in Firefox resulting a=1, b=2 and c=3, but it doesn't work in Chrome. Is it a Chrome bug or it is not valid javascript code? (I failed to find it in javascript references) How can I modify this code to make it suitable for Chrome, with minimum damage to it? (I don't really like to write a = arr[0]; b = arr[1]... or the same with arr.shift() all the time) P.S. this is just an example code, in real code

Python Multiple Assignment Statements In One Line

我的梦境 提交于 2019-11-30 01:27:37
(Don't worry, this isn't another question about unpacking tuples.) In python, a statement like foo = bar = baz = 5 assigns the variables foo, bar, and baz to 5. It assigns these variables from left to right, as can be proved by nastier examples like >>> foo[0] = foo = [0] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'foo' is not defined >>> foo = foo[0] = [0] >>> foo [[...]] >>> foo[0] [[...]] >>> foo is foo[0] True But the python language reference states that assignment statements have the form (target_list "=")+ (expression_list | yield_expression)

Google Go Lang Assignment Order

人盡茶涼 提交于 2019-11-30 00:10:31
问题 Let's look at the following Go code: package main import "fmt" type Vertex struct { Lat, Long float64 } var m map[string]Vertex func main() { m = make(map[string]Vertex) m["Bell Labs"] = Vertex{ 40.68433, 74.39967, } m["test"] = Vertex{ 12.0, 100, } fmt.Println(m["Bell Labs"]) fmt.Println(m) } It outputs this: {40.68433 74.39967} map[Bell Labs:{40.68433 74.39967} test:{12 100}] However, if I Change one minor part of the test vertex declaration, by moving the right " } " 4 spaces, like so: m[

Expression must be a modifiable L-value

吃可爱长大的小学妹 提交于 2019-11-29 23:59:03
I have here char text[60]; Then I do in an if : if(number == 2) text = "awesome"; else text = "you fail"; and it always said expression must be a modifiable L-value. You cannot change the value of text since it is an array, not a pointer. Either declare it as char pointer (in this case it's better to declare it as const char* ): const char *text; if(number == 2) text = "awesome"; else text = "you fail"; Or use strcpy: char text[60]; if(number == 2) strcpy(text, "awesome"); else strcpy(text, "you fail"); 来源: https://stackoverflow.com/questions/6008733/expression-must-be-a-modifiable-l-value

how to re-assign variable in python without changing id?

一曲冷凌霜 提交于 2019-11-29 22:58:42
问题 when assign a variable to another they point to a same object, So, how to value change for one of them but variables still point same object! a = 10 b = a a -= 1 print(b) #expect to print 9 but it print 10 how to re-assign variable in python without changing id? 回答1: I'm not sure whether you're confused about variables in Python, or about immutable values. So I'm going to explain both, and half the answer will probably seem like "no duh, I already knew that", but the other half should be