Side Effect (computer Science) - Example

Example

One common demonstration of side effect behavior is that of the assignment operator in C++. For example, the assignment operator returns the right operand and has the side effect of assigning that value to a variable. This allows for syntactically clean multiple assignment:

int i, j; i = j = 3;

Because the C++ assignment operator right associates, this equates to

int i, j; i = (j = 3); //j = 3 returns 3, which then gets assigned to i

Where the result of assigning 3 into "j" then gets assigned into "i". This presents a potential hangup for novice programmers who may confuse

while (b == 10) {} //Tests if b evaluates to 10

with

// The assignment function returns 10 // which automatically casts to "true" // so the loop conditional always evaluates to true while (b = 10) {}

Read more about this topic:  Side Effect (computer Science)

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)