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++
  
User name:
Password:
Reply:
 

UserPost
Dwedit
Posts: 70/92
Oh crap.. Sorry, I was thinking Visual Basic. Visual Basic does that stuff with for...next loops, not C++.
HyperLamer
Posts: 5722/8210
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.
Dish
Posts: 437/596
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*/
}

Ramsus
Posts: 136/162
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.
Dwedit
Posts: 69/92
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.
beneficii
Posts: 215/567
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?
Kasumi-Astra
Posts: 1604/1867
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.
Ramsus
Posts: 116/162
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.

Kyoufu Kawa
Posts: 1680/2481
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.
Slay
Posts: 160/339
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.)
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.013 seconds.