Detail about the function of such operators: +=,*=, -=, /=
I was studying Java and I saw this example in a book:
int x=2, n=3;
int result=1;
for (int i=1; i<=n; i++)
result*=x;
System.out.println(result);
This calculates 2 to the power of 3. What I do not understand is how it does it in detail. I guess that for (int i=1; i<=n; i++)
decides how many times int x=2
is going to be repeated, in this case, "3", then it gets int result=1
and multiply it by int x=2
, and multiply int x=2
by itself three times. Maybe I am correct, but I do not understand how it happens, and why, as it is not possible to see the process step by step in the program (is it?). Could any one help me and tell what makes int x=2
repeat three times here, and how? And what exactly these expressions *=
and result*=x;
do? I saw everywhere that a*=b
is the same as a=a*b
, but it does not explain much how it works and in which cases I should use it, and I would really like to understand it, to know how to use it in future, in the case of I need to create a program and it may help me.
java
add a comment |
I was studying Java and I saw this example in a book:
int x=2, n=3;
int result=1;
for (int i=1; i<=n; i++)
result*=x;
System.out.println(result);
This calculates 2 to the power of 3. What I do not understand is how it does it in detail. I guess that for (int i=1; i<=n; i++)
decides how many times int x=2
is going to be repeated, in this case, "3", then it gets int result=1
and multiply it by int x=2
, and multiply int x=2
by itself three times. Maybe I am correct, but I do not understand how it happens, and why, as it is not possible to see the process step by step in the program (is it?). Could any one help me and tell what makes int x=2
repeat three times here, and how? And what exactly these expressions *=
and result*=x;
do? I saw everywhere that a*=b
is the same as a=a*b
, but it does not explain much how it works and in which cases I should use it, and I would really like to understand it, to know how to use it in future, in the case of I need to create a program and it may help me.
java
It's simply a short form forresult = result * x;
(or + x, - x, or / x).
– Elliott Frisch
Nov 21 '18 at 23:52
1
I saw everywhere thata*=b
is the same asa=a*b
, but it does not explain much how it works... what more do you want to know? and in which cases I should use it... use it wherever you can; it's cleaner.
– shmosel
Nov 21 '18 at 23:56
Here's a case where you can't use*=
:int result; result*=2;
because result needs to have a value in order to be able to do*=
. In my opiniona = a * 2
is just as fine asa *= 2
. In fact, the second version takes more "brain gymnastics". And programming, like any text, is communication, as much with the computer as yourself and your colleagues. So clarity is always good.
– Erk
Nov 22 '18 at 0:32
@Erk Both versions are equally invalid if it's not initialized. As for which version is more readable, that's highly subjective and ultimately depends on convention. IMO, the shorter way is more expressive and more conventional among experienced developers.
– shmosel
Nov 25 '18 at 7:50
@shmosel: I'm answering two different questions. And now I'm answering another question... ;o) And in my experience, most of the very experienced programmers tend to "move elsewhere" so you're going to work at best with intermediate programmers... but then, IRL you'll have to deal with both forms...
– Erk
Nov 25 '18 at 9:59
add a comment |
I was studying Java and I saw this example in a book:
int x=2, n=3;
int result=1;
for (int i=1; i<=n; i++)
result*=x;
System.out.println(result);
This calculates 2 to the power of 3. What I do not understand is how it does it in detail. I guess that for (int i=1; i<=n; i++)
decides how many times int x=2
is going to be repeated, in this case, "3", then it gets int result=1
and multiply it by int x=2
, and multiply int x=2
by itself three times. Maybe I am correct, but I do not understand how it happens, and why, as it is not possible to see the process step by step in the program (is it?). Could any one help me and tell what makes int x=2
repeat three times here, and how? And what exactly these expressions *=
and result*=x;
do? I saw everywhere that a*=b
is the same as a=a*b
, but it does not explain much how it works and in which cases I should use it, and I would really like to understand it, to know how to use it in future, in the case of I need to create a program and it may help me.
java
I was studying Java and I saw this example in a book:
int x=2, n=3;
int result=1;
for (int i=1; i<=n; i++)
result*=x;
System.out.println(result);
This calculates 2 to the power of 3. What I do not understand is how it does it in detail. I guess that for (int i=1; i<=n; i++)
decides how many times int x=2
is going to be repeated, in this case, "3", then it gets int result=1
and multiply it by int x=2
, and multiply int x=2
by itself three times. Maybe I am correct, but I do not understand how it happens, and why, as it is not possible to see the process step by step in the program (is it?). Could any one help me and tell what makes int x=2
repeat three times here, and how? And what exactly these expressions *=
and result*=x;
do? I saw everywhere that a*=b
is the same as a=a*b
, but it does not explain much how it works and in which cases I should use it, and I would really like to understand it, to know how to use it in future, in the case of I need to create a program and it may help me.
java
java
edited Nov 21 '18 at 23:55
shmosel
35.8k43891
35.8k43891
asked Nov 21 '18 at 23:47
IHSV555IHSV555
154
154
It's simply a short form forresult = result * x;
(or + x, - x, or / x).
– Elliott Frisch
Nov 21 '18 at 23:52
1
I saw everywhere thata*=b
is the same asa=a*b
, but it does not explain much how it works... what more do you want to know? and in which cases I should use it... use it wherever you can; it's cleaner.
– shmosel
Nov 21 '18 at 23:56
Here's a case where you can't use*=
:int result; result*=2;
because result needs to have a value in order to be able to do*=
. In my opiniona = a * 2
is just as fine asa *= 2
. In fact, the second version takes more "brain gymnastics". And programming, like any text, is communication, as much with the computer as yourself and your colleagues. So clarity is always good.
– Erk
Nov 22 '18 at 0:32
@Erk Both versions are equally invalid if it's not initialized. As for which version is more readable, that's highly subjective and ultimately depends on convention. IMO, the shorter way is more expressive and more conventional among experienced developers.
– shmosel
Nov 25 '18 at 7:50
@shmosel: I'm answering two different questions. And now I'm answering another question... ;o) And in my experience, most of the very experienced programmers tend to "move elsewhere" so you're going to work at best with intermediate programmers... but then, IRL you'll have to deal with both forms...
– Erk
Nov 25 '18 at 9:59
add a comment |
It's simply a short form forresult = result * x;
(or + x, - x, or / x).
– Elliott Frisch
Nov 21 '18 at 23:52
1
I saw everywhere thata*=b
is the same asa=a*b
, but it does not explain much how it works... what more do you want to know? and in which cases I should use it... use it wherever you can; it's cleaner.
– shmosel
Nov 21 '18 at 23:56
Here's a case where you can't use*=
:int result; result*=2;
because result needs to have a value in order to be able to do*=
. In my opiniona = a * 2
is just as fine asa *= 2
. In fact, the second version takes more "brain gymnastics". And programming, like any text, is communication, as much with the computer as yourself and your colleagues. So clarity is always good.
– Erk
Nov 22 '18 at 0:32
@Erk Both versions are equally invalid if it's not initialized. As for which version is more readable, that's highly subjective and ultimately depends on convention. IMO, the shorter way is more expressive and more conventional among experienced developers.
– shmosel
Nov 25 '18 at 7:50
@shmosel: I'm answering two different questions. And now I'm answering another question... ;o) And in my experience, most of the very experienced programmers tend to "move elsewhere" so you're going to work at best with intermediate programmers... but then, IRL you'll have to deal with both forms...
– Erk
Nov 25 '18 at 9:59
It's simply a short form for
result = result * x;
(or + x, - x, or / x).– Elliott Frisch
Nov 21 '18 at 23:52
It's simply a short form for
result = result * x;
(or + x, - x, or / x).– Elliott Frisch
Nov 21 '18 at 23:52
1
1
I saw everywhere that
a*=b
is the same as a=a*b
, but it does not explain much how it works... what more do you want to know? and in which cases I should use it... use it wherever you can; it's cleaner.– shmosel
Nov 21 '18 at 23:56
I saw everywhere that
a*=b
is the same as a=a*b
, but it does not explain much how it works... what more do you want to know? and in which cases I should use it... use it wherever you can; it's cleaner.– shmosel
Nov 21 '18 at 23:56
Here's a case where you can't use
*=
: int result; result*=2;
because result needs to have a value in order to be able to do *=
. In my opinion a = a * 2
is just as fine as a *= 2
. In fact, the second version takes more "brain gymnastics". And programming, like any text, is communication, as much with the computer as yourself and your colleagues. So clarity is always good.– Erk
Nov 22 '18 at 0:32
Here's a case where you can't use
*=
: int result; result*=2;
because result needs to have a value in order to be able to do *=
. In my opinion a = a * 2
is just as fine as a *= 2
. In fact, the second version takes more "brain gymnastics". And programming, like any text, is communication, as much with the computer as yourself and your colleagues. So clarity is always good.– Erk
Nov 22 '18 at 0:32
@Erk Both versions are equally invalid if it's not initialized. As for which version is more readable, that's highly subjective and ultimately depends on convention. IMO, the shorter way is more expressive and more conventional among experienced developers.
– shmosel
Nov 25 '18 at 7:50
@Erk Both versions are equally invalid if it's not initialized. As for which version is more readable, that's highly subjective and ultimately depends on convention. IMO, the shorter way is more expressive and more conventional among experienced developers.
– shmosel
Nov 25 '18 at 7:50
@shmosel: I'm answering two different questions. And now I'm answering another question... ;o) And in my experience, most of the very experienced programmers tend to "move elsewhere" so you're going to work at best with intermediate programmers... but then, IRL you'll have to deal with both forms...
– Erk
Nov 25 '18 at 9:59
@shmosel: I'm answering two different questions. And now I'm answering another question... ;o) And in my experience, most of the very experienced programmers tend to "move elsewhere" so you're going to work at best with intermediate programmers... but then, IRL you'll have to deal with both forms...
– Erk
Nov 25 '18 at 9:59
add a comment |
2 Answers
2
active
oldest
votes
In this loop:
for (int i=1; i<=n; i++)
result*=x;
i
is not used, and we know that n=3
, so we could rewrite the code as:
int x = 2
int result = 1;
result *= x; // result is 2
result *= x; // result is 4
result *= x; // result is 8
System.out.println(result);
Regarding a*=b
being the same as a=a*b
, it's not exactly the same, but close enough. a*=b
is the same as a=a*(typeOfA)b
where (typeOfA)
is a cast to the type of a
, eg a=a*(int)b
. This rarely causes a different result because usually the types of a
and b
are the same, but may have an effect due to the java's automatic widening/casting when types of a
and b
differ.
add a comment |
Let's take it step by step:
int x=2, n=3;
int result=1;
These two lines could be simplified at most into:
int x;
int n;
int result;
x = 2;
n = 3;
result = 1;
The int
constructs (e.g. int x
) define a variable of type int (e.g. x
). This means x
can hold an integer number.
Then the x = 2
construct sets the value of x
to 2
.
Doing int x=2
is a short form for that.
You're able to define and set more than one variable using the int x=2, n=3
construct as a shorthand.
The for-loop, here with curly braces to mark the beginning and end of the block (the part of code repeating in the for-loop):
for (int i=1; i<=n; i++) {
result*=x;
}
Using no curly braces means repeat the next ONE instruction.
As you pointed out yourself, the for clause loops three times.
The loop consists of the following segments:
for ([variable instantiations]; [condition]; [loop instruction])
The variable instantiations
can (but does not have to) be used for setting up variables, usually a single variable to use as the index of the loop. In your case a new instantiation of a variable i
with the value 1
.
The loop condition
is used to test if the loop should continue. As long as this statement is true, the loop will. In your case a test is made of i
against n
. As long as i
is less than or equal to (<=
) n
the loop continues.
The final loop instruction
is a bit of code that will be performed once for each loop. In your case it will increment (++
) i
with one. (i++
is equivalent to i = i + 1
).
The instruction in the for-loop, result *= x
means that result
will be multiplied by x
(2) and the result will be stored back in result
(i.e. result = result * 2
)
Finally, the result will be printed to the console:
System.out.println(result);
If I'm not completely mistaken you should get: 8
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422026%2fdetail-about-the-function-of-such-operators%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In this loop:
for (int i=1; i<=n; i++)
result*=x;
i
is not used, and we know that n=3
, so we could rewrite the code as:
int x = 2
int result = 1;
result *= x; // result is 2
result *= x; // result is 4
result *= x; // result is 8
System.out.println(result);
Regarding a*=b
being the same as a=a*b
, it's not exactly the same, but close enough. a*=b
is the same as a=a*(typeOfA)b
where (typeOfA)
is a cast to the type of a
, eg a=a*(int)b
. This rarely causes a different result because usually the types of a
and b
are the same, but may have an effect due to the java's automatic widening/casting when types of a
and b
differ.
add a comment |
In this loop:
for (int i=1; i<=n; i++)
result*=x;
i
is not used, and we know that n=3
, so we could rewrite the code as:
int x = 2
int result = 1;
result *= x; // result is 2
result *= x; // result is 4
result *= x; // result is 8
System.out.println(result);
Regarding a*=b
being the same as a=a*b
, it's not exactly the same, but close enough. a*=b
is the same as a=a*(typeOfA)b
where (typeOfA)
is a cast to the type of a
, eg a=a*(int)b
. This rarely causes a different result because usually the types of a
and b
are the same, but may have an effect due to the java's automatic widening/casting when types of a
and b
differ.
add a comment |
In this loop:
for (int i=1; i<=n; i++)
result*=x;
i
is not used, and we know that n=3
, so we could rewrite the code as:
int x = 2
int result = 1;
result *= x; // result is 2
result *= x; // result is 4
result *= x; // result is 8
System.out.println(result);
Regarding a*=b
being the same as a=a*b
, it's not exactly the same, but close enough. a*=b
is the same as a=a*(typeOfA)b
where (typeOfA)
is a cast to the type of a
, eg a=a*(int)b
. This rarely causes a different result because usually the types of a
and b
are the same, but may have an effect due to the java's automatic widening/casting when types of a
and b
differ.
In this loop:
for (int i=1; i<=n; i++)
result*=x;
i
is not used, and we know that n=3
, so we could rewrite the code as:
int x = 2
int result = 1;
result *= x; // result is 2
result *= x; // result is 4
result *= x; // result is 8
System.out.println(result);
Regarding a*=b
being the same as a=a*b
, it's not exactly the same, but close enough. a*=b
is the same as a=a*(typeOfA)b
where (typeOfA)
is a cast to the type of a
, eg a=a*(int)b
. This rarely causes a different result because usually the types of a
and b
are the same, but may have an effect due to the java's automatic widening/casting when types of a
and b
differ.
edited Nov 22 '18 at 0:02
answered Nov 21 '18 at 23:56
Bohemian♦Bohemian
295k64416553
295k64416553
add a comment |
add a comment |
Let's take it step by step:
int x=2, n=3;
int result=1;
These two lines could be simplified at most into:
int x;
int n;
int result;
x = 2;
n = 3;
result = 1;
The int
constructs (e.g. int x
) define a variable of type int (e.g. x
). This means x
can hold an integer number.
Then the x = 2
construct sets the value of x
to 2
.
Doing int x=2
is a short form for that.
You're able to define and set more than one variable using the int x=2, n=3
construct as a shorthand.
The for-loop, here with curly braces to mark the beginning and end of the block (the part of code repeating in the for-loop):
for (int i=1; i<=n; i++) {
result*=x;
}
Using no curly braces means repeat the next ONE instruction.
As you pointed out yourself, the for clause loops three times.
The loop consists of the following segments:
for ([variable instantiations]; [condition]; [loop instruction])
The variable instantiations
can (but does not have to) be used for setting up variables, usually a single variable to use as the index of the loop. In your case a new instantiation of a variable i
with the value 1
.
The loop condition
is used to test if the loop should continue. As long as this statement is true, the loop will. In your case a test is made of i
against n
. As long as i
is less than or equal to (<=
) n
the loop continues.
The final loop instruction
is a bit of code that will be performed once for each loop. In your case it will increment (++
) i
with one. (i++
is equivalent to i = i + 1
).
The instruction in the for-loop, result *= x
means that result
will be multiplied by x
(2) and the result will be stored back in result
(i.e. result = result * 2
)
Finally, the result will be printed to the console:
System.out.println(result);
If I'm not completely mistaken you should get: 8
.
add a comment |
Let's take it step by step:
int x=2, n=3;
int result=1;
These two lines could be simplified at most into:
int x;
int n;
int result;
x = 2;
n = 3;
result = 1;
The int
constructs (e.g. int x
) define a variable of type int (e.g. x
). This means x
can hold an integer number.
Then the x = 2
construct sets the value of x
to 2
.
Doing int x=2
is a short form for that.
You're able to define and set more than one variable using the int x=2, n=3
construct as a shorthand.
The for-loop, here with curly braces to mark the beginning and end of the block (the part of code repeating in the for-loop):
for (int i=1; i<=n; i++) {
result*=x;
}
Using no curly braces means repeat the next ONE instruction.
As you pointed out yourself, the for clause loops three times.
The loop consists of the following segments:
for ([variable instantiations]; [condition]; [loop instruction])
The variable instantiations
can (but does not have to) be used for setting up variables, usually a single variable to use as the index of the loop. In your case a new instantiation of a variable i
with the value 1
.
The loop condition
is used to test if the loop should continue. As long as this statement is true, the loop will. In your case a test is made of i
against n
. As long as i
is less than or equal to (<=
) n
the loop continues.
The final loop instruction
is a bit of code that will be performed once for each loop. In your case it will increment (++
) i
with one. (i++
is equivalent to i = i + 1
).
The instruction in the for-loop, result *= x
means that result
will be multiplied by x
(2) and the result will be stored back in result
(i.e. result = result * 2
)
Finally, the result will be printed to the console:
System.out.println(result);
If I'm not completely mistaken you should get: 8
.
add a comment |
Let's take it step by step:
int x=2, n=3;
int result=1;
These two lines could be simplified at most into:
int x;
int n;
int result;
x = 2;
n = 3;
result = 1;
The int
constructs (e.g. int x
) define a variable of type int (e.g. x
). This means x
can hold an integer number.
Then the x = 2
construct sets the value of x
to 2
.
Doing int x=2
is a short form for that.
You're able to define and set more than one variable using the int x=2, n=3
construct as a shorthand.
The for-loop, here with curly braces to mark the beginning and end of the block (the part of code repeating in the for-loop):
for (int i=1; i<=n; i++) {
result*=x;
}
Using no curly braces means repeat the next ONE instruction.
As you pointed out yourself, the for clause loops three times.
The loop consists of the following segments:
for ([variable instantiations]; [condition]; [loop instruction])
The variable instantiations
can (but does not have to) be used for setting up variables, usually a single variable to use as the index of the loop. In your case a new instantiation of a variable i
with the value 1
.
The loop condition
is used to test if the loop should continue. As long as this statement is true, the loop will. In your case a test is made of i
against n
. As long as i
is less than or equal to (<=
) n
the loop continues.
The final loop instruction
is a bit of code that will be performed once for each loop. In your case it will increment (++
) i
with one. (i++
is equivalent to i = i + 1
).
The instruction in the for-loop, result *= x
means that result
will be multiplied by x
(2) and the result will be stored back in result
(i.e. result = result * 2
)
Finally, the result will be printed to the console:
System.out.println(result);
If I'm not completely mistaken you should get: 8
.
Let's take it step by step:
int x=2, n=3;
int result=1;
These two lines could be simplified at most into:
int x;
int n;
int result;
x = 2;
n = 3;
result = 1;
The int
constructs (e.g. int x
) define a variable of type int (e.g. x
). This means x
can hold an integer number.
Then the x = 2
construct sets the value of x
to 2
.
Doing int x=2
is a short form for that.
You're able to define and set more than one variable using the int x=2, n=3
construct as a shorthand.
The for-loop, here with curly braces to mark the beginning and end of the block (the part of code repeating in the for-loop):
for (int i=1; i<=n; i++) {
result*=x;
}
Using no curly braces means repeat the next ONE instruction.
As you pointed out yourself, the for clause loops three times.
The loop consists of the following segments:
for ([variable instantiations]; [condition]; [loop instruction])
The variable instantiations
can (but does not have to) be used for setting up variables, usually a single variable to use as the index of the loop. In your case a new instantiation of a variable i
with the value 1
.
The loop condition
is used to test if the loop should continue. As long as this statement is true, the loop will. In your case a test is made of i
against n
. As long as i
is less than or equal to (<=
) n
the loop continues.
The final loop instruction
is a bit of code that will be performed once for each loop. In your case it will increment (++
) i
with one. (i++
is equivalent to i = i + 1
).
The instruction in the for-loop, result *= x
means that result
will be multiplied by x
(2) and the result will be stored back in result
(i.e. result = result * 2
)
Finally, the result will be printed to the console:
System.out.println(result);
If I'm not completely mistaken you should get: 8
.
edited Nov 23 '18 at 17:30
answered Nov 22 '18 at 0:53
ErkErk
51165
51165
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422026%2fdetail-about-the-function-of-such-operators%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's simply a short form for
result = result * x;
(or + x, - x, or / x).– Elliott Frisch
Nov 21 '18 at 23:52
1
I saw everywhere that
a*=b
is the same asa=a*b
, but it does not explain much how it works... what more do you want to know? and in which cases I should use it... use it wherever you can; it's cleaner.– shmosel
Nov 21 '18 at 23:56
Here's a case where you can't use
*=
:int result; result*=2;
because result needs to have a value in order to be able to do*=
. In my opiniona = a * 2
is just as fine asa *= 2
. In fact, the second version takes more "brain gymnastics". And programming, like any text, is communication, as much with the computer as yourself and your colleagues. So clarity is always good.– Erk
Nov 22 '18 at 0:32
@Erk Both versions are equally invalid if it's not initialized. As for which version is more readable, that's highly subjective and ultimately depends on convention. IMO, the shorter way is more expressive and more conventional among experienced developers.
– shmosel
Nov 25 '18 at 7:50
@shmosel: I'm answering two different questions. And now I'm answering another question... ;o) And in my experience, most of the very experienced programmers tend to "move elsewhere" so you're going to work at best with intermediate programmers... but then, IRL you'll have to deal with both forms...
– Erk
Nov 25 '18 at 9:59