for( int x = 0; x < 10; ++x ){ printf( "hi" ); } return 0; } Let's try to compile it. apreche@apreche:~$ gcc test.c test.c: In function ‘main’: test.c:5: error: ‘for’ loop initial declaration used outside C99 modeLet's try again. apreche@apreche:~$ gcc -std=c99 test.c apreche@apreche:~$ Hmmm, one of these things is not like the other...
I'm going to conceede this to Apreche because I just went through a bunch of my code and I never put the declaration in the for loop. In fact I almost never use for loops. I'm a fan of the while and do loops.
I'm going to conceede this to Apreche because I just went through a bunch of my code and I never put the declaration in the for loop. In fact I almost never use for loops. I'm a fan of the while and do loops.
The for loop is used more than any other loop. If you aren't using the for loop, you are doing it wrong.
Are you saying that there is no reason to use a do or while loop?
No, but usually there is a better way than just using those two. While the do and while may work for most cases, it may not be the cleanest or most efficient way.
Are you saying that there is no reason to use a do or while loop?
Very rarely. It really depends on the language and what you are writing. I can tell you that pretty much every high-level program I have written in recent memory consists almost entirely of for and foreach loops iterating over various data structures. I only end up using a while loop for something like reading data from a file e.g: while( !EOF ){ read(); }
That's what most of the coding I have done deals with. I use the while loops for cycling through arrays of data that are of variable sizes.
This is why they created the foreach loop. In any modern high-level language (PHP, Python, Ruby), you can use the foreach loop to iterate over just about anything of any size.
In a language like C, I usually don't iterate over an array of unknown size. That's the sort of software design that results in the oft-discussed buffer overflow. Proper C coding usually requires you to define data structures and handle dynamic memory allocation manually. Part of that includes tracking the size of said data structure, and with that size a for loop can be used for easy iteration. In most cases where a for loop does not suffice for iteration of a complex data structure, I usually write some sort of recursive iteration function.
Oh, and here is how to replace that while loop with a for loop.HMTKDataItem* pCurrent; for( pCurrent = pHead; pCurrent->pNext != NULL; pCurrent = pCurrent->pNext ){
Oh, and here is how to replace that while loop with a for loop.HMTKDataItem* pCurrent; for( pCurrent = pHead; pCurrent->pNext != NULL; pCurrent = pCurrent->pNext ){
That does work. Do you know which one would be faster?
I wrote that code back in 2005 as a quick way to get into using LinkedLists. It is not optimized in any way and was written to be clear to the new C/C++ programmer.
I love pointers. I tried to learn java but, once I found out that java does not support pointers I stopped.
I try to avoid pointers whenever possible. They have a habit of fucking you in the ass if you do something wrong.
I don't think you quite understand. This is low(er) level programming. For programs you are writing in your day to day life, you do not need to have precision control over the hardware and the memory. However, if you want to write, say, the software that runs on the microwave in your kitchen, you can't write that in Java. It's just not going to happen.
You don't not write with pointers because it is hard. You don't because you don't do anything that requires that sort of low-level control of the hardware. One day, if you have to write a driver or something, you are going to be in deep shit. Anyone who is seriously learning to be a programmer needs to learn the real deal, even if you never use it. If you don't understand how the machine is working on a lower level, you will fail to write optimized code, even at the high level.
I don't think you quite understand. This is low(er) level programming. For programs you are writing in your day to day life, you do not need to have precision control over the hardware and the memory. However, if you want to write, say, the software that runs on the microwave in your kitchen, you can't write that in Java. It's just not going to happen.
You don't not write with pointers because it is hard. You don't because you don't do anything that requires that sort of low-level control of the hardware. One day, if you have to write a driver or something, you are going to be in deep shit. Anyone who is seriously learning to be a programmer needs to learn the real deal, even if you never use it. If you don't understand how the machine is working on a lower level, you will fail to write optimized code, even at the high level.
No I understand. I wasn't talking about programming low level, I was just talking in general since he referenced Java. I have only finished one year of my CS degree so pardon me if I'm not teh awesome and know everything like you claim. I'm learning, and I'm doing my damned hardest to learn everything. Feel free to correct me if I ever say something not quite right.
Oh, and here is how to replace that while loop with a for loop.HMTKDataItem* pCurrent; for( pCurrent = pHead; pCurrent->pNext != NULL; pCurrent = pCurrent->pNext ){
That does work. Do you know which one would be faster?
My guess is that the for loop would be faster, but it might also be equal. I'd have to look at the generated assembly code to know for sure.
No I understand. I wasn't talking about programming low level, I was just talking in general since he referenced Java. I have only finished one year of my CS degree so pardon me if I'm not teh awesome and know everything like you claim. I'm learning, and I'm doing my damned hardest to learn everything. Feel free to correct me if I ever say something not quite right.
It's just something about what you said kind of bothered me. You said you avoid using pointers whenever possible. Well, the pointers are there whether you "use" them or not. That's how computers work. We read and write addresses of spaces in memory in order to tell the computer to do things to those spaces in memory. That's how it works. Higher level languages are just doing more work for you, and very inefficiently I might add.
Also, writing programs in C where you actually have to manage memory on your own does not have a "habit of fucking you in the ass". It's just that when you code on such a low level you have to not only think about the logic of your program, but also the logic of the actual computer. It's no longer about figuring out how to get your application to work, but how to get a machine that works in a specific way to make it happen. This is a skill that fewer and fewer people have, but it is also a skill the world desperately needs more of.
It's just something about what you said kind of bothered me. You said you avoid using pointers whenever possible. Well, the pointers are there whether you "use" them or not. That's how computers work. We read and write addresses of spaces in memory in order to tell the computer to do things to those spaces in memory. That's how it works. Higher level languages are just doing more work for you, and very inefficiently I might add.
Well, again, I'm just learning things. You don't really have to be condescending when you point out my ignorance to certain subjects that I have yet to get to. I'm just trying to add to the dicourse and maybe learn something along the way.
Comments
#include <stdio.h>
int main(void){
for( int x = 0; x < 10; ++x ){
printf( "hi" );
}
return 0;
}
Let's try to compile it.
apreche@apreche:~$ gcc test.c
Let's try again.test.c: In function ‘main’:
test.c:5: error: ‘for’ loop initial declaration used outside C99 mode
apreche@apreche:~$ gcc -std=c99 test.c
Hmmm, one of these things is not like the other...apreche@apreche:~$
while( !EOF ){ read(); }
In a language like C, I usually don't iterate over an array of unknown size. That's the sort of software design that results in the oft-discussed buffer overflow. Proper C coding usually requires you to define data structures and handle dynamic memory allocation manually. Part of that includes tracking the size of said data structure, and with that size a for loop can be used for easy iteration. In most cases where a for loop does not suffice for iteration of a complex data structure, I usually write some sort of recursive iteration function.
HMTKDataItem* HMTKDataItem::Find(wxString Search)
{
if (pHead == 0)
{
return 0;
}
HMTKDataItem* pCurrent = pHead;
while(pCurrent->pNext)
{
if(pCurrent->Name.Contains(Search)==1)
{
return pCurrent;
}
pCurrent = pCurrent->pNext;
}
return 0;
}
It's not very elegant but it is a code sample
HMTKDataItem* pCurrent;
for( pCurrent = pHead; pCurrent->pNext != NULL; pCurrent = pCurrent->pNext ){
I wrote that code back in 2005 as a quick way to get into using LinkedLists. It is not optimized in any way and was written to be clear to the new C/C++ programmer.
You don't not write with pointers because it is hard. You don't because you don't do anything that requires that sort of low-level control of the hardware. One day, if you have to write a driver or something, you are going to be in deep shit. Anyone who is seriously learning to be a programmer needs to learn the real deal, even if you never use it. If you don't understand how the machine is working on a lower level, you will fail to write optimized code, even at the high level.
Also, writing programs in C where you actually have to manage memory on your own does not have a "habit of fucking you in the ass". It's just that when you code on such a low level you have to not only think about the logic of your program, but also the logic of the actual computer. It's no longer about figuring out how to get your application to work, but how to get a machine that works in a specific way to make it happen. This is a skill that fewer and fewer people have, but it is also a skill the world desperately needs more of.