P
Phil Bradby
Hi
I am using K&R to teach a course in C and I'm puzzled by Exercise 2-2,
"write a loop equivalent to the for loop above without using && or ||".
Here is my answer:
for(i=0; i<lim-1?(c=getchar())!='\n'?c!=EOF:0:0; i++) s=c;
These are my questions.
1) Is my answer right? I think so but it makes my head ache to think
about it!
2) Is it what they had in mind? It wasn't really obvious to me what
solution they were thinking of and I don't think many of my students will
come up with the answer above.
3) What's the point of the question? I can't think of any circumstances
where && and || wouldn't be available - why would you want to tie your
hands by avoiding a language feature that does exactly what's needed?
Cheers,
PB
I am using K&R to teach a course in C and I'm puzzled by Exercise 2-2,
"write a loop equivalent to the for loop above without using && or ||".
Here is my answer:
for(i=0; i<lim-1?(c=getchar())!='\n'?c!=EOF:0:0; i++) s=c;
These are my questions.
1) Is my answer right? I think so but it makes my head ache to think
about it!
2) Is it what they had in mind? It wasn't really obvious to me what
solution they were thinking of and I don't think many of my students will
come up with the answer above.
3) What's the point of the question? I can't think of any circumstances
where && and || wouldn't be available - why would you want to tie your
hands by avoiding a language feature that does exactly what's needed?
Cheers,
PB