... and here is how you would do this with Python:
arr = [12, 7, 9, 25, 87]
for item in arr:
print(item)
Just to compare the syntax, here is the same thing in C:
#include <stdio.h>
int main()
{
int arr[] = {12, 7, 9, 25, 87};
int i;
for(i = 0; i < sizeof(arr)/sizeof(int); i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
There is "no beauty" in even simple C code! Let's all thank the Python development team for giving us a readable syntax. Come to think of it, they all were very fluent in C since the core of Python is writtten in C.
I guess the true beauty of C is somewhat hidden, it can do low level stuff more readable than assembly language.
sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
Its true ! Thanks Python Dev team. I saw one example from Ene where there was no indentation, I whoop it was horrible!
Anyway it is more readable than assembly but wait a minute snee, Assembly is more readable than 10000100101111, I mean a turbulent of machine code language!
evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
There is "no beauty" in even simple C code!
"Beauty is only in the eye of the beholder".
Those that know, find a well written piece of code in C very motivating and easy to work with.
The comparison of languages as it has been described here, only shows the lack of knowledge. for(i = 0; i < sizeof(arr)/sizeof(int); i++)
There's no need to evaluate sizeof(arr)/sizeof(int) every single time in the loop, since it never changes.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
I don't agree, because the compiler can see that sizeof(arr)/sizeof(int) never changes, so it certainly generates the code with a constant value. Isn't it a lack of knowledge Aia ? Knowledge, beauty, great philosophical problems to think about !
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
I don't agree, because the compiler can see that sizeof(arr)/sizeof(int) never changes, so it certainly generates the code with a constant value. Isn't it a lack of knowledge Aia ? Knowledge, beauty, great philosophical problems to think about !
The compiler doesn't miraculously change that expression into a constant. It evaluates each time through the loop. An unnecessary waste of CPU cycles when making a single call and keeping the value would have been sufficient.[As a way of clarification]
The comment about sizeof() was not the reference to lack of knowledge, but the comparison of languages base on syntax, which is why I said, "The comparison of languages as it has been described here".
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
I tried comparing the assembly code produced when you remplace sizeof(arr)/sizeof(int) by the constant 5. The diff between the 2 programs is
< cmpl $4, -8(%ebp)
< jle .L3
---
> movl -8(%ebp), %eax
> cmpl $4, %eax
> jbe .L3
So the difference is only one movl instruction.
Gribouillis
Posting Maven
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
>So the difference is only one movl instruction. cmpl $4, -8(%ebp) is not the same that cmpl $4, %eax jle .L3 in not the same that jbe .L3 movl -8(%ebp), %eax is in a loop by virtue of "Jump if Below or Equal" Having the opportunity to execute many times. So it is not "only one mov1 instruction" executed only once.
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218