Questions tagged [variable-assignment]

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

4336 questions
3837
votes
12 answers

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

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…
700
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
  • 88,616
  • 93
  • 252
  • 370
508
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
  • 27,520
  • 68
  • 161
  • 264
407
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,651
  • 5
  • 19
  • 20
398
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
  • 7,397
  • 6
  • 34
  • 45
389
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
  • 315,713
  • 212
  • 479
  • 689
361
votes
12 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…
initialZero
  • 6,197
  • 9
  • 29
  • 38
345
votes
4 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
  • 3,459
  • 2
  • 15
  • 4
337
votes
12 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
  • 15,389
  • 20
  • 57
  • 65
286
votes
12 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
  • 4,286
  • 4
  • 26
  • 32
270
votes
16 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
  • 9,438
  • 8
  • 47
  • 66
250
votes
7 answers

What is the difference between i++ and ++i in C#?

I've seen them both being used in numerous pieces of C# code, and I'd like to know when to use i++ and when to use ++i? (i being a number variable like int, float, double, etc).
Dlaor
  • 2,900
  • 3
  • 17
  • 14
221
votes
8 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
  • 8,315
  • 4
  • 30
  • 23
212
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,498
  • 3
  • 12
  • 33
202
votes
10 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?
Suraj
  • 35,905
  • 47
  • 139
  • 250
1
2 3
99 100