• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Coding Techniques

## Recommended Posts

Alpha_ProgDes    6921
Ok I''m coding. I''m using just whatever loop, if structures, or class I can think of. Obviously just using a construct because it looks right, isn''t a good thing. I wanted to know if there is a book or site that shows you when to use a while loop, do while loop, or for loop; when to use a class or struct; or just when to use which coding techniques or structure AND how to set it up CORRECTLY. I''ve been searching in Google and can''t find what I want. All I see is Data Structures and Algorithms (which is something you take in CS II). I need something that tells you the correct setup for simple things like loops and selection structure. Yet shows when and how to use those correctly. Can anyone point me in the right direction? thanks.

##### Share on other sites
Cedric    158
That''s an odd question... I don''t think that you need a book. Just know the characteristics of the various loops, and practice! Eventually, you will know what should be used "instinctively".

For more advanced (actually, much more advanced, compared to for/while) constructs, check out the Design Pattern book by Gamma et al. (IIRC). But be aware that if you don''t know when to use a for and when to use a while, you shouldn''t start reading this book now.

Good luck,

Cédric

##### Share on other sites
Alpha_ProgDes    6921
I know it''s a strange question.
Seeing as I didn''t word it correctly.
I don''t know if I''m looking for a Game Code Design or a
Code Design book in general (most likely the latter).

All I know is I tend to use different constructs alot.
And they work, but obviously my use in them is flawed.
For I know I could have used different one or even used them differently.

Hopefully someone knows what I''m trying to say and can give me some good advice.

Thanks.

##### Share on other sites
Guest Anonymous Poster
quote:
Original post by Alpha_ProgDes
I wanted to know if there is a book or site that shows you when to use a while loop, do while loop, or for loop; when to use a class or struct; or just when to use which coding techniques or structure AND how to set it up CORRECTLY.

I think the reason you''re having problems is that this is more of a style issue than a correctness issue.

In general:

- A while loop checks the condition at the start of each iteration.

- A do while loop checks the condition at the end of every iteration.

- A for loop performs instructions a set number of times.

However, none of these are hard and fast rules. I can make a for loop into a while loop:
bool bCondition = true;for ( ; bCondition ; ){...}

The only reason I can think of to not do this is simply that it''s not very standard, and might confuse other people who look at the code. On a solo project, that''s not usually a big concern.

On classes versus structs, I generally use classes for data structures that include functions, and structs for data structures that do not. There is no rule here either. There is exactly one difference between a struct and a class (bonus points for anyone who can name it!). However, again, it''s more standard to use classes for OO.

##### Share on other sites
Sneftel    1788
Pretty much any type of loop can be used for any other type. The main difference, really, is CONCEPTUAL. Use the right type of loop for the right purpose.

For loop: implies a set number of iterations that is known before the loop is entered. Also used to iterate over an entire data structure (i.e., a linked list). Basically, a finite series.

While loop: the most general. Implies doing something "as many times as it''s needed", which may be zero times. Implies that checking whether something still needs to be done is a simple process (i.e. a condition will suffice, with no extra need for algorithm stuff).

Do loop: probably the least used. Use of this loop implies you don''t know whether to stop until AFTER you''ve done stuff; that is, the act of executing the body of the loop produces a decision on whether to execute it again, or finish. An example is getting input from the user and seeing if it''s valid; you don''t know whether the input is good until after the user has entered it.

Goto loop: Get thee behind me, foul programming demon!

Don''t listen to me. I''ve had too much coffee.

##### Share on other sites
CWizard    127
quote:
Original post by Anonymous Poster
There is exactly one difference between a struct and a class (bonus points for anyone who can name it!).
Members are public by default in structs!!!! Hooray, whatdoiwin????

EDIT: hehe, too quick there,
---------------------------------------------
"VB is a disease"

[edited by - CWizard on October 23, 2002 1:43:28 PM]