Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use boolean?

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 49789
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49789
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes, you're right, Joanne. Thank you. I missed that.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You can avoid the nested if by using the continue statement. Whether that's better is open to personal preference.

     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Ilja Preuss:
    if (CR == false)


    Again you repeated the same thing which is redundant
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by seetharaman venkatasamy:


    Again you repeated the same thing which is redundant


    See my first reply.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 99
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 99
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 49789
    69
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic