

In the meantime, I’ll continue to document all the fun I’m having while teaching. I absolutely cannot wait to become a professor. Whether its fighting with the Scanner API or discovering new ways to play Rock Paper Scissors, I’ve got to say that I’m having a lot of fun learning from my students. I write an article to discuss my findings.Now, I hope to share my findings with others! The Take HomeĪt this point in the series, I’m starting to notice a trend: Whether or not I actually understood the bug was a separate problem, but I’m sure I saved that student a lot of time.

Thankfully, I was there to at least recognize the issue. If we used the i = i++ syntax in a loop, we’d have an infinite loop on our hands with little at our disposal to diagnose the bug.
JAVA FOR LOOP SHORTHAND CODE
If the explanation above isn’t great, try taking a look at the following code snippet which replicates the behavior: int temp = i As a result, i overwrites itself with the old value. Since i++ is a postfix operator, i is incremented but its old value is returned. To be honest, I was little perplexed when I saw it live in the lab.Īs it turns out, i = i++ doesn’t work as expected, and it has to do with how the right side of the assignment statement is evaluated. After all, it looks like we increment the variable and store the result. The following list covers quite a few examples-albeit some are silly: i = i + 1 ĭespite all the options above, I’d never thought to try i = i++, yet it makes a lot of sense. Java Increment OperatorsĪs I mentioned already, there are a lot of ways to increment a variable in Java. Unfortunately, my push for self-expression can sometimes result in mangled syntax like: int i = 0 Įvery time I see something like this I feel like I move one step closer to overcoming the curse of knowledge. Of course, once I think they’ve grasped the concept, I usually resort to shorthand like i++ and i += 1. For example, I tend to teach i = i + 1 first because it trips students up who haven’t recognized that the equal sign is really the assignment operator. Every once in a while I’ll even throw in some recursion to shake things up, but you’ll never catch me dead using a do while loop.Īt any rate, this mentality shows up when I increment variables as well. If I’m feeling good, I might even show off a for each loop. Sometimes I’ll use a for loop while other times I’ll use a while loop. For instance, it’s not uncommon for me to share solutions to looping problems using a different loop syntax each time. To aid in that message, I like to switch up my syntax every now and then. After all, I often tell my students that there are many ways to solve a problem. Perhaps this odd bit of code was my fault. In my recent attempt to teach loops, I stumbled up a student solution that had the following while loop syntax: int i = 0

However, as I continue to teach, I find that students always come up with creative ways to stretch the language. As someone who has written a bit of Java learning material, I consider myself at the very least proficient in the language.
