user3259071
user3259071

Reputation:

difference in else if {.. and else { if(

I recently got coursework back and was award 0 marks for some stupid reason and I checked someone in my classes and this is my code:

 public static String take(String s, int n) {

  while(true){
      if(s.equals("")){
          return "";
      } else if(s.length() < n){
          return s;
      } else { 
          return s.substring(0,n);    
      }

but his is

public static String take(String s, int n) {
    while (true) {
        if (s.equals("")) {
            return "";
        } else {
            if (s.length() < n) {
                return s;
            } else {
                return s.substring(0, n);
            }
        }
    }
}

I was wondering is there a difference in

    else if{...}

and

    else{
          if{ }
    }

Our code does EXACTLY the same..

Upvotes: 0

Views: 124

Answers (7)

manwendra
manwendra

Reputation: 1

No there is no difference technically but good practice to use else if {}

Upvotes: 0

ajb
ajb

Reputation: 31689

else if has no special meaning in Java. It's an else part whose statement is an if statement; and one must take into account that since the else part is not a block enclosed in curly braces, the else part has only one statement. So you could indent your example like this, to see it more clearly:

  if(s.equals("")){
      return "";
  } else 
      if(s.length() < n){
          return s;
      } else { 
          return s.substring(0,n);    
      }

while the other example looks like

    if (s.equals("")) {
        return "";
    } else {
        if (s.length() < n) {          // the second if statement starts here
            return s;
        } else {
            return s.substring(0, n);
        }                              // the second if statement ends here
    }

which is just the same, except for the extra braces around the second if statement.

But although else if is not special, as others have said, it's common enough that programmers follow different indentation and { } conventions when using it, especially since sometimes there can be many of them chained together. (In fact, in languages like Perl that don't allow else if without a curly brace after the else, or Ada which requires end if at the end of each if, the idiom is so indispensable that they added a special keyword elsif to allow programmers to use it.)

Upvotes: 0

slim
slim

Reputation: 41223

In most cases I encourage people to use braces wherever they can; that is never to use:

 if(x) y = z;

and always to use:

 if(x) {
    y = z;
 }

However, "if else" is a solid idiom, so it's the exception to that rule:

 if(x) {
      ...
 } else if(y) {
      ...
 } else {
      ...
 } 

... is well formatted code, and is equivalent to:

 if(x) {
      ...
 } else {
    if(y) {
      ...
    } else {
      ...
    } 
 }

... which is messier, and has scope for misplaced braces which change the logic.

Upvotes: 0

Rakesh KR
Rakesh KR

Reputation: 6527

There is no difference.

You can do it in both ways.

enter image description here

Upvotes: 0

zakinster
zakinster

Reputation: 10688

There is technically no difference, it's a matter of code style.

But keep in mind that in case of multiple else if, you may end up with a messy indentation :

if(test1) {
    ...
} else {
    if(test2) {
        ...
    } else {
        if(test3) {
            ...
        } else {
            if(test4) {
            ...
            }
        }
    }
}

instead of the cleaner :

if(test1) {
    ...
} else if(test2) {
    ...
} else if(test3) {
    ...
} else if(test4) {
    ...
}

Upvotes: 2

Keerthivasan
Keerthivasan

Reputation: 12880

No, there is no difference at all in its working. But, you can have multiple else if blocks consecutively, but not multiple else blocks consecutively with if inside it

Upvotes: 1

ucsunil
ucsunil

Reputation: 7494

No, there is no difference. You can do it either way.

Upvotes: 4

Related Questions