-5

Note: I'm new in JavaScript, so i'm unable to search and understand matters about JS if this question related with others questions. I think this is the platform for asking question.

I'm trying to understand basic While Loop statement, I'm counting 0 to 9 as given below:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

  <script type="text/javascript">
var myCounter = 0;
var linebreak = "<br />";
document.write(linebreak);

while(myCounter < 10){
document.write("My Counter = " + myCounter);
document.write(linebreak);
myCounter++;
}

document.write("While loop is finished!");
</script>
  
</body>
</html>

My question is what is the role of myCounter++; and what does it mean? why ++?

Aariba
  • 1,174
  • 5
  • 20
  • 52
  • 3
    it means `myCounter = myCounter +1` , it increments the variable that's all – AshBringer Mar 16 '16 at 10:55
  • 1
    [Increment and decrement](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators#Increment_and_decrement) – Arun P Johny Mar 16 '16 at 10:56
  • 2
    It means what all JavaScript tutorials and references say it means, you know, the ones you read. See [the MDN page](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment_()). –  Mar 16 '16 at 10:56
  • @torazaburo clever :p – Alexander Derck Mar 16 '16 at 10:57
  • Request for close this question with very low quality of basic programming. – Ajay Pandya Mar 16 '16 at 10:57
  • 2
    Try to remove it and you'll see a magic :) – g.e.manor Mar 16 '16 at 10:58
  • 2
    @AjayPandya There is down-voting, and there is close-voting. Close voting is supposed to be done for specific reasons, none of which this questions satisfies as far as I can tell. However, it's an excellent question to down-vote, since it shows no research effort and is not useful. If down-voted enough, it will be deleted soon enough, but **only if** it has no accepted answer and no answer with a positive score, which implies some obvious possible steps vis-a-vis the answers, which can hardly be considered useful if the question was not useful in the first place. –  Mar 16 '16 at 11:05

5 Answers5

1

++ is an increment operator.

It increases the value of the variable with 1. In this case it makes sure the loop actually ends at some point, because it will run as long as myCounter < 10. If you didn't increment the value, the loop would run forever.

lshas
  • 1,691
  • 1
  • 19
  • 39
0
++ is the increment operator..for ex i++ means i=i+1      

for(int i=0;i<10;i++)
{
    System.out.printline(i);
}
In the following example first of all the intial value of i is 0 so 0<10 it comes inside the loop and print the value of i again the value of i is incremented to 1(i=i+1)
Revathi Bala
  • 159
  • 2
  • 2
  • What is `System.out`? Actually `i++` does **NOT** mean `i=i+1`, at least not in the sense that you could replace one with the other, since the former using the increment operator evaluates to the **pre-increment** value. Also, why are you formatting the first and last lines as code by indenting them? Please read up on formatting guidelines. –  Mar 16 '16 at 11:08
0

as an extension to all above answers / comments, note that the counters in "for" / "while" loops do not have to solely increment by 1. If you use ++ then the value of the increment will be 1, but it you want a different increment value, use the following:

myCounter+=2;

which will increment myCounter by 2... etc

equally - you can decrement the counter but the following:

myCounter--;
myCounter-=2;

notice also that this will increment / decrement the value after the function. If you want to increment / decrement the value first, put the ++ or -- in front of myCounter.

++myCounter;
--myCounter;
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

From your question what is the role of myCounter++;

it means + operator will increase your variable myCounter value with 1 ,this is post increment,you can also use myCounter=myCounter+1 for increment by 1,myCounter=myCounter +2 for increment by 2

post increment means the loop condition checks the conditional statement before it loops again. consequently, when myCounter equals x the loop breaks. myCounter is updated before the condition is checked.

Ajay Pandya
  • 2,417
  • 4
  • 29
  • 65
  • I'm confused, You say "`myCounter+1` for increment by 1", but that will compute the result of adding `1` to `myCounter` and then throw the result away, unless you assign the result back to `myCounter`. –  Mar 16 '16 at 11:12
  • yes sorry i forgot that have to assign it to again otherwise throws without increment. – Ajay Pandya Mar 16 '16 at 11:12
0

'++' is the Increment Operator.

'myCounter++' is equal to 'myCounter = myCounter + 1'

So the 'myCounter' Variable will keep increase till 'myCounter >= 10'


If the operator appears before the variable (++myCounter), the value is modified before the expression is evaluated. If the operator appears after the variable (myCounter++), the value is modified after the expression is evaluated.

Elvis
  • 11
  • 1
  • *`myCounter++` is equal to `myCounter = myCounter + 1`* This is just purely incorrect, at least if you mean that `myCounter++` in general can be replaced with `myCounter = myCounter + 1` and have the same semantics. –  Mar 16 '16 at 11:16