2013-06-20 92 views
-6

對不起,這個問題,但我沒有其他地方去了,它讓我覺得我找不到解決方案。我對編程語言Pascal非常好,所以這個C語言對我來說似乎很熟悉,但添加一個if函數來改變while循環的整個結構對我來說太複雜了。請任何幫助表示讚賞。嗨,有人可以幫助我的這個循環

The array variable consists of a sequence of ten numbers. Inside the while loop, you must write two if conditions, which change the flow of the loop in the following manner (without changing the printf command):

  • If the current number which is about to be printed is less than 5, don't print it.
  • If the current number which is about to be printed is greater than 10, don't print it and stop the loop.

    Notice that if you do not advance the iterator variable i and use the continue derivative, you will get stuck in an infinite loop.
#include <stdio.h> 

int main() 
{ 
    int array[] = {1, 7, 4, 5, 9, 3, 5, 11, 6, 3, 4}; 
    int i = 0; 

    while (i < 10) 
    { 
     /* your code goes here */ 

     printf("%d\n", array[i]); 
     i++; 
    } 
    return 0; 
} 
+0

爲什麼Java,C++和Pascal標籤? –

+4

您是否熟悉'continue'和'break'? –

+0

如果你熟悉C語言,你肯定熟悉'if'和'break'嗎? –

回答

1

這是測試你對C流量控制的理解。你要找的東西是這樣的:

if (array[i] < 5) {i++; continue; } // increment, go back to while 
if (array[i] > 10) break;   // leave while 
1

你真的想用breakif語句。你需要知道這些概念是與任何語言。

if (array[i] > 10) 
     break; 
if (array[i] >= 5) 
     printf("%d\n", array[i]); 
+0

第二個if應該是'if(array [i] <5){i ++;繼續; }' – ctn

+1

我不認爲你需要那個@ctn。除非我誤解他的問題。聽起來像是它的<= 5,什麼都不做,5到10打印,> 10打破。這應該沒問題。 – greedybuddha

+1

是的,它的配方不是很精確。有人可能會爭辯說,如果你正在「改變printf命令」,通過在printf語句中加入。這些要求顯然是要教學生關於「繼續」和「休息」。 – ctn

相關問題