0

My for loop is still being executed even if my if statement is false. For example I type something random instead of straight line and the forloop will still run and output the cost. Why is that?

if (method.equalsIgnoreCase("applesauce"));
{
    for (i = 0; i <span; i++) 
    {
        total = total * apple;
        additional++;
        System.out.println(total);

    }
}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
HelpMe
  • 61
  • 1
  • 8

1 Answers1

2
if (method.equalsIgnoreCase("applesauce"));  <--

You terminated your statement right away with an ; When you write ; at the end of statement it terminates right there.

Your current code can be translated as

if (method.equalsIgnoreCase("applesauce")) {

}

{
    for (i = 0; i <span; i++) 
    {
        total = total * apple;
        additional++;
        System.out.println(total);
    }
}
user253751
  • 57,427
  • 7
  • 48
  • 90
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307