简体   繁体   English

请说明此程序中的条件

[英]Please explain if Condition in this program

Please Explain how if statement is working here I am new in c++.请解释 if 语句如何在这里工作我是 C++ 的新手。

#include <iostream>
using namespace std;
int main (){

    int b=6,c=5;
    if (b++==7 && ++c==5)
    {
        b*=c;
        cout<<"IF OUTPUT IS "<<++b<<endl;
    }
    else 
        cout<<"ELSE 1ST OUTPUT "<<b--<<endl;
    cout<<"ELSE 2ND  OUTPUT IS "<<b<<endl;

    return 0 ;
}

  

In the above code since inside the if condition you are checking post increment , (b++ ==7) and the initial value of b is 6 so the compiler first evaluates the first condition of b++==7 considering b=6 only and then modifies its value to 7 hence evaluating the initial condition as false because 6==7 results in false while for ++c==5 as this is pre increment it first modifies the value of c to 6 and then checks the condition 6===5 which is also false.在上面的代码中,因为在 if 条件中,您正在检查 post increment , (b++ ==7) 并且 b 的初始值为 6 所以编译器首先评估 b++==7 的第一个条件,只考虑 b=6 然后修改它的值为 7,因此将初始条件评估为假,因为 6==7 导致假,而对于 ++c==5,因为这是预增量,它首先将 c 的值修改为 6,然后检查条件 6=== 5 这也是假的。 Due to this both condition results in false and the program control goes to the else part of the code.因此,这两个条件都导致错误,程序控制转到代码的 else 部分。 In the else print considering post decrement in the first print statement the value of b is printed as 7 only and later on decremented by 1 printing 6 in next statement.在第一个打印语句中考虑后减量的 else 打印中,b 的值仅打印为 7,然后在下一个语句中减 1 打印 6。

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

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