Lesson hanging in same place for 2 days

I’m on lesson two of loops. Yesterday, when trying step 2 and clicking RUN, it just shows the wait loop animation…forever. No big deal, I reloaded the page and tried again. And again, and again. I gave up, figuring maybe the connection or site was having issues. Now it’s doing the same thing today. It just hangs forever. The rest of the site, and my connection, seem to be working fine. Did I accidentally write some code that the system can’t process? lol
All it is is:

while (emails > 0)
{
emails = emails--;
}

could be an infinity loop, this looks weird:

emails = emails--;

using -- is a shorthand, if we change your code to the full code (without shorthand), we get:

emails = emails = emails - 1

emails-- is short for emails = emails - 1.

3 Likes

I changed it to
emails - 1
and it works.
Why though? Why wouldn’t
emails–
work here? Isn’t it the same thing?

It is exactly an infinite loop.

No, it’s not the same thing.

The C# decrement operator -- comes in two variants, which Microsoft refers to as “postfix” and “prefix”.

You’re using the “postfix” form, i.e. emails--. The output of this operator, i.e. the value you get back if you assign it to another variable, is the value of the operand before the operation.

Your code is therefore repeatedly re-assigning emails to the same value, and never decrementing it. We can demonstrate this like so:

int number = 3;
Console.WriteLine(number);  // output: 3
Console.WriteLine(number--); // output: 3
Console.WriteLine(number); // output: 2

Conversely, if we were using the “prefix” form of the operator - which would be --emails - then the output of that operation is the value of the operator after the operation is performed. Like so:

int another_number = 3;
Console.WriteLine(another_number); // output: 3
Console.WriteLine(--another_number); // output: 2
Console.WriteLine(another_number); // output: 2

As a demonstration:

using System;

class MainClass {
  public static void Main (string[] args) {
    int emails = 5;
    Console.WriteLine("The value of 'emails' is: " + emails); // output: The value of 'emails' is: 5
    emails = emails--;
    Console.WriteLine("The value of 'emails' after the decrement operator -- is: " + emails); // output: The value of 'emails' after the decrement operator -- is: 5
  }
}

As you can see, the value of emails never changes so the condition for your while loop never becomes false. Thus, the code never finishes. :slight_smile:

Does that help? You can run my terrible example code above on a repl, if you want.

Edit: The above stuff about “postfix” and “prefix” operators is semantic, really.

To fix your code, you can do either of the following.

Solution A - Use the decrement operator only

while (emails > 0)
{
  emails--;
}

Solution B - Write the full arithmetic operation “longhand”

while (emails > 0)
{
  emails = emails - 1;
}

Makes total sense. I was unaware of the prefix option, and really of how decrement works. Apparently the lesson that mentioned increment and decrement did a piss poor job at explaining how they work, at least from what I recall of it.
Thank you!

1 Like

No worries, glad we cleared it up. :slight_smile:

Edit: The increment and decrement operators, ++ and --, just get tacked on to whatever operand you want to run the operation on. E.g.

i--; // decrement the value of i by 1
money++; // increment the value of money by 1

// these will produce the same end result, changing the variables by 1 in each case:
++money;
--i;

emails-- would work, emails = emails-- doesn’t work.