Guys I don't know the output of the following loop please help me out

for ( int = 10; i > 0; i--)
      {
      system.out.println ( i*2);
      }

and one more:

while( a < 20 )
{ a += 3; System.out.println( a ); }

Thanks!

Recommended Answers

All 4 Replies

for ( int = 10; i > 0; i--)

missing i in the declaration

Guys I don't know the output of the following loop please help me out

I think you should study how loops work before asking questions like these.. the codes you posted are very basic it should be easy enough for you alone

Even a book can show examples like these

for ( int i = 10; i > 0; i--)
    {
    system.out.println ( i*2);
    }

the output is the current iterative value multiplied to 2

while( a < 20 )
    { a += 3; System.out.println( a ); }

"if" the value of a is originally 0 the last output should be 18

to find out the output of those few lines, why didn't you just run the code and check it yourself?

Well, if you do copy the code you've just posted, it won't run and will give an error message because you missed giving the counter "i" a name in your loop.

See below:

for ( int = 10; i > 0; i--) -->  int i, not just int

Ok. What's happening in the loop is that i is going to decrement by 1 as long as it's greater than 0 and then be multiplied by 2.

1st iteration:   i = 10 and 10 > 0  --->> output: 10*2 = 20
 2st iteration:   i = 9 and 9 > 0  --->> output: 9*2 = 18

it will keep going until the condition is met in your loop. That'll be the 10th iteration when the condition is met. 

10th iteration:                ---->> output: 1*2 = 2

I hope this helps you with understanding for-loops.. They can be quite tricky!

In your for-statement, you

System.out.println();

was made to

system.out.println();

. Observe capitalization because Java is case-sensitive.

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.