0

Here is what i want.

If word.length() > 0 then delete or insert word.

So, we can structure the code like this:

if(word.length()>0){
   if(action.equals("insert")){
       insert(word);
   }
   else if(action.equals("delete")){
       delete(word);
   }
}

However, the above nested if is hard to read, so we can do

if(word.length()>0 && action.equals("insert")){
    insert(word);
}
else if(word.length()>0 && action.equals("delete")){
    delete(word);
}

However, the above code repeats word.length() 2 times which lead to duplicates & that is not good. So if we try

    if(word.length()>0){
        System.out.println("ok to insert or delete");
    }
    else if(action.equals("insert")){
        insert(word);
    }
    else if(action.equals("delete")){
        delete(word);
    }

however, the above code will do the Insert or Delete even if word.length==0.

That quite confusing, cos in wiki http://en.wikipedia.org/wiki/Conditional_(programming), they said:

"Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped."

if condition1 then
   --statements
elseif condition2 then
    -- more statements
elseif condition3 then
    -- more statements;
...
else
    -- other statements;
end if;

What wiki said mean, if condition1 ==true then do the condition2, but if if condition1 ==false then skip all following conditions (condition2,3,...)

But that does not happen in Java? I am confused??

7
  • What wiki said mean, if condition1 ==true then do the condition2 What? No. Commented Nov 23, 2013 at 23:04
  • 1
    it said "Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped." Commented Nov 23, 2013 at 23:05
  • 2
    "else" means "otherwise". Commented Nov 23, 2013 at 23:07
  • 2
    By using the topmost, nested if. There's nothing wrong with it, really. Commented Nov 23, 2013 at 23:10
  • 2
    Your first solution is fine. Commented Nov 23, 2013 at 23:10

4 Answers 4

4

It will work rewriting your code like:

if(word.length()==0){
    System.out.println("word length is 0");
} else if(action.equals("insert")){
    insert(word);
} else if(action.equals("delete")){
    delete(word);
}

And it is read as: if word length is 0 it will print "word length is 0" otherwise: if action is equals to "insert" it will invoke method insert otherwise if action is equals to "delete" then invoke delete method.

Each if structure has one statement that is executed if the given condition is true. And only this statement is executed, {} could be used to group more statements (block of code). So it is right to say that in a structure of if-elseif only the statements following the first condition that is found to be true will be executed.

The first condition has one block for the true case and one statement (if structure) for the else case. The same for the second condition, and the 3rd has just a block for the true case. Another way to see the statements separations would be:

if(word.length()==0){
    System.out.println("word length is 0");
} else 
    if(action.equals("insert")){
       insert(word);
    } else 
       if(action.equals("delete")){
           delete(word);
       }

using block for the true and false case you would have something like:

if(word.length()==0){
    System.out.println("word length is 0");
} else {
    if(action.equals("insert")){
       insert(word);
    } else {
       if(action.equals("delete")){
           delete(word);
       }
    }
}

the result is the same.

Sign up to request clarification or add additional context in comments.

Comments

1

You misread the wiki. But if you don't like option 1 (which I think is fine) then make the insert and delete methods look at the word length:

public void insert(String word) {
   if( word == null || work.isEmpty() ) return
}

Then you have:

if( "insert".equals(action) ){
    insert(word);
}
else if( "delete".equals(action) ){
    delete(word)
}

I used "insert".equals(action) instead of action.equals("insert") just in case "action" is null.

Comments

1

The wiki is correct, you just have your conditions backwards.

"however, the above code will do the Insert or Delete even if word.length==0."

Not quite. The code will do the insert or delete only if word.length == 0. If word.length > 0, it will print the message, then the insert and delete blocks will be skipped.

It should probably be:

 if(word.length() == 0) //if condition1 is true, we will skip all the other blocks after this one
 {
        System.out.println("Cannot insert or delete");
 }
 else if(action.equals("insert")) //okay, condition1 must have been false at this point
 {
        insert(word);
 }
 else if(action.equals("delete")) //and condition2 must have been false at this point
 {
        delete(word);
 }

Comments

0
if(word.length()>0){
        System.out.println("ok to insert or delete");
    }
    else if(action.equals("insert")){
        insert(word);
    }
    else if(action.equals("delete")){
        delete(word);
    }

here you do two diffrent checks thats the problem first you check the word length and then you check for the action

I think the best way you can do it is like you first did it

if(word.length()>0){
   if(action.equals("insert")){
       insert(word);
   }
   else if(action.equals("delete")){
       delete(word);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.