Affectation (informatique)

Un article de Wikipédia, l'encyclopédie libre.

En algorithmique et en programmation informatique, une affectation, aussi appelée assignation par anglicisme, est une structure qui permet d'attribuer une valeur à une variable.

Il s'agit d'une structure particulièrement courante en programmation impérative, et dispose souvent pour cette raison d'une notation courte et infixée, comme x = expr ou x := expr. Dans certains langages, le symbole est considéré comme un opérateur d'affectation, et la structure entière peut alors être utilisée comme une expression. D'autres langages considèrent une affectation comme une instruction et ne permettent pas cet usage.

Notation[modifier | modifier le code]

La structure d'affectation est le plus souvent notée x = expr ou x := expr.

x=expr (sans espace autour du caractère =) Tous les shell unix. Par exemple {Bourne shell, Bash}
x = expr Fortran, PL/I, C (et ses descendants comme C++, Java, etc.), Python, Go (pour des variables préalablement déclarées), R, haskell, etc.
x := expr ALGOL (et ses dérivés), Simula, CPL, BCPL, Pascal[1], PL/M, Ada, Smalltalk, Eiffel[2],[3], Oberon, Dylan[4], Go[5] (initialisation et déclaration simultanées) Io, ML[6], etc.

D'autres langages utilisent une notation en forme de flèche ou un mot-clé.

x <- expr F#, OCaml, R, S
x <<- expr R
assign("x", expr) R
x ← expr APL[7], Smalltalk
x =: expr J
let x = expr Rust
LET x = expr BASIC
let x := expr XQuery
set x to expr AppleScript
set x = expr C shell
Set-Variable x (expr) Windows PowerShell
x : expr Macsyma, Maxima, Rebol

Certains langages placent l'expression à gauche de l'opérateur, et l'identificateur à droite.

MOVE expr TO x COBOL
expr → x TI-Basic, Casio Basic
expr -> x R
put expr into x LiveCode

Certains langages, comme Lisp[8],[9] ou Tcl, utilisent une notation préfixée.

(setf x expr) Lisp
(set! x expr) Scheme[10],[11],[12]
set x expr Tcl
expr x ! Forth

Notes et références[modifier | modifier le code]

  1. (en) Lawrie Moore, Foundations of Programming with Pascal, New York, John Wiley & Sons, (ISBN 0-470-26939-1)
  2. (en) Bertrand Meyer, Eiffel the Language, Hemel Hempstead, Prentice Hall International(UK), (ISBN 0-13-247925-7)
  3. (en) Richard Wiener, An Object-Oriented Introduction to Computer Science Using Eiffel, Upper Saddle River, New Jersey, Prentice Hall, (ISBN 0-13-183872-5)
  4. (en) Neal Feinberg, Keene, Sonya E., Mathews, Robert O. et Withington, P. Tucker, Dylan Programming, Massachusetts, Addison Wesley, (ISBN 0-201-47976-1)
  5. (en) The Go langage Specification: short variable declarations
  6. (en) Jeffrey D. Ullman, Elements of ML Programming: ML97 Edition, Englewood Cliffs, New Jersey, Prentice Hall, (ISBN 0-13-790387-1)
  7. (en) Kenneth E. Iverson, A langage, John Wiley and Sons, (ISBN 0-471-43014-5, lire en ligne)
  8. (en) Paul Graham, ANSI Common Lisp, New Jersey, Prentice Hall, (ISBN 0-13-370875-6)
  9. (en) Guy L. Steele, Common Lisp: The Language, Lexington, Digital Press, (ISBN 1-55558-041-6)
  10. (en) R. Kent Dybvig, The Scheme langage: ANSI Scheme, New Jersey, Prentice Hall, (ISBN 0-13-454646-6)
  11. (en) Jerry D. Smith, Introduction to Scheme, New Jersey, Prentice Hall, (ISBN 0-13-496712-7)
  12. (en) Harold Abelson, Sussman, Gerald Jay et Sussman, Julie, Structure and Interpretation of Computer Programs, New Jersey, McGraw-Hill, (ISBN 0-07-000484-6)