The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
0 votes
if x=y=z=-1

then the values of x,y,z after

++x && ++y ||++z


is shown as x=0,y=-1, and z=0


Please explain the reason behind this as I am not getting the reason of z=0.
asked in Programming by (13 points) | 78 views

2 Answers

+3 votes
this is the concept of short circuiting, statement "++x && ++y ||++z" can we written as (++x&&++y) || (++z) because logical operator has left to right associativity.

first ++x will be executed as x is -1 initially, it will be incremented  as 0 and we know anding with 0 will result in 0 that's why y will not be executed. Now because in first part(++x&&++y) we are getting whole term as 0 now ||(logical or) operator will check the second part too, ++z will increment z to 0.

Final result x=0,y=-1 and z=0.
answered by Loyal (7.9k points)
+1 vote
x=-1 y=-1 and z=-1

Here concept of short circuiting is applied

for example a&&b if a is true means a is having any value other than 0 then we will see next part b..if b is true then whole expression will give value as 1(true) as T&&T=T.if a is false in that case whole expression will be false and return 0(false) and next part b will not be executed because we have declared the result by seeing the first part a as F&&(anything)=F

on the other hand a| |b .if a is true then whole expression is true does not care about second one,in this case b will not be executed as T | | (anything ) =T .if a is false then we will see the next part b ,if b is true then whole expression will return true otherwise false. F | | F=F ,F | | T =T

now we come to our ques,here ++x will be executed and the value of x becomes 0 (false) so ++y will not be executed because 0&&(anything) =0 .now come to the second part ++z ,z will become 0 after increment so 0 | | 0 will return value 0.

hence the final value of x=0 ,y=-1,z=0
answered by Active (3.9k points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

47,139 questions
51,388 answers
66,700 users