Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
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;
   }
}
Posted
Updated 31-Jul-15 3:06am
v2
Comments
Michael_Davies 31-Jul-15 8:42am    
Looks like someone wanted a blank line after every two lines.
anaQata 31-Jul-15 8:56am    
yeah. I do get that.
I wanted someone to explain to me the logic behind it!!
[no name] 31-Jul-15 9:21am    
You want someone to explain your own logic to you?
F-ES Sitecore 31-Jul-15 9:22am    
Someone who doesn't know about modulus.
anaQata 31-Jul-15 9:25am    
i saw the code somewhere. Its not my original...Wes

Quote:
I wanted someone to explain to me the logic behind it!!

That could easily achieved by code inspection, that is mentally simulating the code execution.

At startup counter is equal to 0.
At first iteration (after first output line) it gets incremented, beocming 1, hence the if block is not entered.
At second iteration (after th output line) it get incremented again, becoming 2. Now the if block is entered, the newline is printed and counter becomes 0 again. This repeats over and over.
 
Share this answer
 
Comments
anaQata 31-Jul-15 9:32am    
Thank you very much CPallini. Aren't you supposed to EXPLICITLY show that the counter is linked to the for loop?
CPallini 31-Jul-15 11:08am    
Actually counter isn't neeeded at all, you may use the loop index, instead:
if (gallons % 2 == 0)
{
//...
}
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


C#
double gallons, liters;
int counter; // there is no reason not to join this line with the next
counter =0;  // but there is no need for a counter

// this is a for loop: (init;limit;increment)
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;
   }

   // the alternative excluding the counter is:
   //if(gallons % 2 == 0)
   //   System.out.println();
}




This is how I would have written it:

C#
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.
 
Share this answer
 
v2
Comments
anaQata 31-Jul-15 9:43am    
Thank you for your simpler logic.
Going by my "counter logic" however, aren't you supposed to explicitly show the relationship between counter and the loop?
Andy Lanng 31-Jul-15 10:05am    
The is no explicit relationship between the int 'counter' and the loop. There is an implicit (implied) relationship. The new logic is explicit.

The reason that it is not a direct relationship is that 'counter' can be set to any arbitrary initial value and can be used outside of the loop. The loop does not depend on 'counter'. The 'gallons' int is in the definition of the loop so it's relationship is explicit.

Is this what you mean?
anaQata 31-Jul-15 10:38am    
"There is an implicit (implied) relationship"

So from my understanding, my code is ambiguous?
Andy Lanng 31-Jul-15 11:01am    
no, there is an implicit relationship. Implicit does not mean ambiguous but it does not mean explicit, either.

If I say that A = 1 then that is explicit.
If I say that B = 2 then I can get that B != 1 but that is implicit.
If I say that C != A || C != B then that is (kind of) ambiguous as I can't say anything about what C is or isn't

But let's not discuss the language and get to the meaning.

What did you mean by "aren't you supposed to explicitly show the relationship between counter and the loop".
anaQata 31-Jul-15 11:15am    
What did you mean by "aren't you supposed to explicitly show the relationship between counter and the loop"
the same way you used "gallons" in your code which is for example used in the for loop.
I thought there could be a direct way of showing that int counter was directly related to the loop!!
The debugger is your friend.

For problems like understanding how a program works, there is nothing like the debugger. The debugger let you see the execution step by step and this is invaluable.

The debugger is your friend too when it comes to understanding why a program don't do what you want.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900