• Post Reply Bookmark Topic Watch Topic
  • New Topic

var scope within if  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm trying to work out some minor bug code is suposed to have;
well, i did find it but dont understand why the first if cant be "obeyed" inside last big if
here's the code:

i mean that if i dont write 2nd if code will write something like " one bottles"
can someone pls tell me why?
thanks in advance
Edited again:
if i take off first if then it works ok too; so why some times if is "obeyed" everywhere and others dont?
[ February 22, 2004: Message edited by: miguel lisboa ]
[ February 22, 2004: Message edited by: miguel lisboa ]
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see what your getting at, it works fine, and I did take away the first if(beerNum == 1) piece of code, and it works the exact same another thing i did to make it look as if your singing it is on the first print statement i made it look like this:
System.out.println("\n"+beerNum + " " + word + " of beer on the wall");
davy
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tx for your reply;
anyway i actually figured it out- the point is that,with if #1 and without if #2, the line saying beerNum = beerNum - 1; makes that when inside last big if the loop never sees NumBeer==1, so coudnt "obbey"
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[
Originally posted by Davy Kelly:
"Four spaces per indent is probably the max you should use; many use two. (A few use three, but they're just weirdos.)" - Jim Yingst

-- sorry - couldn't resist:
you should not use spaces anyway! Just use tabs.
Most editors allow to show tabs at different positions. So user may set their editors to their prefered values. In the opposite way, converting spaces to tabs, you get bulk.
Another point: Tabs cannot be emulated by different numbers of spaces, because tabs mark positions, not distances.


Linus Torvald, CodingStyle,
Chapter 1: Indentation
Tabs are 8 characters, and thus indentations are also 8 characters.
There are heretic movements that try to make indentations 4 (or even 2!)
characters deep, and that is akin to trying to define the value of PI to
be 3.
Rationale: The whole idea behind indentation is to clearly define where
a block of control starts and ends. Especially when you've been looking
at your screen for 20 straight hours, you'll find it a lot easier to see
how the indentation works if you have large indentations.
Now, some people will claim that having 8-character indentations makes
the code move too far to the right, and makes it hard to read on a
80-character terminal screen. The answer to that is that if you need
more than 3 levels of indentation, you're screwed anyway, and should fix
your program.
In short, 8-char indents make things easier to read, and have the added
benefit of warning you when you're nesting your functions too deep.
Heed that warning.

again sorry - couldn't resist.
(Knight of the medal of dogmatic indenters)
[ February 23, 2004: Message edited by: Stefan Wagner ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I hadn't seen Linus' style guide before. It's not a bad idea, and it probably works great for Linux kernel code. But for better or worse, it's not a widely accepted standard in the Java world. If Sun had adopted a style of one tab per indent, then we'd we fine - but they didn't. So the biggest problem with this style is that most Java developers aren't used to seeing it. Readability is often a matter of doing what people expect; this is a good example. 2, 3, or 4 spaces are common in the java world; 8 spaces is not.
Actually Sun style advocates mixed tabs and spaces, which I detest, precisely because tabs have variable width. All spaces is good, all tabs is good, but mixed tabs and spaces are bad, because if you change the tab spacing it looks completely screwed up. And you can't control what tab spacing another developer uses; if they look at your code and see bad alignment, they'll probably just assume you're incompetent, rather than check to see if changing the tab spacing fixes the problem.
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm... i'gm glad i never came across that prob - tabs 2 spaces and spaces 2 stabs; just use plain 4 space tab indent, and that's all
 
Davy Kelly
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Stepan,
But may I point out, that not all tabs are set up the same way.
My OS cam with the tab in notepad having 12 spaces for indenting and my friend had his set to 10 (where is this standard 8???), and this made my programs way too annoying to read that i binned the tab.
But I only used it as a quote, personally i thought it was funny!
But it is time for a change of quote, sorry Jim!
Davy
 
sever oon
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally think that tabs are evil simply because every editor has their own interpretation of what to do with them, and not all of them are configurable. As far as Linus' quote, it doesn't really apply. Check it out:

To be perfectly honest, though, I cannot friggin' believe that we haven't adopted some sort of SCIDs-based solution yet! Compilers don't care about whitespace, so why can't programmers use whatever coding conventions they want? That should be a workspace preference that you set up with your IDE, and all code should be stored in source control with NO whitespace (unless it's significant to the compiler, as in a string or between tokens). That way, we'd be rid of people nuking diffs by converting between spaces and tabs all the time, among a host of other advantages (if I move method1() below method2(), for example, this does not functionally change the class at all but nukes the source diffs).
When oh when will someone follow through on this?
sev
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!