2014-01-14 50 views
0

注:斜塔C++背景與蟒蛇背景C++的條件語句

在最後如果看代碼如下聲明: 什麼呢I = 1 if語句之後的意思。 看最後的其他語句。 在else語句之後,i = 0是什麼意思? 爲什麼他們有

#include <stdio.h> // preprocessor command 
int foo(int x) // function definition 
{ 
return x+1; // return expression value 
} 

int main() // this is where all C programs start 
{ 
    int i = 0; // variable definition + init. 
    while (i < 10) 
    { // loop + condition 
     i = i+1; // expression + assignment 
     printf("%d ", foo(i)); // function calls, output 
    } 
    if (i >= 10) i = 1; // conditional code execution 
    else i = 0; 
    return i; // return result, exit function 
} 
+0

我不知道,如果你問_why_他們在那裏或_how做陳述的工作?_但如果是前者,你正在做一個檢查,看是否while循環完全執行,然後返回1,如果沒有則返回0。返回碼對錯誤檢查等有用。 – JMercer

回答

1

在你的榜樣,i=1是不包括在if語句來,不是條件代碼分配的句子。如果你想這是相當於

if (i >= 10) // conditional code 
    i = 1; // will execute if i>=10 

或更多更清晰的

if (i >= 10) // conditional code 
{ 
    i = 1; // will execute if i>=10 
} 

,不建議不過,你可以寫所有的代碼在一行。

你應該知道不同語言的indent style的:

縮進不是大多數編程語言,它被用作輔助符號的要求。相反,程序員縮進以更好地向人類讀者傳達他們程序的結構。具體而言,縮進用於顯示控制流構造(如條件或循環)與其內部和外部包含的代碼之間的關係。但是,某些編程語言(如Python和Occam)使用縮進來確定結構,而不是使用大括號或關鍵字。

0

這只是一條if語句。當只有一行被執行時,你不需要大括號。

0

試着改變你的代碼::

#include <stdio.h> // preprocessor command 
int foo(int x) // function definition 
    { 
    return x+1; // return expression value 
    } 

    int main() // this is where all C programs start 
     { 
     int i = 0; // variable definition + init. 
while (i < 10) 
{ // loop + condition 
    i = i+1; // expression + assignment 
    printf("%d ", foo(i)); // function calls, output 
} 
if (i >= 10) { 
     i = 1; // conditional code execution 
     }else{ 
    i = 0; 
    } 
    return i; // return result, exit function 
} 

同時使用循環,你應該非常小心牙套後,如果條件爲if(i<10)如果你把括號那麼只有你的控制將基於循環中移動根據循環中提到的條件。

0

他們是C/C++相當於到Python 3的

i = 1 if i >= 10 else 0 

在C++中的if/else關鍵字採取一種說法:

if (condition) 
    <statement> 
[else if (condition) 
    <statement>] 
[else 
    <statement>] 

語句是一個條款,以分號結束,例如a = b;或包含在{} s中的複合語句,例如

if (a == 1) 
    std::cout << "a is one\n"; 
if (a == 2) { 
    std::cout << "a is two\n"; 
    return 0; 
} 

C++很大程度上不關心空白。所以上面的相當於

if(a==1)std::cout<<"a is one\n";if(a==2){std::cout<<"a is two\n";return 0;} 

if (a 
== 1) 
{ 
    std :: cout 
    << 
    "a is " 
    "one" 
    "\n"; 
} if // horrible but legal. 
(
    a == 
      2) 
{ std :: 
    cout 
    << 
     "a is two" 
     "\n" 
      ; 
      return 
       0 
       ; 
       } 

或各種其他排列:)

0

C/C++確實需要通過使用凹口,而相比之下,蟒分離代碼塊。

如果if/else之後的語句是單個項目,則不需要大括號。

if(x) a(); else b(); 

等同於:

if(x){ a(); }else{b();} 

和:

if(x) 
{ 
    a(); 
} 
else 
{ 
    b(); 
} 
0

他們是C/C++等同於Python的

i = [1 if i >= 10 else 0] 

在C++中的if/else關鍵字採取聲明:

if (condition) 
    <statement> 
[else if (condition) 
    <statement>] 
[else 
    <statement>] 

語句是一個條款,用分號,例如終止a = b;,或複合語句,包含在{} S,包含一個或多個分號封端的語句或複合語句

statement := 
    statement-text ';' | 
    '{' statement '}' 

如下:++在很大程度上不關心空白

if (a == 1) 
    std::cout << "a is one\n"; 
if (a == 2) { 
    std::cout << "a is two\n"; 
    return 0; 
} 

℃。因此,上述相當於

if(a==1)std::cout<<"a is one\n";if(a==2){std::cout<<"a is two\n";return 0;}