Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Let's teach Slay C++ | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Slay

Level: 25

Posts: 160/339
EXP: 85592
For next: 4028

Since: 04-28-05
From: Threshold Between Heaven and Hell

Since last post: 1 day
Last activity: 1 day
Posted on 06-21-05 12:09 AM Link | Quote
It's a joke. I've got a compiler and have been reading tutorials on how to start programmnig basic applications in C++, but am bound to have dumb questions along the way. Rather than creating a new topic for every question I have, I thought what I'd do was create a single topic, and throw all my questions in it. Don't start to sweat just yet; this is far from a "make me a game" topic. You'll see.

Oh and if you must know, I first started "programming" in C++ when I used Graal Online's custom NPC language, it's virtually identical to C++, though since it's self contained, many things such as main() and cout/cin were unnecessary. I actually think that looking at pre-existing NPCs in Graal was a fantastic way to introduce myself to the basics of programming, such as boolean operands, loops and integers vs float vs chars. But now I ramble. If multiple questions go on unanswered, please reference the question number when posting a reply. I'll change a question to red when it's been answered.

I'll write key words in all caps, to descern them from normal speech, and italicize code..

Question One
Simply put; what is the difference between FOR loops and WHILE loops? With FOR loops, you define a variable, a condition, then modify the variable. It seems that you can do the identical inside of a WHILE loop. Such as...
for ( int x = 0; x < 10; x++ )
This defines that we have an integer x which contains the digit zero, and so long as x is less than ten, add one to x. So we continually add one to x until it equals ten, when the loop ends. But...
while (x < 10)
x++;

...does the exact same thing, if I'm not mistaken. This would also add one to x so long as x is less than ten, the only difference I see is that in a FOR loop, you define a variable within, whereas with a WHILE loop, you would need to define the variable ahead of time. I suppose what I want to know is what the practical difference is between the two. I cannot think of a situation in which you could only use WHILE for something that FOR could not do, or the converse of that.

Question Two
Is there a list I can access of preprocessor directives? The two I know so far are...
iostream
cstdlib
I'm ignorant of how many there potentially are, so if the number is extremely high or listing them all is somehow improbable or impractical, let me know. I don't plan to use any of them yet, really, I just possess astronomical levels of curiosity, to which this is not exempt.



01.EDIT
(Thanks to Ramsus on both questions. More to inevitably come in the following days.)


(edited by Slay on 06-22-05 04:17 AM)
Kyoufu Kawa
I'm not bad. I'm just drawn that way.
Level: 70

Posts: 1680/2481
EXP: 3008456
For next: 7355

Since: 03-19-04
From: Catgirl Central

Since last post: 14 hours
Last activity: 13 hours
Posted on 06-21-05 12:53 AM Link | Quote
Question 1.

In a for loop, x is increased, I think, at the end of each iteration. In a while loop, x's increasal can happen at any point within the iteration.
Ramsus

Octoballoon
Level: 19

Posts: 116/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 06-21-05 02:19 AM Link | Quote
The difference between while and loop is just stylistic. It depends on what you want to communicate/emphasize in your code.

If you want to emphasize just doing something while it's true, use while. If you want to do things one step at a time (incrementing or decrementing), you normally use for.

I.e.

while ((ch = getchar()) != EOF) { do this; }

vs.

for (int k=0; k<100; k+=2)
evens[k] = k;

And

while (*s2++ = *s1++);

/* Note: this assumes that s2 can hold s1, and s1 is null/zero-terminated. It also assumes we don't use s2 or s1, suggesting both are copies of some other pointer to the array (i.e. we put this in its own function, call it, say, strcpy). */

vs.

