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
  • 263,859
  • 56
  • 671
  • 702
HelpMe
  • 61
  • 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
  • 50,383
  • 6
  • 45
  • 81
Suresh Atta
  • 118,038
  • 37
  • 189
  • 297