| 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: 32673
|
|
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: 32673
|
|
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: 3011
|
|
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: 32673
|
|
|
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: 3011
|
|
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: 32673
|
|
|
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.
|
 |
 |
|
|
subject: How to use boolean?
|
|
|