簡體   English   中英

for循環中的小疑問

[英]Small doubt in for-loops

在以下代碼段中將測試 “ x”值多少次?

int x;
for(x=0;x < 10; x++)
   printf("%d",x);

在我看來,答案是11,但我的模塊說是10 ?! 我想念什么?

十一,在每次循環迭代開始時測試條件,然后調用printf:

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)

僅當x <10時循環才會運行,因此x都是0-9而不是0-10的所有值。 0-9共有10個值,因此循環運行10次。

或者,如果您只是在談論比較,那么它是測試11次。 您的模塊不正確。

如果您對調試器不滿意,則可以作弊:

int main() {
    int x;
    for(x=0;(printf("testing %d\n", x) || 1) && (x < 10); x++)
        printf("%d\n",x);
    return 0;
}

哪個打印

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

如果你想要做的事情的正確方法,並學會調試軟件的過程中,通過讀取啟動

這是帶有上面代碼的gdb會話。 您可以計算回路測試線被擊中的次數。 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       }

如果您的問題是表達式x < 10的計算次數是多少,答案取決於-。 這取決於編譯器優化。 如果編譯器生成幼稚的代碼,則它將對其進行11次評估。 如果編譯器完全展開循環,則答案將為0。介於兩者之間的任何值也是可能的。

0-9處有十個值,但將測試 11次,最后一次返回false,退出循環。

是。 TEST將執行11次,正文僅執行10次。

for(x=0;x < 10; x++) 

X從零開始,但是在9結束,因為您的代碼在x小於10時循環,因此這里有一些解決方法:

for(x=0;x <= 10; x++) 

for(x=0;x < 11; x++) 

這些都將導致11

嘿,伙計們,這要容易得多!

for循環如下所示:

環

這樣就可以測試條件:

  • 在第一次迭代之前,
  • 每次迭代之后。

因此,進行10次交互會進行11次測試。 簡單!

10次​​ -

  1. 給x賦0
  2. 檢查x <0是否為真,否則為6
  3. 執行循環體
  4. 增量x
  5. 轉到2。
  6. 循環后的代碼

如果經過它,最終將導致循環主體被調用10次,因為第十一次循環條件變為假,並且該主體從不執行。

是的, x < 10表達式被評估11次。 前10次為true,最后一次為false(因為x==10 )。

你是認真的嗎? 這應該很直觀!

x被測試了n+1次(其中n是10), 但是僅由於您使用<運算符*,所以滿足了n次條件

看效果:

int x;
for(x=0;x < 10; x++)
   printf("%d",x);
printf("%d",x);

最后一個打印語句應輸出10,這表示x被測試了一次(因為我們從零開始,所以x實際上被測試了11次)。

這是一個通常在面試問題中被問到的腦筋急轉彎。 一種簡單的檢查方法是將其從10更改為1,即基本情況:

for(x = 0; x <1; x ++)printf(“%d”,x);

它被打印1次。 當x = 1時,它將退出循環,並且不會包含在for循環的內容中。 因此,它被打印“ x”次,而不是“ x + 1”次。

同樣,您也可以這樣考慮:for(x = 1; x <2; x ++)

當x = 1而不是x = 2或(2-1)倍時,它將執行循環內容。 因此,要記住的重要事情是它何時真正脫離循環,以及x的哪些值在循環內容內運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM