简体   繁体   English

C ++条件语句

[英]C++ conditional statement

Note: Leaning C++ background with python background 注意:倾斜的C ++背景和python背景

Looking at the last if statement of the code below: what does i=1 mean after the if statement. 查看下面的代码的最后一个if语句:if语句之后i = 1是什么意思。 Looking at the last else statement. 查看最后的else语句。 what does the i=0 mean after the else statement? 在else语句之后,i = 0是什么意思? Why are they there 他们为什么在那里

#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
}

In your example, i=1 is a assignment sentence not included in if -statement, not a conditional code. 在您的示例中, i=1if -statement中不包括的赋值语句,而不是条件代码。 It's equivalent to 相当于

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

or more clearer 或更清晰

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

If you want, not recommended though, you can write all the code in one line. 如果需要,尽管不建议这样做,但您可以将所有代码写在一行中。

You should be aware of the indent style of different languages: 您应该注意不同语言的缩进样式

Indentation is not a requirement of most programming languages, where it is used as secondary notation. 缩进不是大多数编程语言所必需的,在大多数语言中,缩进都用作辅助表示法。 Rather, programmers indent to better convey the structure of their programs to human readers. 相反,程序员倾向于将程序的结构更好地传达给人类读者。 In particular, indentation is used to show the relationship between control flow constructs such as conditions or loops and code contained within and outside them. 尤其是,缩进用于显示控制流构造(例如条件或循环)以及它们内部和外部包含的代码之间的关系。 However, some programming languages (such as Python and Occam) use the indentation to determine the structure instead of using braces or keywords. 但是,某些编程语言(例如Python和Occam)使用缩进来确定结构,而不是使用花括号或关键字。

It's just a one-line if statement. 这只是一行if语句。 When there is only one line to be executed you don't need the braces. 当只有一行要执行时,您不需要大括号。

Try changing your code to:: 尝试将代码更改为::

 #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
 }

you should be very careful with the braces while using loop, after if condition as in if(i<10) if you put braces then only your control will move inside the loop based onb the conditions mentioned in the loop. 在使用循环时,您应该非常小心括号,如果条件如if(i<10)如果您放置了括号,则根据循环中提到的条件,只有控件会在循环内移动。

They are the C/C++ equivalent to Python 3's 它们是相当于Python 3的C / C ++

i = 1 if i >= 10 else 0

In C++ the if / else keywords take a statement: 在C ++中,if / else关键字采用以下语句:

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

A statement is a clause, terminated by a semicolon, eg a = b;, or a compound statement, enclosed in {}s, eg 语句是一个用分号(例如a = b;)终止的子句,或用{}括起来的复合语句(例如)

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

C++ largely does not care about whitespace. C ++在很大程度上不关心空格。 So the above is equivalent to 所以上面相当于

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

or 要么

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

or various other permutations :) 或其他各种排列方式:)

C/C++ does require to separate code blocks by using indentations, in contrast to python. 与python相比,C / C ++确实需要使用缩进来分隔代码块。

If the statement after an if/else is a single item, it does not require braces. 如果if / else之后的语句是单个项目,则不需要大括号。

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

is equivalent to: 等效于:

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

and: 和:

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

They are the C/C++ equivalent to Python 它们是等效于Python的C / C ++

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

In C++ the if / else keywords take a statement: 在C ++中,if / else关键字采用以下语句:

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

A statement is a clause, terminated by a semicolon , eg a = b; 语句是一个以分号结尾的子句,例如a = b; , or a compound statement, enclosed in {}s, comprising one or more semicolon-terminated statements or compound statements 或包含在{}中的复合语句,包含一个或多个以分号结尾的语句或复合语句

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

As follows: 如下:

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

C++ largely does not care about whitespace. C ++在很大程度上不关心空格。 So the above is equivalent to 所以上面相当于

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM