Questions tagged [variable-assignment]

A process of setting or re-setting the value stored in the storage location(s) denoted by a variable name.

3760 questions
3728
votes
11 answers

Why don't Java's +=, -=, *=, /= compound assignment operators require casting?

Until today, I thought that for example: i += j; Was just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Does it mean that in fact i += j; is a shortcut for…
588
votes
5 answers

Command not found error in Bash variable assignment

I have this script called test.sh: #!/bin/bash STR = "Hello World" echo $STR when I run sh test.sh I get this: test.sh: line 2: STR: command not found What am I doing wrong? I look at extremely basic/beginners bash scripting tutorials online and…
Jake Wilson
  • 78,902
  • 83
  • 230
  • 344
500
votes
26 answers

Why does this go into an infinite loop?

I have the following code: public class Tests { public static void main(String[] args) throws Exception { int x = 0; while(x<3) { x = x++; System.out.println(x); } } } We know he should have…
The Student
  • 25,055
  • 64
  • 149
  • 253
375
votes
9 answers

How can I index a MATLAB array returned by a function without first assigning it to a local variable?

For example, if I want to read the middle value from magic(5), I can do so like this: M = magic(5); value = M(3,3); to get value == 13. I'd like to be able to do something like one of these: value = magic(5)(3,3); value = (magic(5))(3,3); to…
Joe Kearney
  • 6,947
  • 5
  • 32
  • 42
373
votes
12 answers

JavaScript OR (||) variable assignment explanation

Given this snippet of JavaScript... var a; var b = null; var c = undefined; var d = 4; var e = 'five'; var f = a || b || c || d || e; alert(f); // 4 Can someone please explain to me what this technique is called (my best guess is in the title of…
chattsm
  • 4,281
  • 4
  • 17
  • 20
326
votes
14 answers

Is it possible only to declare a variable without assigning any value in Python?

Is it possible to declare a variable in Python, like so?: var so that it initialized to None? It seems like Python allows this, but as soon as you access it, it crashes. Is this possible? If not, why? EDIT: I want to do this for cases like…
Joan Venge
  • 269,545
  • 201
  • 440
  • 653
322
votes
10 answers

Assign output of a program to a variable using a MS batch file

I need to assign the output of a program to a variable using a MS batch file. So in GNU Bash shell I would use VAR=$(application arg0 arg1). I need a similar behavior in Windows using a batch file. Something like set VAR=application arg0 arg1.
initialZero
  • 5,667
  • 8
  • 26
  • 37
281
votes
3 answers

Set variable in jinja

I would like to know how can I set a variable with another variable in jinja. I will explain, I have got a submenu and I would like show which link is active. I tried this: {% set active_link = {{recordtype}} -%} where recordtype is a variable…
MyTux
  • 2,819
  • 2
  • 13
  • 4
272
votes
13 answers

Initialization of all elements of an array to one default value in C++?

C++ Notes: Array Initialization has a nice list over initialization of arrays. I have a int array[100] = {-1}; expecting it to be full with -1's but its not, only first value is and the rest are 0's mixed with random values. The code int array[100]…
Milan
  • 13,907
  • 20
  • 52
  • 64
240
votes
13 answers

How to assign from a function which returns more than one value?

Still trying to get into the R logic... what is the "best" way to unpack (on LHS) the results from a function returning multiple values? I can't do this apparently: R> functionReturningTwoValues <- function() { return(c(1, 2)) } R>…
mariotomo
  • 8,472
  • 7
  • 43
  • 63
232
votes
11 answers

What is the difference between shallow copy, deepcopy and normal assignment operation?

import copy a = "deepak" b = 1, 2, 3, 4 c = [1, 2, 3, 4] d = {1: 10, 2: 20, 3: 30} a1 = copy.copy(a) b1 = copy.copy(b) c1 = copy.copy(c) d1 = copy.copy(d) print("immutable - id(a)==id(a1)", id(a) == id(a1)) print("immutable - id(b)==id(b1)",…
deeshank
  • 3,451
  • 4
  • 22
  • 29
210
votes
14 answers

Why is x == (x = y) not the same as (x = y) == x?

Consider the following example: class Quirky { public static void main(String[] args) { int x = 1; int y = 3; System.out.println(x == (x = y)); // false x = 1; // reset System.out.println((x = y) == x);…
John McClane
  • 3,218
  • 2
  • 9
  • 27
198
votes
7 answers

Multiple left-hand assignment with JavaScript

var var1 = 1, var2 = 1, var3 = 1; This is equivalent to this: var var1 = var2 = var3 = 1; I'm fairly certain this is the order the variables are defined: var3, var2, var1, which would be equivalent to this: var var3 = 1, var2 = var3, var1…
David Calhoun
  • 6,835
  • 3
  • 25
  • 22
166
votes
9 answers

R: += (plus equals) and ++ (plus plus) equivalent from c++/c#/java, etc.?

Does R have a concept of += (plus equals) or ++ (plus plus) as c++/c#/others do?
SFun28
  • 32,209
  • 43
  • 123
  • 233
160
votes
7 answers

What does the keyword Set actually do in VBA?

Hopefully an easy question, but I'd quite like a technical answer to this! What's the difference between: i = 4 and Set i = 4 in VBA? I know that the latter will throw an error, but I don't fully understand why.
Jon Artus
  • 5,948
  • 11
  • 39
  • 41
1
2 3
99 100