Question with returns in bool

May 26, 2017 at 4:13am
Hello guys, i have a question about "if" in one line. the idea of the function is to return the value if the sentence has or not characters. Because in if like this it works.

bool isEmpty(string s){

if (length(s)== 0){
return true;
} else {
return false;
}

But the idea is to work like this

(length(s) != 0)? return false: return true;
Last edited on May 26, 2017 at 4:22am
May 26, 2017 at 6:38am
This is fine, but a simple return !length(s); is preferred.

The syntax you're looking for is
return (length(s) != 0)? false: true;
However, in a boolean context, testing against 0 is redundant:
return length(s)? false: true;
And you might as well just return the result of the test:
return !length(s);
Last edited on May 26, 2017 at 6:44am
May 26, 2017 at 2:27pm
Thx a lot :).
Topic archived. No new replies allowed.