没有得到这个程序的预期输出 [英] Not getting the expected output in this program

查看:47
本文介绍了没有得到这个程序的预期输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

新手在这里,PLZ帮助.......


这就是我编码的(它非常非常很简单,我仍然面临着这段代码的问题)--------->

__________________________________________________ _______________

Hello everyone,
Newbie here, PLZ HELP.......

This is what I coded (it''s very very simple, still I''m facing problem with this code) --------->
__________________________________________________ _______________

展开 | 选择 | Wrap | 行号

推荐答案


大家好,

新手在这里,PLZ HELP ..... ..


这是我编码的(它非常简单,我仍然面临着问题)他的代码)--------->

__________________________________________________ _______________

#include< conio.h>

#include< stdio.h>

int main()

{

int i = 0;

int j = 1 ;

clrscr();

printf(" \ n%d",i ++&& ++ j);

printf(" \ n%d%d",i,j);

getch();

返回(0);

}

__________________________________________________ _______________

**************

**输出**

**************

0

1 1

__________________________________________________ _______________

我正在使用Borland Turbo C ++ 3.0。

我希望代码

printf (\ n%d,i ++&& ++ j);

将j递增1并输出0然后将i递增1.

现在我变为1,j变为2.

现在执行以下代码时

printf(" \ n%d%d", i,j);

我现在希望将i和j的值打印为1和2的代码,但我输出为

1 1.

我哪里错了,请指导我。

__________________________________________________ _______________

感谢E有人提前...............
Hello everyone,
Newbie here, PLZ HELP.......

This is what I coded (it''s very very simple, still I''m facing problem with this code) --------->
__________________________________________________ _______________
#include<conio.h>
#include<stdio.h>
int main()
{
int i=0;
int j=1;
clrscr();
printf("\n %d",i++ && ++j);
printf("\n %d %d",i,j);
getch();
return(0);
}
__________________________________________________ _______________
**************
**OUTPUT**
**************
0
1 1
__________________________________________________ _______________
I''m using Borland Turbo C++ 3.0.
I expect the code
printf("\n %d",i++ && ++ j);
to increment j by 1 and output 0 and then increment i by 1.

Now i becomes 1 and j becomes 2.
Now when the code below is executed
printf("\n %d %d",i,j);
I expect now that the code to print value of i and j as 1 and 2, but I get output as
1 1.

Where am I wrong, Please guide me.
__________________________________________________ _______________
THANKS TO EVERYONE IN ADVANCE...............



短路评估会导致问题。短路评估意味着只要表达式的结果已知,表达式的其余部分就不会被评估。

在任何条件下 a&& b ,如果a为假,则无论b的值如何,表达式都不能为真。所以表达式的其余部分被跳过,因为我们已经知道了答案。

当你做 i ++&& ++ j ,我从零开始,因为你使用了后缀增量运算符,直到分号才增加i。 0 = false,因此递增j的其余语句永远不会执行。


有你的解释,但更重要的是要意识到:

- 在函数调用中混合布尔表达式通常是一个坏主意,并且

- 在更复杂的语句中使用一元运算符通常也是一个坏主意。

两者都会导致意外的结果和难以阅读的代码。

Short-circuit evaluation causes the problem. Short-circuit evaluation means as soon as the result of an expression is known, the rest of the expression is not evaluated.
In any condition a && b, if a is false then the expression cannot be true regardless of the value of b. So the rest of the expression is skipped because we know the answer already.
When you do i++ && ++j, i starts at zero and because you used the postfix increment operator, i is not incremented until the semicolon. 0 = false, thus the rest of the statement incrementing j is never executed.

There''s your explanation, but what''s more important to realize is that:
- mixing boolean expressions inside function calls is generally a bad idea, and
- using unary operators within more complex statements is also generally a bad idea.
Both will often lead to unexpected results and hard-to-read code.



短路评估会导致问题。短路评估意味着只要表达式的结果已知,表达式的其余部分就不会被评估。

在任何条件下 a&& b ,如果a为假,则无论b的值如何,表达式都不能为真。所以表达式的其余部分被跳过,因为我们已经知道了答案。

