Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.
for(int i = 0; i < str.length(); i++)
 {
    if(i != str.length() - 1 && Character.toString(str.charAt(i)).equals("*"))
 {
      if(i != 0 && Character.toString(str.charAt(i - 1)).equals(Character.toString(str.charAt(i + 1)))) {
      } else if(i == 0) {
      } else {
        return false;
      }
    }
  }
  return true

what does the empty braces after if/else statement mean or which value is returned

share|improve this question

closed as off topic by ChrisF Jan 29 '12 at 23:21

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Is it just me or is this code... uh... crap? IMO, str.length() should be stored in a temp variable if you're going to use it every iteration, and why even go to the trouble of reinventing the wheel when you can just use what's available to you and definitely faster (javascript) : var i=0; while(true) { i=str.indexOf("*",i); if (i==-1 || i==str.length -1) {break;} else if (i!=0 && str.charAt(i-1) == str.charAt(i+1)) { return false;} } return true;. Don't understand the point in learning from the code that you've posted. Even if it is merely for an example of theory, I don't get it. =/ –  Michael Jul 28 '11 at 6:45
1  
It might be clearer if you reformatted your code so that the brackets lined up beneath each if/else if statement. –  ChrisF Jul 28 '11 at 10:27
add comment

5 Answers

The empty braces mean "do nothing." If i == 0 it will fall through and return true. It will also fall through and return true if either of the other if conditions evaluates to false.

It will only return false if i does not equal 0 and both of the other if conditions evaluate to true.

share|improve this answer
add comment

This is a style of programming which could be called self-descriptive or self-commentary. Here, developer instead of writing something like

// We don't do anything in case that i==0

writes an empty curly braces, so that other developers can understand the business going here.

share|improve this answer
add comment

If the if(...) returns true or else if(...) returns true, return true is done, else return false.

share|improve this answer
add comment

Read it like a parser:

"If [condition] then execute next statement or block. Well, here we have an open block token, so everything in the block gets executed. And here we have a close block token, OK, that's the end of that block." So it executes an empty statement.

As for what value gets returned, again, read it like a parser. If we enter the first blank block, the next token is an else, which means control flow skips down to after the current if/else chain. If we enter the second blank block, same thing. If we enter the last block, we hit a return false and return with a value of false.

Otherwise, we start hitting those close braces and slide all the way down to the next actual statement, which is the next iteration of the for loop, and we do it all over again. And if we make it all the way through the loop without ever triggering the return false, then control flow passes to the return true line, and we return true.

share|improve this answer
add comment

It's often used construct to break very complicated "if" statements into more manageable simpler conditions (resembling switch statements) to improve code clarity and document intent. However this example is misleading, because you can refactor to something like:

for(int i = 1; i < str.length()-1; i++) {
  if(Character.toString(str.charAt(i)).equals("*")) {
    if(!Character.toString(str.charAt(i - 1)).equals(Character.toString(str.charAt(i + 1)))) {
        return false;
    }
  }
}
return true

or see Michael's solution. Less code and imho more obvious.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.