Having some issues with ++ and printing!

First of all, I’d like to apologize if there is any issue with my question.

I don’t really understand why my code is printing out the correct position.
When I call Console.WriteLine($“Song number {index} is rated three stars”); shouldn’t the code need {position} instead of {index}?
I don’t undertsand why index has 1 being added to it…

using System;

namespace BuiltInMethods
  class Program
    static void Main(string[] args)
      string[] summerStrut;
      summerStrut = new string[] { "Juice", "Missing U", "Raspberry Beret", "New York Groove", "Make Me Feel", "Rebel Rebel", "Despacito", "Los Angeles" };
      int[] ratings = { 5, 4, 4, 3, 3, 5, 5, 4 };
    int index = (Array.IndexOf(ratings, 3));
    int position = index++;
    int threeStars = Array.Find(ratings, three => three == 3);
      Console.WriteLine($"Song number {index} is rated three stars");

1 Like

Welcome to the forum! I’ve edited your post so that your code is formatted correctly. (Take a look if you want to see what I added. If you can do this in future posts, it makes it a lot easier for us to run your code and re-create problems! :slight_smile: )

This is related to…

… and all your questions will be answered when I explain exactly what is happening with this line:

int position = index++;

Let’s get technical, shall we?

In C#, ++ is what’s called the unary increment operator. There’s two variations of this operator:

  1. The postfix increment operator (the one you’re using): x++
  2. The prefix increment operator ++x

Whichever way you use it, the purpose of the increment operator is to increase the value of the operand - which, in an example of x++ would be the variable x - by 1. Like so:

int x = 3;
Console.WriteLine(x); // output: 4

What’s interesting, though, is that the increment operator - as well as doing its job on the operand - has a return value. Like this:

int x = 3;
int y = x++;
Console.WriteLine(x); // output: 4
Console.WriteLine(y); // output: 3

Can you see what’s happened there?

When we have the postfix assignment operator - x++ - on the right-hand side of an assignment operation - int y = x++ - the return value which is assigned to our variable y is the value of the operand (x) before the increment operation.

In other words, when we write int y = x++ what C# is doing is:

  • declare a new variable, y,
  • set y equal to the value of x,
  • then increment x by 1.

This is why, when you’re writing back to the console later on, you’re getting the correct - and expected - value in your string. If you were to use the position variable, you’d find that actually that has the wrong number - it’d be off by one.

Hopefully that makes sense, and I’ve not confused you further. If I have, apologies - but let me know and I’ll try better to explain. :slight_smile:


@thepitycoder Thank you so much, you were completely clear on your explanation!

So if I wanted to give position the equivalence of 1 + index I’d use the prefix increment operator which would give position the value of 4 and index the value of 3?

That makes a lot of sense!

1 Like