非常基本,如果在C中有功能

Whi*_* S. -2 c if-statement function

我是学生,我明天要参加考试,如果功能实际意味着需要有人向我解释一下这里的"a || b"和"a && b".

这是我的意思的一个例子:

a = 0,b = 1,c = 0

一个)

if(a||b)
  c=++b;
c++;
Run Code Online (Sandbox Code Playgroud)

解决方案:c = 3

b)

if(a&&b)
  c=++b;
c++;
Run Code Online (Sandbox Code Playgroud)

解决方案:c = 1

我不明白|| b和&& b是什么意思.我把它看作是一个OR b和一个AND b,但这究竟意味着什么?

man*_*mat 6

这些是您解决这两个问题的起始值: a=0 , b=1 , c=0

这两个问题中的问题是:"变量c的值是多少"


让我们看看第一个问题:

if(a||b) // if a or b is true (meaning in this case not 0)
  c=++b; // then increment the value of b (was 1, now 2) and assign the value to c
c++; // increment c's value again (was 2, now 3)
Run Code Online (Sandbox Code Playgroud)

因此,解决方案是3.


第二个问题

if(a&&b) // if a is true (shortcutting here, because a is 0, which is false)
  c=++b; // we don't get to this part
c++; // increment c (which was 0, now 1)
Run Code Online (Sandbox Code Playgroud)

解决方案是1