Sunday 28 March 2021

Assignment Statements | PPL | Sebesta | Expressions and Assignment Statements

                   In this post, we will see Assignment Statements | PPL | Sebesta | Expressions and Assignment Statements | assignment statements, ppl, sebesta, expressions and assignment statements  


6. Assignment Statements

 

Simple Assignments

 

i. Nearly all programming languages currently being used use the equal sign for the assignment operator.

 

ii. ALGOL 60 and Ada makes use of := as the assignment operator.

 

iii. In some languages, such as Fortran and Ada, an assignment can appear only as a stand-alone statement, and the destination is restricted to a single variable.

 

Conditional Targets

 

       Perl allows conditional targets on assignment statements.

       For example, consider

 

($flag ? $count1 : $count2) = 0;

 

       which is equivalent to

if ($flag) {

$count1 = 0;

}

else {

$count2 = 0;

}

 

 

Compound Assignment Operators

 

              The form of assignment that can be abbreviated with this technique has the destination variable also appearing as the first operand in the expression on the right side, as in

 

a = a + b

 

              By Compound Assignment Operator, above statement is written as follows:

 

a+=b

 

              It is provided by ALGOL 68, C-based languages, Perl, JavaScript, Python, and Ruby.

 

Unary Assignment Operators

 

              The C-based languages, Perl, and JavaScript include two special unary arithmetic operators that are actually abbreviated assignments. They combine increment and decrement operations with assignment.

 

              In the assignment statement

sum = ++ count;

 

              It is similar to

count = count + 1;

sum = count;

 

              If the same operator is used as a postfix operator, as in

sum = count ++;

 

              It is similar to

sum = count;

count = count + 1;

 

count ++

 

              It is similar to

count=count+1

 

              When two unary operators apply to the same operand, the association is right to left. For example, in

- count ++

              count is first incremented and then negated. So, it is equivalent to

- (count ++)

 

Assignment as an Expression

 

              Expression is evaluated and then it is assigned.

e.g.

1. while ((ch = getchar()) != EOF) { ... }

 

2. a = b + (c = d / b) - 1

 

 

 

Multiple Assignments

 

              Some programming languages like Perl, Ruby, and Lua supports multiple-target, multiple-source assignment statements.

 

For example, in Perl one can write

($first, $second, $third) = (20, 40, 60);

 

Here, $first is assigned 20, $second is assigned 40 and $third is assigned 60.


Watch following video:

Watch on YouTube: https://www.youtube.com/watch?v=KnLgp8fHR9k


No comments:

Post a Comment