BBC (Brian's Boot Camp)

Not to be confused with the British Broadcasting Corporation, the BBC will focus on Brian's fast paced journey of the mastery of the Java programming language.

Thursday, February 17, 2005

Assignment #1 Answer

Well, here is my solution to The Final Countdown assignment:

class FinalCountdown {
public static void main(String[] arguments) {
int d = 100;

do {
if (0 == d % 5)
System.out.println(d);
d--;
} while (d > -1);

}
}

As I had begun to releate to Sten, I was having a heck of a time with this. Initially, I thought I would want to use two classes -- one that looped & counted and one that did the math and would then print out the lines -- but I thought I should try to make it work in a single class.

What I had difficulty with was the loop type. Initially, I was using a for loop. But no matter what I did, I couldn't get any output from the loop itself. When I changed to the do-while loop, there was suddenly output. The for version looks like this; I added a couple of lines to confirm whether it was actually getting through the whole thing or not:
class FinalCountdown2 {
public static void main(String[] arguments) {
int d;
System.out.println("Starting the loop.");
for (d = 100; d == 0; d--) {
if (0 == d % 5)
System.out.println(d);
}
System.out.println("Ending the loop.");
}
}

5 Comments:

Blogger Sten said...

Excellent, well done. You used a do/while which I think I've used, well never (whiles and fors are most common). So good approach. Good use of the modulus operator too.

From the point of view of a 'best practice' it's good to get into the habit of uses the braces around compound statements even if you don't need them. It makes the code much more readable, and is less prone to bugs. It's the difference between this:

if (false)
System.out.println("I won't print");
System.out.println("I will, but it looks like I won't");

and this:

if (false) {
System.out.println("I won't print");
}
System.out.println("I will, and now it's more clear");

To address your one class/two class question, you definitely made the right choice. A program of this size doesn't warrent more than one method, let alone more than one class.

Your for loop wasn't working because the test would have failed right off the bat (d == 0). So to trace it through, d starts out at 100, it tests to see if d equals 0, it doesn't so the loop doesn't execute. d >= 0 is what you wanted there.

Incidentally here's another way to do the program with a for loop without the mod op.

for (int i = 100; i >= 0; i -= 5) {
System.out.println("i = " + i);
}

So as a follow up question. What's the difference between a while loop and a do/while loop?

11:57 AM  
Blogger Brian T. Grant said...

Cool. I'll keep an eye out for opportunities to use "{" more.

I see why "d == 0" wouldn't work now. I thought I was telling the for loop to run until d == 0. Even when I got the do-while loop working, I went back and tried to get the for loop working again and didn't even think to look at the loop test.

I like your solution, too. I didn't realize that the "increment" position to the loop could be something other that x++ or x--. Now that I think of it, I guess it makes perfect sense.

I'll reply re: the do/do-while loops when I get a chance.

12:34 PM  
Blogger Sten said...

Indeed the single increment or decrement operator is most often used in for loops. But you can do more wacky (read 'less often used') stuff with them too. For example, without compiling this, how many times do you think this loop will execute?

for (int i = 0, j = 10; i < j; i++, j--) {}

1:44 PM  
Blogger Brian T. Grant said...

The difference between a while and a do/while loop is that the while loop tests its condition before looping and a do/while loop tests after looping at least once.

11:49 AM  
Blogger Sten said...

Yep, that's right. A do/while is guarenteed to execute at least once. In my opinion, is why they are seldom used; rare is the case where you would want the body of your loop code to execute without a preemptive test.

1:48 PM  

Post a Comment

<< Home