当你做 i ++&& ++ j ,我从零开始,因为你使用了后缀增量运算符,直到分号才增加i。 0 = false,因此递增j的其余语句永远不会执行。


有你的解释,但更重要的是要意识到:

- 在函数调用中混合布尔表达式通常是一个坏主意,并且

- 在更复杂的语句中使用一元运算符通常也是一个坏主意。

两者都会导致意外的结果和难以阅读的代码。
Short-circuit evaluation causes the problem. Short-circuit evaluation means as soon as the result of an expression is known, the rest of the expression is not evaluated.
In any condition a && b, if a is false then the expression cannot be true regardless of the value of b. So the rest of the expression is skipped because we know the answer already.
When you do i++ && ++j, i starts at zero and because you used the postfix increment operator, i is not incremented until the semicolon. 0 = false, thus the rest of the statement incrementing j is never executed.

There''s your explanation, but what''s more important to realize is that:
- mixing boolean expressions inside function calls is generally a bad idea, and
- using unary operators within more complex statements is also generally a bad idea.
Both will often lead to unexpected results and hard-to-read code.



谢谢

这就是我想要的..........

我知道SHORT-CIRCUIT评估,但我不知道我会在这里面对它。

请再告诉我一件事。术语短路评估是指短路评估吗?是C中的技术术语还是这个现象的随机名称?


再次感谢,非常感谢......

THANKS
That''s what I was lokking for..........
I knew about SHORT-CIRCUIT evaluation, but I didn''t knew that I will face it here.
Please tell me one more thing. Does the term "SHORT-CIRCUIT evaluation" is technical terminology in C or it''s just some random name for this phenomenon?

ONCE AGAIN, THANK YOU VERY MUCH......


短路评估会导致问题。短路评估意味着只要表达式的结果已知,表达式的其余部分就不会被评估。

在任何条件下 a&& b ,如果a为假,则无论b的值如何,表达式都不能为真。所以表达式的其余部分被跳过,因为我们已经知道了答案。

当你做 i ++&& ++ j ,我从零开始,因为你使用了后缀增量运算符,直到分号才增加i。 0 = false,因此递增j的其余语句永远不会执行。


有你的解释,但更重要的是要意识到:

- 在函数调用中混合布尔表达式通常是一个坏主意,并且

- 在更复杂的语句中使用一元运算符通常也是一个坏主意。

两者都会导致意外的结果和难以阅读的代码。
Short-circuit evaluation causes the problem. Short-circuit evaluation means as soon as the result of an expression is known, the rest of the expression is not evaluated.
In any condition a && b, if a is false then the expression cannot be true regardless of the value of b. So the rest of the expression is skipped because we know the answer already.
When you do i++ && ++j, i starts at zero and because you used the postfix increment operator, i is not incremented until the semicolon. 0 = false, thus the rest of the statement incrementing j is never executed.

There''s your explanation, but what''s more important to realize is that:
- mixing boolean expressions inside function calls is generally a bad idea, and
- using unary operators within more complex statements is also generally a bad idea.
Both will often lead to unexpected results and hard-to-read code.



__________________________________________________ _______________

帮助,新手在这里--------->


我仍​​感到困惑......

正如你所说,如果左操作数的计算结果为FALSE,则不会计算逻辑运算符(&&)右侧的操作数。但是根据C语言的优先级规则,UNARY运算符(例如这个问题中的++)比二元运算符具有更高的优先级。

因此,无论运算符左侧的表达式值是什么?首先,必须单独评估i ++和++ j,因为它们的优先级高于&&运营商。


我知道我错了(基本概念),所以请指导我.........


感谢所有人提前.........

__________________________________________________ _______________

__________________________________________________ _______________
HELP, newbie here--------->

Still I am confused......
As you said, the operand on the right side of logical operator (&&) is not evaluated if left operand evaluates to FALSE. But according to priority rules of C language, UNARY operators (for example ++ in this question) have a higher priority then binary operators.
Therefore regardless of value of expression on left of operator &&, first of all i++ and ++j must be evaluated separately as they have a higher priority than && operator.

I know I''m wrong somewhere (in basic concept), so please guide me.........

THANKS TO EVERYONE IN ADVANCE.........
__________________________________________________ _______________


这篇关于没有得到这个程序的预期输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