for (int k=0; s1[k] && k    s2[k] = s1[k];


Question 2:

#include includes source files via the preprocessor. < > implies a system or standard header file. #include "" implies a local file (which you'll normally tag a .h or .hpp onto). Header files are used to write declarations for interfaces:

char *strcpy(char *, char *);
extern int errno;
typedef unsigned long uint32_t;

etc. that tell your source code what functions and data types are available and how to use them. When you want to share functions between source files, you'll have to write your own headers.

Just search for "standard C++ library headers" for a listing of the ones that come with all compilers. All good reference books include library documentation as well that tells you everything about them, and on UNIX you can use manpages to find out what system headers you need for functions and how to use those functions. Apple has their own site that they copy onto every Mac with Xcode, and Microsoft has MSDN plus manuals that come with their compilers.




(edited by Ramsus on 06-20-05 05:25 PM)
Kasumi-Astra
Administrator
Level: 62

Posts: 1604/1867
EXP: 1971846
For next: 12840

Since: 03-15-04
From: Reading, UK
Uni: Sheffield, UK

Since last post: 1 day
Last activity: 12 hours
Posted on 06-27-05 06:25 AM Link | Quote
Use FOR when you know exactly how many itterations of the code you need to execute. Use WHILE when you do not know how many itterations of the code you want to execute, and use an arguement to specify when the loop should stop.
beneficii

Lakitu
Level: 36

Posts: 215/567
EXP: 299656
For next: 8454

Since: 06-27-04
From: Cordova, TN, USA

Since last post: 14 hours
Last activity: 6 hours
Posted on 06-27-05 09:52 AM Link | Quote
Pointers are totally awesome! A pointer points to the address in memory of a value, or the starting address of an array of values. It is declared like so:

type *identifier

If you want to pass the pointer to a value on, for a variable that's just a value, use & like so:

&identifier

If you have a unary (one item) pointer, and you want its value, put on:

*identifier

And if you want from a pointer to an array, just do like a normal array:

identifier[index]

Got it?
Dwedit

Shyguy
Level: 17

Posts: 69/92
EXP: 20794
For next: 3949

Since: 04-26-04

Since last post: 5 days
Last activity: 1 day
Posted on 07-13-05 12:10 PM Link | Quote
While and for loops are actually different.
This might be compiler specific, but in a for loop, it remembers the value to compare the variable to, but in a while loop, it checks the variable each time.

int number;
int count()
{
number--;
return number;
}

void some_function()
{
number=10;
int i;
for (i=0;i {
cout << i << endl;
}
}

void some_other_function()
{
number=10;
int i=0;
while (i {
cout << i << endl;
i++;
}
}

I think the while version and the for version will have different results, but if you want to test it out and correct me, go ahead.
Ramsus

Octoballoon
Level: 19

Posts: 136/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 07-13-05 06:53 PM Link | Quote
EDIT: Accidentally hit enter the first time while quoting the above post to see the source code.

That's a consequence of your compiler and its optimizer. While and for should be equivalent, and it sounds more like your compiler has a broken optimizer (if anything, it should inline count, not store the result, because calling count() has side effects). I suggest avoiding side effects altogether in conditional statements.

I even profiled the code after compiling it with GCC, and sure enough, count gets called 6 times from some_function and 6 times from some_other_function, and with -O3 optimizations in GCC, all of your functions are simply inlined. Both times it had the same exact output.


(edited by Ramsus on 07-13-05 10:08 AM)
(edited by Ramsus on 07-13-05 10:10 AM)
(edited by Ramsus on 07-13-05 10:12 AM)
Dish

Spiny
Level: 38

Posts: 437/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 07-13-05 10:09 PM Link | Quote
What ramsus said. For loops are exactly the same as while loops, except they have an opening and iteration statement. The only time this makes any real significance is when you use 'continue' to restart the loop -- in which case the while loops starts over, whereas the for loop runs its iteration then starts over. Example:



int i;

for(i = 0; i < 5; i++)
{
if(i == 3) continue;

DoSomething(); // this will be skipped if i == 3, however it will be run when i == 4
}

i = 0;
while(i < 5)
{
if(i == 3) continue; /* this will cause an infinite loop when i reaches 3, since it is never incremented*/

DoSomething();
i++; /* the continue keyword will not execute this line like it does in the for loop*/
}

HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 5722/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 07-14-05 04:50 AM Link | Quote
So Continue goes back to the start of the loop? Neat!

Also, there is one situation where loops can act the way Dwedit described. If you use the 'volatile' keyword, like so:

voltatile int x = 12;
while(x > 0)
{
DoSomething();
x--;
}


Normally it would just stick x in a register and decrement it, but with the volatile keyword, it actually reads x from memory, decrements it, and writes it back. Why? In case another thread modifies it. If you want to end the loop from another thread by setting x to 0, it won't work without this keyword because the loop won't even notice the change.

Of course this does mean volatile vars take longer to work with, so be careful how you use them.
Dwedit

Shyguy
Level: 17

Posts: 70/92
EXP: 20794
For next: 3949

Since: 04-26-04

Since last post: 5 days
Last activity: 1 day
Posted on 07-15-05 09:19 AM Link | Quote
Oh crap.. Sorry, I was thinking Visual Basic. Visual Basic does that stuff with for...next loops, not C++.


(edited by Dwedit on 07-15-05 12:19 AM)
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Let's teach Slay C++ | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.008 seconds.