looping

Started by
28 comments, last by craphead 21 years, 4 months ago
To the original poster, you really need to clean up your code. For one thing, your variable declarations take up way too much space. For example, instead of saying:

int x;
int y;
int z;
...

You could just say:

int x, y, z;

OR better yet, you could initialize them all on one line like this instead:

int x = 0, y = 3, z = 2;

That would considerably shorten down the size of your program(code line wise) and make it easier to read.

And you can also read up on those case statements, it makes work alot easier both to work with and to read/understand.

[edited by - Apocalypse_Demon on November 30, 2002 8:49:04 PM]
Advertisement
not to mention, several times he uses cout ineffectively
cout << "this is my text" << endl;
cout << "" << endl;

you dont need to put the quotes in the second line there,
and moreover- you can better format your stuff like so:
cout << "this is what i will put on line 1" << endl <<        "this is what i will put on line 2" << endl <<        "this is what i will put on line 3" << endl <<        "and so on. it's much easier to read this" << endl <<        "way, no?" << endl << endl; // if you need to make an extra line break, just stick one in there.  


[edit: code tags for formating]

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

[edited by - eldee on November 30, 2002 9:04:24 PM]

-eldee;another space monkey;[ Forced Evolution Studios ]
quote:Original post by eldee
though i hear it''s bad practice to break from loops, i dunno why

It''s bad form. Usually it''s much more "structured" to have some other means of breaking out of loops.

But frankly, I don''t see what''s SO bad about it

And to the original poster of this thread, you should learn about functions, reading from files and arrays. It would considerable untangle your code ^^;
quote:Original post by Beowulf_
>>I sincerely hope that you mean
>>while(loop == true)
>>or, better yet,
>>while(true == loop)

Doh! Yeah, you know what I meant... the code wasn''t meant to be taken as-is. It was just a quick example. (why true == loop? I''d be very interested to hear the reasoning behind that one)

Someone already explained it, but it''s such a neat trick that it deserves reiterating. Actually, it''s a guard against human error. If you test (loop == true) and accidentally omit the second =, you''ll get (loop = true) which will assign true to loop and return true. If you switch the order and accidentally type (true = loop), you''ll get a compile time error (you can''t assign a value to a constant) instead of a potentially hard-to-track runtime bug.

(Obviously, this only works when comparing a variable to a constant, and most of the time you won''t make this mistake, but it''s a good habit that will save you from subtle errors every once in a while, and it doesn''t cost you anything, so why not?)
quote:Original post by Miserable
Actually, it's a guard against human error. If you test (loop == true) and accidentally omit the second =, you'll get (loop = true) which will assign true to loop and return true. If you switch the order and accidentally type (true = loop), you'll get a compile time error (you can't assign a value to a constant) instead of a potentially hard-to-track runtime bug.
I agree that it can be a guard, but I would never use it myself. Usually the expressions have a reason of being either on the left or right hand, which is based on how you view the comparison. When you compare, you say "is <left_hand_value> equal to <right_hand_value>?". In this case you ask if true equals something, which doesn't make sense, as we all know what true equals. It is syntaticly correct, but it isn't how most people read it.

EDIT: Btw, I don't think I ever made the mistake of using "=" instead of "==" when comparing.



[edited by - CWizard on November 30, 2002 10:24:56 PM]
I don''t think I''ve ever made that mistake, either, but I''ve seen enough newbies make it that I think it might be a good idea, for beginning programmers at least, to get into the habit. (Newbies have a tendency not to fully understand the difference between = and ==, which can result in some pretty heinous programs.)
I realize the difference. I only make the mistake as a matter of habit because I learned visual basic before I learned C++. (grrr) Anyway, it''s a nice thought, but thinking hard enough to type "true = variable", would require you to be aware of the problem in the first place, in which case you could just as easily type "variable == true".

If you''re using the GNU C/C++ Compiler or a port thereof (such as MinGW which comes with Dev-C++) you can add -Wall (it''s case sensitive, and means ''Warn All'') to your compiler options and it will usually warn you when you do something stupid.

MSVC probably has a similar option but I don''t use it and as a result wouldn''t know.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Beowulf: The point is to get into the habit of always putting the constant first so that you don''t have to think about it.
hey, atleast he''s good enough to go work for micro$oft.
Hey kid, ask Santa for some programming books.

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement