An Introduction to Programming Languages: Simultaneous Learning in Multiple Coding Environments by Paul A. Gagniuc
Author:Paul A. Gagniuc
Language: eng
Format: epub
ISBN: 9783031232770
Publisher: Springer International Publishing
4.4.3 Multiple Assignments
What kind of optimization can be implemented for multiple variables that store the same value? The answer to this question is given by multiple assignments. Usually, variables are declared one by one in a separate manner, namely, on separate lines. Computer languages such as Visual Basic 6.0, Python or JavaScript allow the assignment of a single value to several variables simultaneously. For instance, let us consider three variables, namely a, b and c. In normal cases, by using the simple assignment method, the following statements are true, namely: âaâ=â1; bâ=â1; câ=â1;â. Note that the semicolon indicates the end of the statement just like in JavaScript, C++, C#, Java and so on. However, by using the multiple assignment method, those three different statements can be reduced to one, such as: âaâ=âbâ=âcâ=â1;â. Thus, all three variables have the same value, namely 1. By extension, value 1 can be replaced with a variable. Let us consider variable d, namely: âaâ=âbâ=âcâ=âd;â, were variables a, b and c will take the value stored in variable d, whatever that value may be. This reduction is not only a cosmetic optimization, but an optimization that is also reflected at the hardware level in most computer languages. That is, because all three variables (a, b and c) are assigned to the same memory location. This multiple assignment optimization works the same whether a real primitive data type for an integer is involved (ex. VB) or an object that simulates a primitive data type for an integer is involved (ex. Ruby). The multiple assignment optimization as discussed above, is actually an old method. What is new instead, is the assignment of multiple objects to multiple variables. In Python for instance, one can assign multiple objects to multiple variables, namely a one-to-one correspondence like: (a, b, câ=â3, âPaulâ, 1). Thus, an integer with the value â3â is assigned to the variables a. A string with the value âPaulâ is assigned to the variables b. An integer with the value â1â is assigned to the variables c. This optimization, however, is really more for syntax cosmetics and shortening the source code, which is otherwise very important.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(16098)
The Mikado Method by Ola Ellnestam Daniel Brolund(13375)
Hello! Python by Anthony Briggs(13181)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(12341)
Dependency Injection in .NET by Mark Seemann(12198)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(10955)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(10804)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10540)
Grails in Action by Glen Smith Peter Ledbrook(10264)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(10201)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(9552)
Hit Refresh by Satya Nadella(9040)
Kotlin in Action by Dmitry Jemerov(8930)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(8681)
The Kubernetes Operator Framework Book by Michael Dame(8488)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8308)
Robo-Advisor with Python by Aki Ranin(8261)
Practical Computer Architecture with Python and ARM by Alan Clements(8234)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8203)