Consider the following:

for (int counter = 1; counter < 1; counter -)
How many times will the loop execute?

Recommended Answers

All 5 Replies

How about you put a print inside the loop ?

Member Avatar

The Loop will not be executed. The condition for the Loop fails as 1 < 1 is false

It will not be executed becuase it cannot be compiled.
counter -
is not valid Java

commented: I saw that too. Good catch. +3

As JamesCherrill pointed out, there is a syntax error, but if you change it to for (int counter = 1; counter < 1; counter--), it will not run, because counter is initialised to 1, the condition checks if it is < 1, so it fails, and the loop doesn't run. If you are thinking about writing an infinite loop, initialise counter to 0.

Be a part of the DaniWeb community

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