I fixed the line indent.
It's not well done, but it makes sense from a requirement point of view. Requirements are arbitrary so 'logic' doesn't come in to the decision.
The code will increment gallons in each loop.
liters will be gallons times 3.7
the output is printed
every even number of gallons gets a new line under it.
Output will be:
1 gallons is equal to 3.7 liters
2 gallons is equal to 7.4 liters
3 gallons is equal to 11.1 liters
4 gallons is equal to 14.8 liters
5 gallons is equal to 18.5 liters
6 gallons is equal to 22.2 liters
7 gallons is equal to 25.9 liters
8 gallons is equal to 29.6 liters
9 gallons is equal to 33.3 liters
10 gallons is equal to 37 liters
double gallons, liters;
int counter;
counter =0;
for(gallons=1; gallons<=10; gallons++)
{
liters=gallons*3.7;
System.out.println(gallons + " gallons is equal to " + liters + "liters");
counter++;
if(counter==2)
{
System.out.println();
counter=0;
}
}
This is how I would have written it:
for(int counter = 1; counter <= 10; counter ++)
{
double gallons = counter;
double liters = gallons * 3.7;
System.out.println(gallons + " gallons is equal to " + liters + "liters");
if(counter % 2 == 0)
System.out.println();
}
So now
counter
is defined only by the
for
loop. The two are inseparable. The value of
counter
is explicitly tied to the
for
loop.
The
gallons
and
liters
variables also only exist in the scope of the
for
loop, but there value is implicitly connected to the loop. They are not defined by, nor do they define the
for
loop.
We could consider the doubles as explicit as they are defined by the counter, which is defined by the loop, but that's letting semantics get in the way of terminology.