-3

the else i have labeled has an error saying it is unnecessary. How can I get that else to line up with the first if?

if (heading < 270 && heading > 90)
        directionToFace= 'S'; 
        if (heading > 180)
        {
            degreeToWalk = (heading-180);
            directionToWalk = 'W';
        }
        else
        {
            degreeToWalk = (180-heading);
            directionToWalk = 'E';
        }
    else//this is where the error occurs
        directionToFace='N';
    {
        if (heading > 270)
        {
            degreeToWalk =(heading-270);
            directionToWalk='W';
        }
        else 
        {
            degreeToWalk = (heading);
            directionToWalk = 'E';
        }
    }
Aiden
  • 1
  • 1
  • 1
    What language is this supposed to be in? Is this a self-contained, compilable example? –  Oct 28 '15 at 15:13

4 Answers4

1
if (heading < 270 && heading > 90){
        directionToFace= 'S'; 
        if (heading > 180)
        {
            degreeToWalk = (heading-180);
            directionToWalk = 'W';
        }
        else
        {
            degreeToWalk = (180-heading);
            directionToWalk = 'E';
        }
}
    else//this is where the error occurs
        directionToFace='N';
    {
        if (heading > 270)
        {
            degreeToWalk =(heading-270);
            directionToWalk='W';
        }
        else 
        {
            degreeToWalk = (heading);
            directionToWalk = 'E';
        }
    }

add brackets after if...

if(cond) 
  stmt1
  stmt2

only stmt1 is dependent on cond.

if(cond){
      stmt1
      stmt2
}

you need to add brackets around them to make it dependent on cond

Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42
0

You are missing { in first line

shirakia
  • 2,369
  • 1
  • 22
  • 33
0

If this is Java, then you cannot put multiple single statements in an if block, it needs to be wrapped in a block (using {}). In your case, "if" is only processing the first line:

directionToFace= 'S'; 

in the first if statement, and the next statement is:

    if (heading > 180)
    {
        degreeToWalk = (heading-180);
        directionToWalk = 'W';
    }
    else
    {
        degreeToWalk = (180-heading);
        directionToWalk = 'E';
    }

Because java doesn't care about spacing, and tabs, it looks at the next statement to process, and it's an else. It gets confused because the last statement wasn't either an if (without an else), or an else if.

So if we look at the code, it appears that you didn't wrap the initial if in a block statement:

 if (heading < 270 && heading > 90)
 {
     // all your statements go here
 }
ergonaut
  • 6,929
  • 1
  • 17
  • 47
0

You have

...
} else
     directionFace = 'N';
{

In pretty much any language which uses {} for block scoping, if/else will either be

if (condition)
   single_line;

or

if (condition) {
    multiple;
    lines;
}

Since you have no { after the else, the directionFace assignment becomes the ONLY component of the else clause`, and everything it is an unconditionally executed block.

Marc B
  • 356,200
  • 43
  • 426
  • 500