VasquezNathan / CSE_100

I created this repo so I can work on my projects across different machines. While you are welcome to use anything here, I am not responsible for anything you do with my code. Please do not plagiarize my work for school.
1 stars 0 forks source link

Use of for loop #1

Open ramenspazz opened 3 years ago

ramenspazz commented 3 years ago

https://github.com/VasquezNathan/CSE_100/blob/fa77d1924aa9209ba7efcd6228c6a4cc1c18da08/Lab06/nvasquez12.cpp#L22

Hi Nathan, Dalton here. Your use of a for loop here works, but should be a while loop for correctness. Typically one uses a while true loop with a break statement instead of allocating extra recource in a for loop. The intent of the code is more clear with a while loop. Good luck learning, happy to see you learning CSCI!

VasquezNathan commented 3 years ago

Hi Dalton, I hope all is well. I see that it would be more clear to have a while true loop, but you mentioned for(;;) using more resources. I was under the impression that they are compiled to assembly identically (using gcc at least). I'll be making the switch to while true because sometimes I can write some ugly code, but for curiosity's sake is a while true more optimal than for(;;)? @ramenspazz thank you again tho, I'm still trying to get used to github so sorry for such a late reply.

ramenspazz commented 3 years ago

Hey Nathan! For GCC/g++ versions more recent than 8, this is true. However it also falls to the specific implementation you are programming for, IE AMD64 vs ARMv7. With the rise of embedded microprocessors, there are sometimes differences in how the assembly is translated. A good example of this is the assembly difference between arduino and raspberry pi devices. You can program them both with C/C++, but the raspberry pi has extra instructions that the arduino doesn't, and in some cases uses different mappings entirely to perform the same function. It's mostly just a clear intention, as sometimes the compiler will misunderstand what you are attempting. While loops usually don't unroll elements as naturally as a for loop, which is explicit in this case to both the reader and the compiler. Don't worry too much about it, but it is a good thing to be aware of! I hope your classes are going well! Best of wishes, Dalton

On Thu, Apr 15, 2021, 01:43 Nathan Vasquez @.***> wrote:

Hi Dalton, I hope all is well. I see that it would be more clear to have a while true loop, but you mentioned for(;;) using more resources. I was under the impression that they are compiled to assembly identically (using gcc at least). I'll be making the switch to while true because sometimes I can write some ugly code, but for curiosity's sake is a while true more optimal than for(;;)? @ramenspazz https://github.com/ramenspazz thank you again tho, I'm still trying to get used to github so sorry for such a late reply.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/VasquezNathan/CSE_100/issues/1#issuecomment-820240148, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACV6PDRUVMNX2MHR3BX637DTI2RLLANCNFSM4ZL4TTSA .