65.0k views
2 votes
What is the value of the var variable after the execution of the following snippet of code:

int var;
var = -1;
var = var - 1;
var = var - var;

User Fiach Reid
by
4.7k points

1 Answer

5 votes

Answer:

The value of var is 0

Step-by-step explanation:

Given:

The code snippet above

Required

Find the value of var after program execution

This line declares var as type integer

int var;

This line initializes var to -1

var = -1;

This line subtracts 1 from var; var - 1 = -1 -1 = -2

var = var - 1;

At this stage the value of var is -2

This line subtracts the value of var from var; i.e. -2 - (-2) = -2 + 2 = 0

var = var - var;

At this stage, the value of var is 0

User Christan
by
5.1k points