-2

I'm a student and I have an exam tomorrow, need someone to explain me what this "a||b" and "a&&b" inside if function actually means.

Here's a example of what I mean:

a=0 , b=1 , c=0

a)

if(a||b)
  c=++b;
c++;

Solution: c=3

b)

if(a&&b)
  c=++b;
c++;

Solution: c=1

I don't understand what problem means by a||b , and a&&b . I see it just as a OR b , and a AND b , but what does that really mean?

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
White S.
  • 35
  • 2

5 Answers5

6

These are your starting values for both problems: a=0 , b=1 , c=0

The questions in both problems are: "What is the value of the variable c"


Let's look at the first problem:

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)

Thus, the solution is 3.


The second problem

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)

The solution is 1

manonthemat
  • 6,101
  • 1
  • 24
  • 49
1

If a OR b is not zero on the first one, if both a AND b are not zero on the second.

  • in c, we don't have any boolean type directly. we do so using integer. 1 means true and 0 means false. – Ahad Dec 15 '16 at 18:42
  • 1
    0 is false any non-zero is true – Jackson Thompson Dec 15 '16 at 18:47
  • C99 introduced _Bool and the use of stdbool.h, so we do have macros that introduce a boolean type...even if they are "reduced" to 1 for true and 0 for false. See http://stackoverflow.com/questions/4767923/c99-boolean-data-type – cigarman Dec 15 '16 at 19:15
1

Yes, that's exactly what it means. 'a OR b' and 'a AND b'

Since a is 0 and b is 1, their OR results in 1 (in the first if condition) and so you execute the statement 'c=++b'. Their AND results in zero and hence you don't execute the statement 'c=++b'

  • 1
    1 || 0 is not 1 , it is `true` – pm100 Dec 15 '16 at 18:44
  • Well,1 means true and 0 means false in C. and also in most other languages. – Zobia Khawaja Dec 15 '16 at 18:47
  • no, in C a zero value is treated as false, a non zero value is treated as true. so you could equally have said 0||1 is 42 (which it also isnt). Your wording makes it look like 0||1 is the same as 0|1 – pm100 Dec 15 '16 at 18:56
1

1)

if(a||b)
  c=++b;
c++;

since 0 || 1 will evaluate to true since one of them is true in OR comparison. So there will be a preincrement of b and its value will be assigned to c. Thus c will be 2 and after it c get incremented again so it will become 3

2)

if(a&&b)
  c=++b;
c++;

the comparision is going to be false since a is 0 which is false in the and comparison. so only c++; line will be evaluated and it will have post increment to 1

glant
  • 479
  • 6
  • 19
1

You actually got it right that a||b simply means "a OR b" and that a&&b means "a AND b". But the real point of those two problems is only to test your knowledge of:

  1. what's true and what's false in C
  2. what OR and AND do
  3. the difference between ++ prefix and ++ postfix

What the first problem is showing is that the if evaluates to true because a is false ( = 0 ) and b is true ( = 1 - in fact any non-zero value is true ) such that a||b = true. That is why c=3 because the statement c=++b was executed. Furthermore, ++b actually returns 2 because a prefix ++ means first you increment, then you return its new value to the rest of the expression (assignment to c in this case). Whereas, postfix ++ means first you return the value of the operand to the expression before you increment it.

On the other hand, what the second problem is showing is that the if evaluates to false because a is false ( = 0 ) and b is true ( = 1 - in fact any non-zero value is true ) such that a&&b = false. That is why c=1 because the statement c=++b was not executed and the c++ that follows will increment the c to 1 from the 0 initial value.

Leon Carlo Valencia
  • 7,979
  • 1
  • 11
  • 12