How many times 'x' value will be开发者_运维知识库 tested in the following code snippet ?
int x;
for(x=0;x < 10; x++)
printf("%d",x);
To me it seems that the answer is 11 but my module says it is 10 ?! what am I missing?
Eleven, as the condition is tested at the beginning of each loop iteration, before printf is called:
0 < 10 == true
1 < 10 == true
2 < 10 == true
3 < 10 == true
4 < 10 == true
5 < 10 == true
6 < 10 == true
7 < 10 == true
8 < 10 == true
9 < 10 == true
10 < 10 == false // exit from loop (printf not executed)
Your loop runs only if x < 10, so x is all values from 0-9, not 0-10. There are 10 values 0-9, so your loop runs 10 times.
Or if you're just talking about comparison, then yes it's test 11 times. Your module is incorrect.
If you're not comfortable with debuggers, you can cheat:
int main() {
int x;
for(x=0;(printf("testing %d\n", x) || 1) && (x < 10); x++)
printf("%d\n",x);
return 0;
}
which prints
testing 0
0
testing 1
1
testing 2
2
testing 3
3
testing 4
4
testing 5
5
testing 6
6
testing 7
7
testing 8
8
testing 9
9
testing 10
If you want to do things the right way and learn to debug software in the process, start by reading this.
Here's a gdb session with the code above. You can count how many times the loop test line gets hit. It's 11.
$ gdb loop
GNU gdb (GDB) 7.0.1-debian
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/nathan/c/loop...done.
(gdb) break 6
Breakpoint 1 at 0x4004ec: file loop.c, line 6.
(gdb) run
Starting program: /home/nathan/c/loop
Breakpoint 1, main () at loop.c:6
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb) n
testing 0
7 printf("%d\n",x);
(gdb)
0
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 1
7 printf("%d\n",x);
(gdb)
1
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 2
7 printf("%d\n",x);
(gdb)
2
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 3
7 printf("%d\n",x);
(gdb)
3
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 4
7 printf("%d\n",x);
(gdb)
4
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 5
7 printf("%d\n",x);
(gdb)
5
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 6
7 printf("%d\n",x);
(gdb)
6
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 7
7 printf("%d\n",x);
(gdb)
7
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 8
7 printf("%d\n",x);
(gdb)
8
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 9
7 printf("%d\n",x);
(gdb)
9
6 for(x=0; (printf("testing %d\n", x) || 1) && (x < 10); x++)
(gdb)
testing 10
8 return 0;
(gdb)
9 }
If your question is about how many times expression x < 10
is evaluated, the answer is -- it depends. It depends on compiler optimization. If compiler generates naive code then it will evaluate it 11 times. If compiler completely unrolls your loop, the answer will be 0. Anything in between also possible.
There are ten values at 0-9, but it will be tested 11 times, the last time returns false, exiting the loop.
Yes. The TEST will be executed 11 times, the body only 10 times.
for(x=0;x < 10; x++)
X begins at zero but it ENDS at 9 because your code loops while x is less than 10 so to fix this here are a few solutions:
for(x=0;x <= 10; x++)
and
for(x=0;x < 11; x++)
These would both result in 11
Hey, folks, that's far easier!
The for loop looks like:
This gives that the condition is tested:
- before the first iteration,
- after each iteration.
Hence 10 interations gives 11 tests. Simple!
10 times --
- Assign 0 to x
- Check if x < 0 is true, if not go to 6
- Execute loop body
- Increment x
- Go to 2.
- Code after the loop
If you go through it, you end up with the loop body being called 10 times, since the eleventh time the loop condition becomes false and the body is never executed.
Yes, the x < 10
expression is evaluated 11 times. The first 10 times it is true and the final time it is false (because x==10
).
This is a brain teaser that normally gets asked in interview questions. An easy way to check is to change it from 10 to 1, i.e., the base case:
for (x = 0; x < 1; x++) printf("%d ", x);
It gets printed 1 time. When x = 1 it breaks out of the loop, and doesn't get included in the contents of the for loop. Therefore, it gets printed "x" times, not "x + 1" times.
Also, you can also think about it as doing this: for (x = 1; x < 2; x++)
It performs the loop contents when x = 1, but not x = 2 or (2 - 1) times. So the important thing to remember is when it actually breaks out of the loop, and which values for x are run within the loop contents.
Are you serious? this should be intuitive!
x
is tested n+1
times (where n
is 10), but the condition is satisfied n times only because you are using the <
operator*!
to see the effect:
int x;
for(x=0;x < 10; x++)
printf("%d",x);
printf("%d",x);
the last print statement should output 10 which indicates that x
is tested one more time (since we are starting from zero, x
is actually tested 11 times).
精彩评论