wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How to use boolean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to use boolean?" Watch "How to use boolean?" New topic
Author

How to use boolean?

Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Hi all,

I came across this code but I don't understand it. Especially the boolean part. What does it mean when CR is set to true? Why must we check (CR == false) in the else statement, isn't it redundant?

Can we avoid using 'nested if' in this code? How to re-write it?




Thank you in advance.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Looks like if (CR == false) makes a new line print only if a letter is printed. Otherwise a new line will be printed for all letters and non letters.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Never use == false or == true. You write if (CR) . . .

And don't use all capital letters for that CR variable, for two reasons.
  • Java conventions use mixed case for identifiers (exception "constants").
  • I thought you were talking about me
  • If you make a mistake and write = false instead of == false, you can get all sorts of nasty errors later on in the program.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    You are right that the CR variable is redundant; you can simply write

    and you don't need to declare any boolean variables.
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3420
        
      12
    Originally posted by Campbell Ritchie:
    You are right that the CR variable is redundant; you can simply write

    and you don't need to declare any boolean variables.


    If you have more than one consecutive non-letter character, this code will print a newline for each one. The OP's code would only print one


    Joanne
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Yes, you're right, Joanne. Thank you. I missed that.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    You can avoid the nested if by using the continue statement. Whether that's better is open to personal preference.



    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    Originally posted by Ilja Preuss:
    if (CR == false)


    Again you repeated the same thing which is redundant
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3420
        
      12
    Originally posted by seetharaman venkatasamy:


    Again you repeated the same thing which is redundant


    See my first reply.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by seetharaman venkatasamy:


    Again you repeated the same thing which is redundant


    It's not redundant.

    I apologize for using "CR == false" instead of "!CR", though. Copy'n'paste... :roll:
    Ls chin
    Ranch Hand

    Joined: Jun 28, 2008
    Posts: 99
    Thanks to everyone who replied, it' much appreciated.


    Originally posted by Campbell Ritchie:
    Never use == false or == true. You write if (CR) . . .
    [/code]
    Understood.

    [code]
    And don't use all capital letters for that CR variable, for two reasons.
  • Java conventions use mixed case for identifiers (exception "constants").
  • I thought you were talking about me
  • If you make a mistake and write = false instead of == false, you can get all sorts of nasty errors later on in the program.
    Ok, got it.
    Ls chin
    Ranch Hand

    Joined: Jun 28, 2008
    Posts: 99
    Originally posted by Ilja Preuss:



    This code looks neater than the 'nested if' one. Thanks.

    [ October 28, 2008: Message edited by: LS chin ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 37953
        
      22
    Can't you get rid of the "continue" (I am one of those people who don't like it) by using else if (!CR) . . .?
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Campbell Ritchie:
    Can't you get rid of the "continue" (I am one of those people who don't like it) by using else if (!CR) . . .?


    Yes.

    Another possibility is to move the if/else into a Method Object and the use return instead of continue.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to use boolean?
     
    Similar Threads
    Writing a Recursive Descent Parser, I think I'm done, well almost?
    Help with a static method
    what is this??? I m confused
    Help with my game.
    how to create a boolean truth table