Reviewed with answer from my quiz, I want to make sure that I understand it right? If not, what is yours?

int x=1;
int j;

for(j=0; j<=2; j++)
x=x*j;
System.out.print(x);

---------------------
answer is x=0

My understanding is since this for loop has no curly bracket, therefore, no loop after the first one. So the math is: x=1*0= 0. Am I right?

Yep, that's right.

No. x=x*j; will be executed 2 times
1st pass x = 1 * 0
2nd pass x = 0 * 1
Answer is still 0

You don't need {} for the contents of a for loop if there is only 1 statement. If you have any doubt try executing this:
for (int i = 0; i<10; i++) System.out.println(i);

Thanks JamesCherrill for letting me know about the for loop doesn't need {} if there is only 1 statement.
OK! so it executed 2 times. How does it get x = 0 * 1 ?

Since in the first run x=0 is the final answer so, in the second run x = 0(x in the first run) * 1(J++) !!

Actually, the loop will be executed 3 times, one for j=0, so x=x*j=1*0=0, then for j=1, so x=x*j=0*1=0, and then for j=2, so x=x*j=0*2=0, and then it will print 0 :)

Thanks JamesCherrill for letting me know about the for loop doesn't need {} if there is only 1 statement.
OK! so it executed 2 times. How does it get x = 0 * 1 ?

IT gets the o from the first run of x = x*j which is x = 1*0 = 0 so now the new value of x = 0 !

Very clear!
Thanks jokers6 and JamesCherrill.
If you don't mind explain to this problem below too. I just want to clear it out which I found it quicker & more clear from this board. Thanks so much!

Suppose num, sum & j are int variables. And the input is: 4 7 12 9 -1

What is the output of the following code?

sum= console.nextInt();
num= console.nextInt();

for (j=1; j<=3; j++)
{
num = console.nextInt();
sum= sum + num;
}
System.out.println(sum);

--------------------------
Answer is; 24

sum = 4
num = 7 (which is never used)
num = 12, sum = 4+12 = 16
num = 9, sum = 12+9 = 25
num = -1 sum = 25+-1 = 24

Uhh...why is num=7 is never used? Also, does loop suppose to be less or equal to 3?


Below is what I have traced on paper (but couldn't find the way to attach file here). It didn't result the same as the corrected answer though, which is 24.

num=4 sum=4 (sum=sum+num)
num=7 sum=11 (sum=4+7)
num=12 sum=23 (sum=12+11)
num=9 (j<=3, done 3 loops)
num=-1

The only thing I can think of but not sure that it right is:
Since sum hasn't initialize to 0 (sum=0) so it starts from 1 because of j start with 1 (j=1). So it does add 1 to the sum at the beginning,

meaning that it added to the 23 from my trace above make it 24. Am I right?

>Uhh...why is num=7 is never used?
Read the code again and watch the calls to nextInt(). You'll see it.

Also, does loop suppose to be less or equal to 3?

Depends what you're trying to do.

To understand a for loop, think of it as a while loop.

Here's a standard construction (never mind that it's obsoleted by the enhanced for)

for (i = 0; i < array.length; i++)  {do stuff;}

this can be rewritten as:

i = 0;
while (i <= array.length)
{ 
   do stuff;

   i++; 
}

The two are identical. A for loop can always be rewritten as a while loop in this way:

for (INIT; COND; INCREMENT)
{  BODY }

is always equivalent to

INIT;
while (COND)
{
  body;
  INCREMENT;
}

So you could have (but shouldn't, for reasons of legibility and style)

for (String s = getNextString(); s!=null; s=getNextString())
{
  process s in some fashion;
}

This will work fine, the reason you shouldn't do it has more to do with idiomatic usage - making sure another programmer can read it and maintain it - than anything else.


What this means is that your loop condition is up to you. Typically, when iterating a collection of stuff, you'll be iterating on length, which is the number of objects. Since we're typically zero-indexed - we start counting at zero - our final element will be numbered (length-1). If we write the loop condition such that i<length it's clear that we're going to break when we hit then index number length, which will always be one past the end of the collection.

That's why you often see < in loop conditions.

However, the important thing is legibility and obviousness. You want to write your loop condition so that it's clear when the loop should stop - it's better to have a loop on i <= LIMIT than to have to do i < LIMIT+1 .

So, in a nutshell: make your own decision. Understand the idioms, they're idioms for a reason, but whenever you're writing a loop, you have to decide what's the clearest way to express your intention.

yes you are !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.