Hi all! In a course I am T.A.ing, one of the students submitted this program as their assignment and I did not fully understand why it wasn't working:
public class Alphabet
{
public static void main (String[] args)
{
char alphabet= 'A';
System.out.println("The following is the English alphabet.");
while (alphabet<='Z')
{
System.out.print(alphabet + " ");
alphabet = alphabet++;
}
}
}
The code above printed out an infinite number of 'A's on the screen. Clearly, the alphabet variable is not being updated despite the "alphabet++." If it is changed to alphabet = ++alphabet (preincrementation instead of postincrementation) the code works correctly. But I still don't fully understand -- alphabet = alphabet++ should still result in alphabet being updated by the end of the loop's iteration. So why is alphabet not updating despite the ++, which means alphabet = alphabet + 1?