Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple if and else statement question  RSS feed

 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pulling a blank.

I have two boolean statements named found and start.

I need an if and else statement that starts if both of the boolean statements are true.

What I'm thinking is...

if (start && found)

This is if start equals true AND if found equals true.

Is this correct? Any insight is helpful.
 
Rodion Gork
Ranch Hand
Posts: 47
1
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it looks correct

Why not trying it yourself?

two boolean statements

You meant two boolean variables, to be precise
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse Schlicklin wrote:Is this correct? Any insight is helpful.

It looks perfect.

A few pointers that might help for you:

1. (which you've already done) Always give your boolean variables nice, descriptive names. This should help you work out what the expression will do.

2. If you're still not quite sure what's going on, write the expression out on a piece of paper in English (or your native language) and add "is true" to every boolean variable.
Your expression would thus be:
  if start is true AND found is true ...

3. If you write complex expressions, add brackets, even if they're not needed, viz:
  if (goingOut && (raining || cloudy))
    takeUmbrella();


HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . add brackets, even if they're not needed, . . .
Since the precedence of && is higher than || those brackets were definitely needed.
if (goingOut && (rainy || cloudy)) takeUmberlla(); // This is the line where Liutauras found the missing ) which I have corrected.
That means several things:-
  • 1: I have been naughty and not indented the code or used the correct {}
  • 2: You do not take an umbrella if you are not going out.
  • 3: If you go out and one or the other of raining or cloudy (or both), you take an umbrella.

  • if (goingOut && rainy || goingOut && || cloudy) takeUmberlla();
    That means exactly the same because || distrbutes through &&
    if ((goingOut && rainy) || (goingOut && || cloudy)) takeUmberlla();
    That means the same, too but uses () to emphasise the precedences.
  • If you are going out and it is raining, or if you are going out and it is cloudy, take the umbrella.
  • What Winston posted was this:
    if (goingOut && (raining || cloudy))
         takeUmbrella();

    That is the same as the first line of code I posted except for some whitespace, but if you remove the () you get this:
    if (goingOut && raining || cloudy) takeUmbrella();
    That means, if you are going out and it is raining, take an umbrella OR if it is cloudy take an umbrella. So if it is cloudy you would have to carry the umbrella indoors as well. It is exactly the same as this:-
    if ((goingOut && raining) || cloudy) takeUmbrella();

    So, if you are not sure about the precedences you can always add (). It is absolutely impossible to do harm with excess ().
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote: . . . It is absolutely impossible to do harm with excess ().
    That is not true. There is one instance and one only where it is possible to do harm with additional (). It is in Java Puzzlers by Bloch and Gafter.

    I shall let people guess where it is.
     
    Liutauras Vilda
    Marshal
    Posts: 4640
    316
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:if (goingOut && (rainy || cloudy) takeUmberlla();
    This is something very close to compile error, or even compile error
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Damn! I shall go back and add the missing )
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Since the precedence of && is higher than || those brackets were definitely needed...

    Yeah, I probably should have thought of a better example.

    @Jesse: My point was more of a general one. Whenever you write code, try as hard as possible to make sure that your readers (including you) CAN'T misunderstand it - even if it involves adding brackets where they're not theoretically needed.

    Better, guys?

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    And has anybody who hasn't read the Bloch and Gafter book worked out where you can cause harm with redundant () yet?
     
    Liutauras Vilda
    Marshal
    Posts: 4640
    316
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. That is why just ordered hard copy, will get in the next 5 working days. Soon will look for the answer.
     
    Campbell Ritchie
    Marshal
    Posts: 55715
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You will really enjoy that book
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!